-1

I have created a local variable b in the function foo(). This function returns the address of the variable b. As b is a local variable to the functionfoo(), the address of the variable is not supposed to be there as the execution of the function foo() finishes. I get the following warning message:

C:\Users\User\Documents\C codes\fun.c||In function 'foo':
C:\Users\User\Documents\C codes\fun.c|6|warning: function returns address of local variable [-Wreturn-local-addr]|

But the pointer ptr of the main() function successfully receives the address of b and prints it. As far as I know about stack, the address of the local variable b should cease to exist after the control of the program returns to the main() function. But the output proves otherwise.

#include<stdio.h>

int* foo()
{
    int b = 8;
    return &b;
}

int main()
{
    int *ptr;
    ptr = foo();
    
    printf("b = %d", *ptr);
    
    
    return 0;
}

Output:

b = 8

  • No magic there. When a function returns, its stack (where local variable live in a function) is released for re-use and cannot be referred to after return. So `b` is local to function `foo()` and after `foo()` returns, `b` no longer exists. However, you can make it `int foo()` then `return b;` and you can return the value of `b`, but not its address. The fact that it *Seems To Work* -- welcome to *Undefined Behavior* -- it may work sometimes and SegFault the next -- or anything in between. – David C. Rankin Aug 09 '20 at 07:22
  • Your code on my box produces `"Segmentation fault (core dumped)"` -- not `b = 8`. In fact simply changing the optimization level will likely change the results. Try compiling with `-Ofast` (gcc/clang) or `/Ox` (VS) and try running the code again. – David C. Rankin Aug 09 '20 at 07:28
  • See: [Undefined, unspecified and implementation-defined behavior](https://stackoverflow.com/q/2397984/3422102) and [What Every C Programmer Should Know About Undefined Behavior #1/3](http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html) – David C. Rankin Aug 09 '20 at 07:32
  • try calling another function between foo() and printf() . Should this new function define any local variables, the memory storing the value of b would probably be overwritten – yairhoff Aug 09 '20 at 08:23

1 Answers1

2

The lifetime of an object is the portion of program execution during which storage (memory) for it is reserved.

When we say an object ceases to exist in the C model, we mean that reservation ends. The memory is no longer reserved for the object. This is merely a bookkeeping operation; the memory might or might not be used for another purpose. The fact that the reservation ends does not mean anything enforces a rule that you must not use the memory.

What happens when you use an object after its memory reservation ends is similar to what happens when you trespass in the real world. Maybe nobody notices and you get away with it. Maybe somebody notices and stops you. Maybe somebody changes what was there, and maybe they do not.

In the case you tested, nothing changed the memory where b was, and you got away with using memory that was no longer reserved for b. But you cannot rely on this; there is no rule that says it will always work.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312