3
#include<stdio.h>

int* a(int* b){
 int a = 20;
 printf("\n  \n");
 if(a == 20){
  printf("\n return from if a : 0x%x \n",a);
  return &a;
 }
 else{
  printf("\n returning from else b : 0x%x\n",b);
  return b;
 }
}

int main(){
 int n = 10;
 int *k,*m;
 k = &n;
 m = a(k);
 printf("\n m ; 0x%x m : %d \n",m,*m);
 return 0;
}

Here i'm returning the local variable of the function returning pointer. During function exit all the variables will bere moved from the stack memory , but how does the function still preserves the value at the address 'a' and prints the data ?

o/p :

 return from if a : 0xbfd8cf14

 m ; 0xbfd8cf14 m : 20

The address is retained in the pointer m and it prints the value correctly. I tried changing different no's.

Angus
  • 12,133
  • 29
  • 96
  • 151
  • 1
    Tagged C++, but completely relevant: http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope – chris Apr 09 '13 at 13:38
  • @chris : The link has a very good explanation . Thanks. – Angus Apr 09 '13 at 13:56
  • 1
    The hotel room answer is a bit famous here. – chris Apr 09 '13 at 13:58
  • possible duplicate of [Returning the address of local or temporary variable](http://stackoverflow.com/questions/2744264/returning-the-address-of-local-or-temporary-variable) – Jonathan Leffler Apr 11 '13 at 22:10

4 Answers4

6

It's that your program invokes undefined behavior. It is free to print anything (or crash, or do whatever it wants).

(Perhaps this is what actually happens: the "removal from memory" doesn't mean that the memory which held the variable is destroyed, set on fire or made disappear by a magician. It's just that it's invalidated. Since it was an automatic variable within a function, most likely what happened is simply that the stack pointer was shifted when the function returned, leaving the variable invalid but intact. Don't rely on this, though.)

3

You could make the return value "reliable" by declaring static int a = -1; which would make a persist even after you return from the function... but that's a really bad idea in many cases... especially if you are going to be multi-threaded.

That said... returning a pointer to a temporary (local) variable is going to cause havoc at runtime. So, you don't ever want to do that... you need to make it a static local or find a better way to deal with it.

Extending the answer after your edits: when you assign -1 to a as a local variable, you're actually storing -1 at a location on the stack and &a points to that location. When you return from the function, the stack is not destroyed, but the behavior is now undefined because while the address is a valid pointer, the content at that address may well have been modified... for example, if you call another function that pushes data on the stack and/or declares local variables, they may well have overwritten the value you expected. In addition, who knows what madness the optimizer may have injected. So... it might work if you happen to read the value before anything else mucks with it... but you have no guarantee it will work and it will often vary from one implementation to another.

K Scott Piel
  • 4,320
  • 14
  • 19
3

The returned pointer will be invalid after exiting the functions. It's a undefined behavior.

The preserved pointer points to a position in memory which you have not permission to read/write on it any more.

To preserve the address valid, use static variable.

static int a = -1;
// ...
return &a;
masoud
  • 55,379
  • 16
  • 141
  • 208
2

It is an undefined behavior. When you leave the function, the stack is not cleaned up, so the actual value of the variable is still there, but you are not supposed to access it. Try calling another function before showing the content of your variable. Try compiling with -O3 for a possibly different behavior.

Fabien
  • 12,486
  • 9
  • 44
  • 62