As far as I know, I should destroy in destructors everything I created with new
and close opened filestreams and other streams.
However, I have some doubts about other objects in C++:
std::vector
andstd::string
s: Are they destroyed automatically?If I have something like
std::vector<myClass*>
of pointers to classes. What happens when the vector destructor is called?
Would it call automatically the destructor ofmyClass
? Or only the vector is destroyed but all the Objects it contains are still existant in the memory?What happens if I have a pointer to another class inside a class, say:
class A { ClassB* B; }
and Class A is destroyed at some point in the code. Will Class B be destroyed too or just the pointer and class B will be still existent somewhere in the memory?
Yes (assuming member variables are not pointers to
std::vector
andstd::string
).If
vector<MyClass>
then all objects contained in the vector will be destroyed. Ifvector<MyClass*>
then all objects must be explicitlydelete
d (assuming the class being destructed owns the objects in thevector
). A third alternative isvector
of smart pointers, likevector<shared_ptr<MyClass>>
, in which case the elements of thevector
do not need to be explictlydelete
d.The
B
must be explicitlydelete
d. Again, a smart pointer could be used to handle the destruction ofB
.std::vector
,std::string
and as far as I know all other STL containers have automatic destructors. This is the reason why it is often better to use these containers instead ofnew
anddelete
since you will prevent memory leaks.Your
myClass
destructor will only be called if your vector is a vector ofmyClass
objects (std::vector< myClass >
) instead of a vector of pointers tomyClass
objects (std::vector< myClass* >
).In the first case the destructor of
std::vector
will also call the destructor ofmyClass
for each of its elements; in the second case the destructor ofstd::vector
will call the destructor ofmyClass*
, which means it will free the space taken to store each pointer but will not free the space taken to store themyClass
objects themselves.The
Class B
objects you point to will not be destroyed, but the space assigned to store its pointer will be freed.As I understand it, this should work as follows:
Is that what you wanted?
Yes.
std::vector
andstd::string
are automatically when they finish out of scope, calling also the destructor of the objects contained (forstd::vector
).As said before,
std::vector
is destroyed when it finish out of scope, calling the destructor of the objects contained. But in fact, in this case, the objects contained are the pointers, not the object pointed by the pointers. So you have todelete
them manually.The same as (2). A will be destroyed and so the pointer, but not the class B pointed. You have to provide a destructor for A that
delete
B.In C++11 there is a very useful solution: use
std::unique_pointer
. Can be use only to point a single object and this will be deleted when the pointer goes out of scope (for example when you destroy yourstd::vector
).http://en.cppreference.com/w/cpp/memory/unique_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 eachnew/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
andstd::string
andMyClass
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.
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: