Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我可以使用宏而不是递归来避免分段错误吗?_C - Fatal编程技术网

C 我可以使用宏而不是递归来避免分段错误吗?

C 我可以使用宏而不是递归来避免分段错误吗?,c,C,这是一个来自美国的延伸问题 在这里,由于递归导致堆栈溢出,所以出现分段错误 所以我把他的密码改成这样 用宏代替该函数,以便删除函数调用 #include <stdio.h> static inline void p(char *a, int b); #define MAGIC(a,b) p(a,b) void p(char *a, int b) { static long int i = 0; if (i != 350000) {

这是一个来自美国的延伸问题

在这里,由于递归导致堆栈溢出,所以出现分段错误

所以我把他的密码改成这样

用宏代替该函数,以便删除函数调用

#include <stdio.h>

static inline void p(char *a, int b); 


#define MAGIC(a,b)  p(a,b) 

void p(char *a, int b) 
{
    static long int i = 0;

    if (i != 350000) 
    {
        printf("\n%ld \t at Hi hello", i);
        i++;
        return MAGIC(a, b);
    } else 
    {
        return;
    }
}


int main() 
{
    char *a = "HI";
    int b = 10;
    MAGIC(a, b);
    printf("\nComplete");
    return 0;

}
#包括
静态内联void p(字符*a,整数b);
#定义魔术(a,b)p(a,b)
无效p(字符*a,整数b)
{
静态长整数i=0;
如果(i!=350000)
{
printf(“\n%ld\t在Hi hello”,i);
i++;
回归魔法(a,b);
}否则
{
返回;
}
}
int main()
{
char*a=“HI”;
int b=10;
魔术(a,b);
printf(“\n完成”);
返回0;
}

我仍然得到分割错误…仍然堆栈溢出。。。。为什么?

不,它不起作用。宏只是文本复制粘贴,因此结果仍然相同

因此,您的代码将扩展为:

void p(char *a, int b) 
{
    static long int i = 0;

    if (i != 350000) 
    {
        printf("\n%ld \t at Hi hello", i);
        i++;
        return p(a, b);
    } else 
    {
        return;
    }
}


int main() 
{
    char *a = "HI";
    int b = 10;
    p(a, b);
    printf("\nComplete");
    return 0;

}
它仍然具有递归性,可能会发生堆栈溢出

编辑:重新设计算法的一种方法如下:

void p(char *a, int b) 
{
    long int i = 0;

    while (i != 350000)
    {
        printf("\n%ld \t at Hi hello", i);
        i++;
    }
}
改变
返回魔法(a,b)

goto启动
并在函数开头添加一个
START
标签

编辑:

使用
while
循环的示例:

void p(char *a, int b) 
{
    static long int i = 0;

    while (i != 350000) 
    {
        printf("\n%ld \t at Hi hello", i);
        i++;
    } 
}
void p(char *a, int b) 
{
    long int i = 0; // static seems wrong here

    for (;i != 350000; i++) 
    {
        printf("\n%ld \t at Hi hello", i);
    } 
}
使用
for
循环的示例:

void p(char *a, int b) 
{
    static long int i = 0;

    while (i != 350000) 
    {
        printf("\n%ld \t at Hi hello", i);
        i++;
    } 
}
void p(char *a, int b) 
{
    long int i = 0; // static seems wrong here

    for (;i != 350000; i++) 
    {
        printf("\n%ld \t at Hi hello", i);
    } 
}

是的,我正在避免函数调用。我只是将函数代码复制到宏中。所以不要使用堆栈。那么我如何使用宏来避免函数调用?有可能吗?简而言之,你不能,除非你在某种程度上重新设计算法。@Mysticial:你的
while
循环是错误的。您需要省略
返回p(a,b)部分:)嘿,伙计,它工作了..但goto对嵌入式系统有危险吗?
goto
只有在(ab)使用不当时才有危险。所有循环都只是
goto
的语法糖。您可以重写代码以使用
while
for
循环使其“更漂亮”。您希望程序做什么,为什么?为什么不直接使用循环呢?