Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有两个工具:gcc5 和 gcc8。以下是片段代码
bool foo() { int var; var = 1; printf("var=%d\n", var); } int calling_foo() { foo(); }
如果我使用 gcc5 编译并运行,则 foo() 调用会返回。但是,如果我使用 gcc8 编译并运行,则 foo() 调用不会返回。
我理解,foo() 中没有返回值,但至少函数应该返回。我认为 gcc8 要严格得多。但是为什么电话没有返回。
您的程序(假设它正在调用calling_foo或foo直接调用)具有未定义的行为,因为执行流程将到达函数体的右括号,该foo函数体被声明为返回 non- void。这本身会导致 C++ 中未定义的行为。
calling_foo
foo
void
因此 GCC 的两个版本都是正确的。他们可以发出或不发出任何他们想要的代码。不保证该程序以任何特定方式运行。