As far as I know that for both vector declarations as:
//TYPE 1
std::vector<cls> vec; //cls is user defined datatype(A class)
Memory for vector is allocated on stack and the memory of contents in the vector is allocated on heap.
It is true for the below declaration as well(Correct me if I am wrong):
//TYPE 2
std::vector<cls*> vec; //cls is user defined datatype(A class)
Now when the vector in Type 1 goes out of scope, memory is deallocated for the objects stored in it.
But what happens in type 2 if I insert elements as below(assuming that I have the proper overloaded constructor) and then the vector goes out of scope:
vec.push_back(new cls(5));
I explicitly tried calling clear but the destructor was not invoked. Will the memory be automatically be deallocated and the destructors be called. If not then how to achieve that.
Also, where is memory allocated for the vector as well as the contents if I declare vector as:
std::vector<cls*> *vec = new std::vector<cls*>;