Say I have:
vector<string>* foo = new vector<string>();
I add a ton of stuff to it, use it, and then I just call:
delete foo;
Did I need to call foo.clear(); first? Or will the delete call the destructor.
Please no comments regarding the folly of this, I know that at a minimum auto-pointers should be used here. This behavior is in the code base I'm working in and it's outside scope for me to go fix it.
Yes, the
vector's destructor will be called, and this will clear its contents.deletecalls the destructor before de-allocating memory, and vector's destructor implicitly calls.clear()(as you know from letting an automatic-storage durationvectorfall out of scope).This is quite easy to test, with a
vector<T>whereTwrites tostd::couton destruction (though watch out for copies inside of thevector):(live demo)