Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to the return value if I don't store it anywhere?

Tags:

c++

In the small sample below:

#include<iostream>
using namespace std;

int z(){
    return 5 + 10; // returns 15
}

int main(){
    z(); // what happens to this return?
    cout << "Did not fail";
    return 0;
}

What happens to the 15? I tried running it in debugger but I can't find it anywhere. I assume that because it didn't get assigned to anything it just vanished but I feel like that's wrong.

I asked my TA about this today and he told me it's stored on the call stack but when I viewed it in debugger I see that it is not.

like image 468
Callat Avatar asked Feb 08 '17 00:02

Callat


People also ask

What happens if you call a function with a return value without storing or using the returned value?

Yes, You can call a function but never store the returned value by that function in java. But it will just unnecessarily consume processing time. Because you will not be achieving what you want to achieve by first declaring that you want some processing to be done by method and return a final output.

Where is return value stored?

The return value is stored in registers - on x64 it goes in RAX. On x86, 64-bit structures like DateTime are returned in EAX:EDX.

What happens if you don't return a value?

If you do not explicitly return a value, the caller will nonetheless use whatever garbage happens to be in that register. The compiler will also use all registers it has available for internal computation within the function.

Is the return value temporary?

Formally: the return value is always a temporary. In contexts where that temporary is used as the argument of a copy constructor (the object is copied), the standard gives explicit authorization for the compiler to elide the copy, “merging” the temporary with the named variable it is initializing.


1 Answers

The C++ standard imposes the "as-if" rule. That rule means that a C++ compiler can do anything to a program as long as all side effects (inputs and outputs that are visible to the rest of the system, like writing to a file or showing stuff on the screen) are respected. Going back to my cheeky philosophical comment, this means that in C++, when a tree falls in the forest and no one is there to hear it, it doesn't have to make a sound (but it can).

In the case of your program, at a high level, since your function does nothing, the compiler may or may not create a call to it, or could even remove it from the compiled binary. If it does include and call it, the return value will go to whatever return slot your platform's application binary interface specifies. On almost every x86_64 system, that will be the rax register for an integer return value. The return value is there but will never be read and will be overwritten at some point.

If it was a non-trivial object instead of an int, its destructor would be invoked immediately.

like image 129
zneak Avatar answered Nov 15 '22 16:11

zneak