C++ Destructors with Vectors, Pointers,
std::vector and std::strings: Are they destroyed automatically?
Yes (assuming member variables are not pointers to std::vector
and std::string
).
If I have something like std::vector what happens when the vector destructor is called? Would it call automatically the destructor of myClass? Or only the vector is destroyed but all the Objects it contains are still existant in the memory?
If vector<MyClass>
then all objects contained in the vector will be destroyed. If vector<MyClass*>
then all objects must be explicitly delete
d (assuming the class being destructed owns the objects in the vector
). A third alternative is vector
of smart pointers, like vector<shared_ptr<MyClass>>
, in which case the elements of the vector
do not need to be explictly delete
d.
What happens if I have a pointer to another class inside a class
The B
must be explicitly delete
d. Again, a smart pointer could be used to handle the destruction of B
.
You only need to worry about for the memory you have created dynamically (When you reserve memory with new
.)
For example:
Class Myclass{
private:
char* ptr;
public:
~Myclass() {delete[] ptr;};
}
if they are in automatic storage, yes. You can have
std::string* s = new std::string
, in which case you have to delete it yourself.nothing, you need to manually delete memory you own (for memory allocated with
new
).if you allocated
b
withnew
, you should destroy it in the destructor explicitly.
A good rule of thumb is to use a delete/delete[]
for each new/new[]
you have in your code.
A better rule of thumb is to use RAII, and use smart pointers instead of raw pointers.
It depends. std::vector
and std::string
and MyClass
all have 1 thing in common - if you declare a variable to be any of those types, then it will be allocated on stack, be local to the current block you're in, and be destructed when that block ends.
E.g.
{
std::vector<std::string> a;
std::string b;
MyClass c;
} //at this point, first c will be destroyed, then b, then all strings in a, then a.
If you get to pointers, you guessed correctly: Only the memory the pointer itself occupies (usually a 4 byte integer) will be automatically freed upon leaving scope. Nothing happens to the memory pointed to unless you explicitly delete
it (whether it's in a vector or not). If you have a class that contains pointers to other objects you may have to delete them in the destructor (depending on whether or not that class owns those objects). Note that in C++11 there are pointer classes (called smart pointers) that let you treat pointers in a similar fashion to 'normal' objects:
Ex:
{
std::unique_ptr<std::string> a = make_unique<std::string>("Hello World");
function_that_wants_string_ptr(a.get());
} //here a will call delete on it's internal string ptr and then be destroyed
If I have something like std::vector what happens when the vector destructor is called?
It depends.
If you have a vector of values std::vector <MyClass>
, then the destructor of the vector calls the destructor for every instance of MyClass
in the vector.
If you have a vector of pointers std::vector <MyClass*>
, then you're responsible for deleting the instances of MyClass
.
What happens if I have a pointer to another class inside a class
ClassB
instance would remain in memory. Possible ways to have ClassA
destructor to make the job for you are to make B
an instance member or a smart pointer.