Stack frame memory allocation

Like any function is pushed onto the stack to execute it, and it will redden upon completion. This way, any local variable will not be available to other functions. But then how can we return a local variable to the caller?

int pickMin( int x, int y, int z ) {
 int min = x ;
 if ( y < min )
    min = y ;
 if ( z < min )
    min = z ;
 return min ;   }

      

The above code works fine. However, in the code below, the compiler prints the warning message " warning: function returns address of local variable [-Wreturn-local-addr] return a;

" but at the end it prints the garbage value, which I think is fine because the variable has already been cleared. But why didn't this happen in ABOVE ?! I mean, he also had to give me back the cost of the trash. Also, I know that the problem in the code below can be solved with malloc

and then return that value. :)

int *returnarray(){
 int a[10]; int i;
  for(i=0;i<10;++i){
     a[i] = i;
 }return a;}   

      

+3


source to share


4 answers


C passes all values ​​by value. In your first snippet, return min

returns a variable int

. Its value is returned. The second fragment consists of return

and the name of the array, which decays to a pointer.
The return value is the memory address of the local variable. The function in which this variable existed returned and nevertheless accessed the memory used by this function, then invokes undefined behavior.



The way to handle this situation (i.e. need to return an array) is either by passing the target array to the function as an argument, or by allocating memory using malloc

and returning that pointer.
> Heap memory is slightly slower, more error prone and requires your attention. However, here's an example of both approaches
create_fill

allocates, assigns and returns a pointer to heap memory, fill_array

returns nothing but expects you to pass an array (which decays to a pointer) and the maximum length to fill. The advantage is that stack memory doesn't require as much care and outperforms heap.

+4


source


The operator return

accurately describes this: it copies the value of the variable and leaves it on top of the stack so that the calling function can use it. Now in C this works for simple values, not for arrays, because your "array variable" a

is actually the address of only its first value.



+2


source


First read the wikipage call stack carefully. It has beautiful photos. See also the x86 wikipage calling conventions.

Then the result (of some C function) is often passed through the register on return (or for large struct

-s in the stack space allocated by the caller).

ABI details . For Linux on x86-64 (in 64 bit), the x86-64 ABI mentions a register %rax

to return the result (in general cases, but when the result is large struct

, the caller passes it the address).

BTW, in principle, I believe that nothing in the C99 standard requires a stack, but I don't know of C implementations without a call stack (usually a processor stack, i.e. via a stack register ).

+1


source


In the first case, you are returning the value of a variable.

So far, in the second case, you are returning the address of a local variable, which, as you correctly said, is not available to other functions. In C, the name of an array is the base address of that array. Therefore, in the second case, the "base address of the array" is copied into any variable / pointer to which the return value of this function is assigned

0


source







All Articles