One of the C++ features that sets it apart from other languages is the ability to allocate complex objects as member variables or local variables instead of always having to allocate them with new
. But this then leads to the question of which to choose in any given situation.
Is there some good set of criteria for choosing how to allocate variables? When should I declare a member variable as a straight variable instead of as a reference or a pointer? When should I allocate a variable with new
rather than use a local variable that's allocated on the stack?
Java Heap Space is used throughout the application, but Stack is only used for the method — or methods — currently running. The Heap Space contains all objects are created, but Stack contains any reference to those objects. Objects stored in the Heap can be accessed throughout the application.
Stack space is mainly used for storing order of method execution and local variables. Stack always stored blocks in LIFO order whereas heap memory used dynamic allocation for allocating and deallocating memory blocks. Memory allocated to the heap lives until one of the following events occurs : Program terminated.
Stack accesses local variables only while Heap allows you to access variables globally. Stack variables can't be resized whereas Heap variables can be resized. Stack memory is allocated in a contiguous block whereas Heap memory is allocated in any random order.
One of the C++ features that sets it apart from other languages
... is that you have to do memory allocation manually. But let's leave that aside:
Note that in the second rule, by "large object" I mean something like
char buffer[1024 * 1024]; // 1MB buffer
but not
std::vector<char> buffer(1024 * 1024);
since the second is actually a very small object wrapping a pointer to a heap-allocated buffer.
As for pointer vs. value members:
The use of smart pointers is of course recommended where appropriate. Note that you can use a reference in case of heap allocation because you can always delete &ref
, but I wouldn't recommend doing that. References are pointers in disguise with only one difference (a reference can't be null), but they also signal a different intent.
There is little to add to the answer of larsmans.
Allocating on the stack usually simplifies resource management, you do not have to bother with memory leaks or ownership, etc. A GUI library is built around this observation, check at "Everything belongs somewhere" and "Who owns widgets."
If you allocate all members on the stack then the default copy ctor and default op= usually suffices. If you allocate the members on the heap, you have to be careful how you implement them.
If you allocate the member variable on the stack, the member's definition has to be visible. If you allocate it on the heap then you can forward declare that member. I personally like forward declarations, it reduces dependency.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With