If a vector contains a bunch of elements that were allocated using new and then dereferenced, is the memory for those elements freed when the vector is deleted? For example:
vector<Obj> a = *(new vector<Obj>());
a.push_back(*(new Obj()));
delete &a;
Is the new Obj that was created deallocated?
This is all going to go horribly wrong. Firstly, the assignment on the first line, “a” is on the stack now (not the heap), and the memory allocated by your
newstatement is now lost, leaked, gone.Secondly, re-occurrance of the same situation. Obj() will be constructed, and copied. The memory from your 2nd
newlost in the depths of space and time, forever.Then you try and delete an object allocated on the stack. This is where your program crashes and burns and all that memory loss is inconsequential.