Original Post
Hello all,
I'm having some difficult with C++ when it comes to decide whether or not to use heap or stack allocation. So far, I believe I have understood the basics, please correct me if I'm wrong.
- Stack allocation: faster, automatically freed and should be used for 'small types' since it's usually a lot smaller than heap (though, the size is definable on the compiler)
- Heap allocation: slower and you have to manage it (manual release)
Therefore, from I what understand, inside a function, if the object to be created is 'small' then it should be put on the stack, otherwise one should put it in the heap.
My main doubt right now is what happens when they're fields (attributes) of a class. For example, please consider the following simplified class hierarchy:
1. If I allocate a CharacterStackAnim in the heap (using operator new), does the 'animation' field also get stored in the heap? Is there any difference from allocating CharacterHeapAnim on the heap?
2. How does one decide whether to make a field (like animation) a pointer or not (whether or not to dynamically allocate it)? Consider that the animation won't be shared with other characters.
3. In Animation, what would be better std::vector or std::vector? What do you need to consider in order to answer such a question?
I would be very grateful if you could answer these questions or point me to a good source for further studies.
Thanks in advance,
Victor Freire
I'm having some difficult with C++ when it comes to decide whether or not to use heap or stack allocation. So far, I believe I have understood the basics, please correct me if I'm wrong.
- Stack allocation: faster, automatically freed and should be used for 'small types' since it's usually a lot smaller than heap (though, the size is definable on the compiler)
- Heap allocation: slower and you have to manage it (manual release)
Therefore, from I what understand, inside a function, if the object to be created is 'small' then it should be put on the stack, otherwise one should put it in the heap.
My main doubt right now is what happens when they're fields (attributes) of a class. For example, please consider the following simplified class hierarchy:
class Timer {
std::string name;
int duration;
int elapsedTime;
public:
// ( . . . )
};
class Frame {
std::string sourceImageId;
int sourceX,
sourceY,
sourceW,
sourceH;
int duration;
public:
// ( . . . )
};
class Animation {
Timer timer;
std::vector<Frame> frames;
int currentFrame;
public:
// ( . . . )
};
class CharacterStackAnim {
Animation animation;
public:
// ( . . . )
};
// Same class as above except that it allocates Animation dynamically
class CharacterHeapAnim {
Animation* animation;
public:
CharacterHeapAnim() { animation = new Animation(); }
~CharacterHeapAnim() { delete animation; }
// ( . . . )
};
1. If I allocate a CharacterStackAnim in the heap (using operator new), does the 'animation' field also get stored in the heap? Is there any difference from allocating CharacterHeapAnim on the heap?
2. How does one decide whether to make a field (like animation) a pointer or not (whether or not to dynamically allocate it)? Consider that the animation won't be shared with other characters.
3. In Animation, what would be better std::vector or std::vector? What do you need to consider in order to answer such a question?
I would be very grateful if you could answer these questions or point me to a good source for further studies.
Thanks in advance,
Victor Freire