A blog about software and making.

Returning ptr to stack vs. heap allocated instance in C++

1
2
3
4
5
6
7
8
9
10
11
12
13
class Thingy;

Thingy* foo()
{

int a; // Stack
Thingy b; // Created on stack
Thingy *ptr_to_b = &b; // Points to address on stack
Thingy *ptr_to_c = new Thingy(); // Points to address of thingy created on heap

return pointer_to_c; // Safe - Thingy lives on heap and outlives foo(). Remember to delete.

return pointer_to_b; // BOOM! Because b lives on stack and will be deleted when foo() returns.