In C++, when is it best to use the stack? When is it best to use the heap?
C++ – When to Use Stack vs Heap
c++
Related Solutions
I'll start from the beginning...
Vector** v = new Vector*[100];
Allocates an array of 100 pointers to objects of type Vector on the heap It returns one pointer- v - the you can use to keep track of this array of pointers.
Delete this array of 100 points with:
delete[] v;
(Use the delete
operator- delete
for a single allocated object, delete[]
for an array)
Next case (I'm assuming you mean new Vector[100]
:
Vector* v = new Vector[100];
You allocated an array of 100 Vectors on the heap and got a pointer to its start location- v. Delete this array with:
delete[] v;
Next...
class Vector
{
int x, y, z;
}
Vector* v = new Vector();
This allocates an object of class Vector on the heap and gives you a pointer to keep track of it. Because you allocated the entire object on the heap, x, y, and z are all allocated on the heap.
Delete it with:
delete v;
class Vector2
{
int items[10];
}
Vector2* v2 = new Vector2();
This one is a bit trickier but I'm going to reason it out...
Classes are blueprints. You haven't allocated any memory at all until you instantiate the class somehow, in this case on the heap. Because the class is a blueprint, items
could not have been allocated until you created an object of class Vector2
on the heap. I think we can reasonably infer that items
is thus allocated on the heap.
Delete v2 with:
delete v2;
And finally:
class Vector3
{
int* items;
}
Vector3 v3 = Vector3();
You allocated all of class Vector3 on the stack, the pointer inside of it items
is also allocated thus. Nothing went on the heap, so don't delete it.
Depending on the context we can consider heap or stack. Every thread gets a stack and the thread executes instructions by invoking functions. When a function is called, the function variables are pushed to stack. And when the function returns the stack rollbacks and memory is reclaimed. Now there is a size limitation for the thread local stack, it varies and can be tweaked to some extent. Considering this if every object is created on stack and the object requires large memory, then the stack space will exhaust resulting to stackoverflow error. Besides this if the object is to be accessed by multiple threads then storing such object on stack makes no sense.
Thus small variables, small objects who's size can be determine at compile time and pointers should be stored on stack. The concern of storing objects on heap or free store is, memory management becomes difficult. There are chances of memory leak, which is bad. Also if application tries to access an object which is already deleted, then access violation can happen which can cause application crash.
C++11 introduces smart pointers (shared, unique) to make memory management with heap easier. The actual referenced object is on heap but is encapsulation by the smart pointer which is always on the stack. Hence when the stack rollbacks during function return event or during exception the destructor of smart pointer deletes the actual object on heap. In case of shared pointer the reference count is maintained and the actually object is deleted when the reference count is zero. http://en.wikipedia.org/wiki/Smart_pointer
Best Answer
Use the stack when your variable will not be used after the current function returns. Use the heap when the data in the variable is needed beyond the lifetime of the current function.