简体   繁体   English

指向 c 中的主 function 的指针

[英]Pointer to main function in c

When we make a pointer to the main function and call it is it duplicating the data in the stack memory like in recursive or just point at it and start over.当我们创建一个指向主 function 的指针并调用它时,它是像递归一样复制堆栈 memory 中的数据,或者只是指向它并重新开始。

This is valid C code:这是有效的 C 代码:

int main(void) {
    main();
}

If you compile and run it, you will notice it will crash with a segmentation fault.如果您编译并运行它,您会注意到它会因分段错误而崩溃。 You're basically blowing up the stack.你基本上是在炸毁堆栈。 This shows it is actually consuming a bit of the stack on each nested call.这表明它实际上在每个嵌套调用中消耗了一些堆栈。


As a further example:再举一个例子:

Like any recursive function, you need to have a mechanism that guarantees a recursion limit within the range of what your environment allows you to use.与任何递归 function 一样,您需要有一种机制来保证递归限制在您的环境允许使用的范围内。

#include <stdio.h>

int main(int argc, char *argv[]) {
    if (argc > 1) {
        main(--argc, ++argv);
        printf("%s\n", *argv);
    }
}

This will print all command line arguments in reverse order.这将以相反的顺序打印所有命令行 arguments。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM