1

Visual Studios and even Codechef is compiling my code which has a missing return statement.

I was solving some competitive programming question and noticed that my program is compiled without a return statement in a function. I wrote a simple function and didn't mention any return statement and the program is being compiled perfectly.

Here's the code

#include <iostream>
using namespace std;

int add(int x, int y)
{
    int c = x + y;
}

int main() {

    int a = add(1, 2);
    cout << a;
    return 0;
}

I was expecting an error which I didn't get and made me wonder what was wrong with my code(of-course not the above one). And in the program above I'm getting output 0 which I don't understand how?

Oblivion
  • 7,176
  • 2
  • 14
  • 33
Sarthik Garg
  • 31
  • 1
  • 3
  • 2
    It's undefined behavior. – machine_1 Nov 06 '19 at 19:29
  • but I'm getting this behaviour from two different independent compilers – Sarthik Garg Nov 06 '19 at 19:32
  • 2
    I would expect a warning about this if you complied it modern compiler and a reasonable warning level. – drescherjm Nov 06 '19 at 19:42
  • The compiler is *not* required to tell you when you break the rules of the language. That's all on *you*. Undefined Behaviour, no diagnostic required is a big C++ dragon you have to appease/fight/avoid. – Jesper Juhl Nov 06 '19 at 19:45
  • I'm using Visual Studio 2019 as IDE and well, from it I expect an error – Sarthik Garg Nov 06 '19 at 19:50
  • 2
    Check your warning/error level flags; warnings from clang, g++, MSVC live: https://godbolt.org/z/Bx0DQr – Richard Critten Nov 06 '19 at 19:50
  • 1
    *but I'm getting this behaviour from two different independent compilers* -- Each one of those compilers have many options to build a program, so it isn't just "two compilers". Multiply those two by the many options, and you have literally hundreds, if not thousands of ways you could have built your program that could make a difference. – PaulMcKenzie Nov 06 '19 at 19:54
  • Does this answer your question? [What should main() return in C and C++?](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) – rsjaffe Nov 06 '19 at 20:11
  • 1
    Sadly this is a place where you do not get an error from the compiler because in exceedingly rare occasions you don't want a hard error here. – user4581301 Nov 06 '19 at 20:12
  • On my machine, my compiler produces 3 warnings. When I run the program, it crashes. – Eljay Nov 06 '19 at 22:18

2 Answers2

7

A function that doesn’t return anything as it was expected to return, has undefined behavior.

Flowing off the end of a value-returning function (except main) without a return statement is undefined behavior.

That is why you should never ignore warnings. /Wall or /w4 is the flag you need on visual studio.

Oblivion
  • 7,176
  • 2
  • 14
  • 33
0

many compilers store returned value in EAX register which in your case holds the result of the add operation. so it will report the correct value. but you should not depend on that in future here is how vs2019 compile this

int c = a + b;
01001FD8  mov         eax,dword ptr [a]  
01001FDB  add         eax,dword ptr [b]  
01001FDE  mov         dword ptr [c],eax  
    return c;
01001FE1  mov         eax,dword ptr [c]  

}
01001FE4  pop         edi  
01001FE5  pop         esi  
01001FE6  pop         ebx  
01001FE7  add         esp,0CCh  
01001FED  cmp         ebp,esp  
01001FEF  call        __RTC_CheckEsp (01001294h)  
01001FF4  mov         esp,ebp  
01001FF6  pop         ebp  
01001FF7  ret  
Ahmed Anter
  • 650
  • 4
  • 13
  • 4
    It's Undefined Behaviour and I would not recommend anything but changing the compiler options to show the problem and fixing the code. – Richard Critten Nov 06 '19 at 19:56
  • me too not recommend it. I am just describing how it worked – Ahmed Anter Nov 06 '19 at 20:11
  • 1
    If you check release code (see my live link in the comment to OP) this does not happen. An answer is recommending a solution to OP (or it's not an answer). If it's just a description it should be a comment. – Richard Critten Nov 06 '19 at 20:44