gpt4 book ai didi

c++ - 当我尝试调用 main 时,它工作了几次,但后来我遇到了 Segmentation fault

转载 作者:太空狗 更新时间:2023-10-29 23:50:14 24 4
gpt4 key购买 nike

为什么我看到段错误?代码:

#include<stdio.h>
int main()
{
puts("WORK");
main();
}

[ how i run program ][ screenshot of error ]

最佳答案

C++ 标准 3.6.1/3 不允许您在 main() 中调用 main()

The function main shall not be used (3.2) within a program. The linkage (3.5) of main is implementation defined. A program that declares main to be inline or static is illformed. The name main is not otherwise reserved. [Example: member functions, classes, and enumerations can be called main, as can entities in other namespaces. ]

你最终会遇到堆栈溢出。(实际上,在 main 中调用 main 是一种未定义的行为)

used(3.2) 说:

An object or non-overloaded function is used if its name appears in a potentially-evaluated expression.

还有这个:

5.2.2.9
"Recursive calls are permitted, except to the function named main"

关于c++ - 当我尝试调用 main 时,它工作了几次,但后来我遇到了 Segmentation fault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33015748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com