-3

I have two tools: gcc5 and gcc8. Following is the snippet code

bool foo() {
  int var;
  var = 1;
  printf("var=%d\n", var);
}

int calling_foo() {
  foo();
}

If I compile and run w/ gcc5, foo() call returns. But, if I compile and run w/ gcc8, foo() call doesn't return.

I understand, there is no return value in foo(), but at least the function should return. I assume that gcc8 is far more stricter. But why the call is not returning.

Suresh
  • 27
  • 1
  • 8
  • 1
    What does "foo() call doesn't return" mean? Does get stuck in an infinite loop? Does it not return a meaningful value? – ForceBru Mar 21 '20 at 19:41
  • 1
    Provide a [mre] and state specifically the observed behaviors that lead to you conclude that `foo` returns in one situation and not the other. For example, if the program prints certain output in one case but another, show that. If you are examining its control flow in the debugger, show that. Provide all information needed for others to reproduce the issue. – Eric Postpischil Mar 21 '20 at 19:44
  • 2
    not returning something from a function declared to return a `bool` is undefined behaviour, its not about which compiler is more strict. Compilers are not required to issue a warning/error when your code has UB – 463035818_is_not_an_ai Mar 21 '20 at 19:44
  • 1
    You have a function returning non-void that doesn't explicitly return anything. That's undefined behaviour (unless the function in question is `main`). – G.M. Mar 21 '20 at 19:45
  • "I understand, there is no return value in foo(), but at least the function should return" thats what you wish, unfortunately thats not how the language is specified ;) – 463035818_is_not_an_ai Mar 21 '20 at 19:46
  • That would be legal (If poor style) in C, but C++ is a different beast. – Shawn Mar 21 '20 at 20:04

1 Answers1

1

Your program (assuming it is calling calling_foo or foo directly) has undefined behavior, because execution flow will reach the closing brace of the foo function body, which is declared to return non-void. That, by itself, causes undefined behavior in C++.

Therefore both versions of GCC are correct. They can emit or not emit whatever code they want. The program is not guaranteed to behave in any particular way.

walnut
  • 21,629
  • 4
  • 23
  • 59