How do C ++ progs get their return value when no return is specified in the function?

I recently wrote a message: A
strange error in a C ++ program: deleting a print-out program

... in which I tried to solve a seemingly incomprehensible problem in which deleting the cout instruction would violate my program.

As it turned out, my problem was that I forgot to return the true / false success flag, which I later used for logic.

But, apparently, SOMETHING was returning and something was always true if I left this cut, but, it would seem, “magically” would become false when I pulled it out.

My question is for all of you:
What determines what a C ++ function returns when the return command is not executed in the function? Is there any logic?

Obviously, forgetting your return type is a bad idea. In this case, however, it was largely due to the nature of my program - fast work with hacking. Later, I decided that it was not worth the effort to implement an algorithm to determine the success / failure of a function call, but accidentally left a code depending on the return.

Bafflingly g ++ did not give me any warnings or errors when compiling the executable as follows:

g++ main.cc -g -o it_util

My version: g ++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)

Again, in order to preserve other future frustrations if they make the same stupid mistake and encounter the same seemingly unstable behavior, can anyone shed light on where the function without returning gets its return value from

Thank!!

+5
6

x86 EAX. :

int func() {
    if(0) return 5; // otherwise error C4716: 'func' : must return a value
}
int main() {
    int a;
    a = func();
}

cl.exe /Zi, MSV++ 10:

push    ebp
mov     ebp, esp
push    ecx
call    j_?func@@YAHXZ  ; func(void)
mov     [ebp+a], eax ; assumes eax contains the return value
xor     eax, eax
mov     esp, ebp
pop     ebp
retn

, undefined .

+5

, ++ . C.

K & R C void, , int. ,

myfunc() {....}

, int, , .

. , , return - . . , , . - () , .

+5

++ Standard 6.6.3 return

; undefined .

( 3.6.1/5):

, return 0;

, , . -Wall gcc ( Neil) ; - " ...".

+4

. , 32- Intel , eax.

+1

, , -Wall

, .

0

" , ++, return? - ?"

.

:

int () { int temp; return temp; }

, . : , , . man, -W -Wall -pedantic. g++.

void. , .

void somefunction (int * ptr_int) {int temp = * ptr_int; temp + = 1000; this-> ptr_int = temp; }

I believe that the above code works, it has been a while since I am encoded in C ++.

0
source

All Articles