1

In C++ does a pointer remains valid after stack unwinding or not?

jlordo
  • 37,490
  • 6
  • 58
  • 83
Ehsank
  • 173
  • 3
  • 9
  • Do you mean a pointer stored on the stack, but pointing to the heap, or do you mean you are pointing to something that was on the part of the stack that is now unwound? – quamrana Apr 25 '11 at 09:43

5 Answers5

2

It depends on the storage of the pointed-to object. If that object was stack-allocated then the pointer surely becomes invalid - stack unwinding will properly destroy the object. If the object was heap-allocated the pointer only becomes invalid if there's some RAII variable that deallocates the object during stack unwinding.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
1

No. With stack Unwinding all variables/pointers that are declared in the scopes of the unwounded part of the stack get destroyed.

Also, the rule takes in to consideration the Storage Type of the variables.
for eg: A static variable retains its value between function calls, which means it is not destroyed during stack unwinding. This is due to the fact that Static variable are not stored on the stack but on the BSS or Data Segments.

Local variables(Auto storage type) created on stack inside a function will always be destroyed when function returns and stack unwinding takes place.

Pointers allocated with memory on heap will not be destroyed on stack unwinding because they are allocated on Heap and not stack.

One important rule to remember is NEVER return pointers or references to local variables inside a function. The pointer or reference will contain garbage values.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

It depends on what your pointer is pointing to. If it points to heap memory, it still remains valid. If it points to stack memory, it becomes invalid.

1

Consider some examples:

void* f1a()
{
    void* p = malloc(10);
    return p;
}

...is the same as...

void* f1b()
{
    return malloc(10);
}

That's fine, as the pointers are to the heap and thus independent of the stack, function calls and programmatic scopes. The pointer value is copied as the function returns.

int* f2()
{
    int x;
    return &x;  // pointer to x - about to become invalid!
}

The above returns a pointer to a variable x on the stack, which will be reclaimed (x lost) when the function returns.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
0

just to state one extra thing which i feel is important

say we have this statement

obj* objptr = new obj(9) //allocate memory on heap and use constructor

if here exception occurs.. the memory heap is relased back....ie no memory leak...

the reason is.... not because of stack unwinding...but because the way the new operator is converted into the following generated code the try catch statement are implemented in new operator...something like this...

void * operator new(size_t s)
{

try
{

void * ptr=new(malloc (s))sample(); //placement new
ptr->...

}

catch(..)
{
operator delete(ptr);   //<= notice here   so if an exception occur then first it         is caught here which releases the memory

}
}

however if within the object some memory allocation is done that still doesnt get freed..

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105