I want to write my own container classes and I've some questions.
I've learnt that when we want to add one or more elements to an STL container (e.g. std::vector), it increases the size of the buffer more than we want. For example, if we have a 4-element-vector and if it's capacity is 4, when we want to insert 3 elements somewhere in it, it sets the capacity (or size, whatever you say) more than 7 (e.g. 10). I think this is done to avoid calling ::operator new frequently. But the size increases unnecessarily, right?
So, for an STL-like custom container, which one is more expensive?:
a) Using new/delete every time we want to add/delete elements (To keep the size of the buffer exact: for an n-element-buffer, the size is n*sizeof(element_type) ).
b) Increasing the capacity unnecessarily (To avoid calling ::operator new and ::operator delete every time).
btw, I've heard about "heap compaction" but I don't have any idea about that.
Hope I could describe.
Thx in advance.
programci_84Member Since 23 Feb 2009
Offline Last Active Today, 06:07 AM
- Group Members
- Active Posts 415
- Profile Views 2,021
- Member Title Member
- Age 28 years old
- Birthday July 14, 1984