I read that STL containers will allocate elements in the heap but the container objects are allocated in the stack.
Therefore, in the code below do I need to first clear off the elements of vector "vec" from the heap before deleting "sub" in order to avoid memory leak?
Or, does the STL vector destructor gets called when "sub" gets deleted and it takes care of clearing off the elements from the heap by default?
For sure, if I declared a vector like: std::vector<int> *v = new std::vector<int>(); then I would need to do a delete v. Can anyone help clarify my confusion.
Here is the code:
struct Subscriber {
int cmd;
std::vector<int> vec;
};
Subscriber *sub = new Subscriber();
sub->vec.clear();
delete sub;
Thanks,
Symon.