2
#include<stdio.h>

int add(int,int);

int main()
{
    int a;
    a=add(5,7);
    printf("%d",a);
}

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

Last night I got a doubt on return statement. See x is an auto variable defined inside the add function and as ANSI said an auto variable exists and has its lifetime only inside the function but here the return statement can make the variable a to exist even outside the function. Where does it store the value ? Stack or heap ?

DarkDust
  • 90,870
  • 19
  • 190
  • 224
niko
  • 9,285
  • 27
  • 84
  • 131
  • 1
    In your post, the real question is, WHY VALUE STILL EXISTS outside `add` function and not why variable still exists. Can you access `x` variable outside `add` function? No, IT DOESN'T EXIST anymore. – kazinix Jul 20 '11 at 06:12

6 Answers6

9

Neither on the stack nor on the heap.

At least in the x86 architecture, the return value of functions is usually copied to the eax register, so it persists outside of the function. This is one of the reasons why you can't return an array but merely a pointer to it.

You can, however, return structs and other larger variables by value, in which case the compiler does some tricks by using the stack and additional registers such as edx.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
  • Maybe you add that it will not be true for bigger/complex values and floating point values. – fyr Jul 20 '11 at 06:08
  • bigger type like float/structure will be pushed to stack – kalyan Jul 20 '11 at 06:14
  • I'm not really happy with your answer, I find it too specific: the C is a stack-based language, so the return value *should* be pushed on the **stack** when exiting a function, whatever the architecture. Now, you're right when you say that when the micro-architecture permits it, the compiler can perform some optimization like pushing it on an unused register to speed up the function return, but that shouldn't be considered as a generality. – Gui13 Jul 20 '11 at 08:06
  • 1
    @Gui - C is not a stack based language. Using a stack is just one possible implementation, nothing the language requires. – Bo Persson Jul 20 '11 at 13:36
5

What return actually does depends on the machine architecture to which the code is compiled. On a machine with registers the value of x is copied to a register. The caller will pick up the value from there and do something with it. Here, that value is copied to a variable named a.

If this code is compiled on a stack machine, the return value might be just left on the stack itself. The object code for calling the add function will look something like this:

push 7
push 5
call add
seti 0x28ccf4

add will pop the values from the stack, add them together and push the result back to the stack. (If the function deals with data structures that will not find into a slot on the stack, their addresses are pushed instead.) seti is an operation that will pop a value from the stack and assign it to the integer variable at a particular address. (Here, 0x28ccf4 is the address of a.). As the top of the stack will now contain the result of adding 5 and 7, the value of a will become 12.

Vijay Mathew
  • 26,737
  • 4
  • 62
  • 93
4

It varies. In a typical case, you can count on anything up to at least the size of an int being returned in some register(s) (e.g., typically eax on x86, $v0-$v1 on MIPS, r0 on ARM).

On many machines, larger return values (e.g., a struct with several members) will result in the caller allocating space on the stack to hold the return value, and either passing a pointer to that space as a hidden argument to the function, or the function having implicit knowledge of the location of that space relative to (for example) the stack pointer when the function was entered.

Though the question is tagged C, I'll also mention that in C++ there are a few special rules about return values. Specifically, there's a return value optimization and a named return value optimization. These allow a compiler to elide code for copying a return value, even if/when (for example) the copy would normally have visible side effects. These are intended to allow optimization where the function constructs a return value where the calling code will need it, rather than constructing it locally in the function, and then making a copy back to where the calling code can use it.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
3

It is stored in a register in most platforms I know about, but this could be changed by compiler implementation.

It is worth noticing that while return x where x is a value and not pointer, such as in you case is legitimate, if x is a pointer, you should make sure that the buffer / data it points to is valid when the function returns, otherwise it will be a valid pointer that points to invalid location.

MByD
  • 135,866
  • 28
  • 264
  • 277
2

This is most likely covered in programming language course if you are a CS student. Anyway, you can check out the Wikipedia.

And in fact, it is stored in the call stack, aka stack.

zw324
  • 26,764
  • 16
  • 85
  • 118
  • Actually iam an electronic student.But much intersted in programming the stuff.So i just shifted my work to these. – niko Jul 20 '11 at 06:03
  • 1
    @niko If you are into this kind of stuff, I recommend a book named Programming Language Pragmatics, a thrilling read that may get you to know languages better than most people. – zw324 Jul 20 '11 at 06:05
1

The return value of add function is copied back to 'a' in the main function. The auto variables declared within a function are present in the activation record for that function on the call stack and the activation record is removed at the end of function execution, after copying any return value back to the caller's variable..

Vikdor
  • 23,934
  • 10
  • 61
  • 84
  • Yes, C is a "stack-based" language. Niko if you want to know how the return is handled, you can dive in this [Wikipedia article](http://en.wikipedia.org/wiki/Call_stack). You can also read [this](http://www.eventhelix.com/RealtimeMantra/Basics/CToAssemblyTranslation.htm) article about how C code is translated in machine code, pushing arguments and return values on the stack and retrieving them. – Gui13 Jul 20 '11 at 06:05
  • 1
    An answer on SO might help, it's very clear: http://stackoverflow.com/questions/275214/scope-and-return-values-in-c – Gui13 Jul 20 '11 at 06:16
  • C is not a stack based language. Using a stack is just one possible implementation, nothing the language requires. – Bo Persson Jul 20 '11 at 13:42