Works fine in C ++, but a runtime error in C

I have this code here ::

int f(int x, int *py, int **ppz) { int y, z; **ppz += 1; z = **ppz; *py += 2; y = *py; x += 3; return x + y + z; } int main() { int c, *b, **a; c = 4; b = &c; a = &b; printf( "%d\n", f(c,b,a)); printf( "%d\n", c); } 

(Please select header files accordingly)

When I run this code as a C ++ program, it runs smoothly. But when I run it as a C program, it finishes executing it, prints all the values, but generates a Runtime Error in the last line of code. I connected the debugger, and at the end of the program (when I run this code in C) when I click the "Step-Over" button, it transfers me to the min_gw file, which looks something like this:

 __mingw_CRTStartup() __mingw_CRTStartup+208: mov %eax,%ebx __mingw_CRTStartup+210: call 0x407fb0 <_cexit> __mingw_CRTStartup+215: mov %ebx,(%esp) __mingw_CRTStartup+218: call 0x408088 < ExitProcess@4 > __mingw_CRTStartup+223: nop 

What is this file? And why does the code not work in C ??

Ideal links ::

C :: http://ideone.com/sDbqnI

C ++ :: http://ideone.com/Ubb71k

Thanks for any help in advance .. :)

+5
source share
2 answers

All versions of C ++ and C, starting with C99, add an implicit return 0; to the end of the main file before falling from the end.

C89 (by default for most compilers) is not, so it's UB if you really come back from main, falling from the end.

Edit: in practice (assuming that you are working without disinfectants), this means that you will return everything that is already happening in $eax , i.e. the return value of the last function. Since printf usually returns a nonzero value, which is considered a failure.

+11
source

You forgot the return statement at the end of the main success indicator.

0
source

All Articles