I have a vector of structs declared in a class. My question is do I have to explicitly free the memory allocated by the vector in the class destructor or is that done automatically when I destroy the instance. As in, do I have to include code in the class destructor to free the memory of the vector.
-
What vector are you using? – TieDad Jun 24 '13 at 01:07
-
1No. `std::vector` destructs its contents when its lifetime has ended. – congusbongus Jun 24 '13 at 01:09
3 Answers
If you don't have a destructor and the vector is the class member it will do implicite by the default destructor which is generated by compiler.
The default destructor calls the destructors of the base class and members of the derived class.
The destructors of base classes and members are called in the reverse order of the completion of their constructor:
The destructor for a class object is called before destructors for members and bases are called. Destructors for nonstatic members are called before destructors for base classes are called. Destructors for nonvirtual base classes are called before destructors for virtual base classes are called.
And about the destructor you provide by your own:
A class's destructor (whether or not you explicitly define one) automagically invokes the destructors for member objects. They are destroyed in the reverse order they appear within the declaration for the class.
-
-
1"If you don't have a destructor " is in fact not a precondition for the destruction of non-static class members since their destruction will take place after the destructor body has been executed. – Pixelchemist Jun 24 '13 at 02:24
If you are allocating the structs yourself using new
then you need to deallocate them using delete
when you are finished with them. std::vector
won't do that for you.
Item 13 in the book Effective C++ (highly recommended) says to use objects to manage resources. This helps you avoid problems like having to worry about your resource de-allocation code not getting called because, for example, an exception was thrown. If you are using C++11 you can look into shared_ptr or just create your own resource management class.
Update: As jogojapan pointed out, std::auto_ptr is unsuitable for STL containers.

- 8,830
- 3
- 19
- 19
-
1[Why is it wrong to use auto_ptr with standard containers?](http://stackoverflow.com/questions/111478/why-is-it-wrong-to-use-stdauto-ptr-with-standard-containers) – jogojapan Jun 24 '13 at 01:53
-
1Ahh.. thanks @jogojapan I always forget about that.. I'll update my answer – sjs Jun 24 '13 at 01:54
-
2If you allocate stuff using `new` you must free them using `delete`. `free` is for allocations using `malloc`. – Pixelchemist Jun 24 '13 at 02:22
-
Thanks @Pixelchemist - I've been spending too much time in plain C lately! – sjs Jun 24 '13 at 02:25
Regardless of whether you have your own destructor or not: Every member of your class will have its destructor called (remember: if the member is a pointer and you allocated memory manually you need to free the allocated memory in the destructor body since the automatic destruction will destroy the pointer but not deallocate memory it points to).
The C++ Standard says that after the destructor body all non-variant non-static data members will have their destructors called.

- 24,090
- 7
- 47
- 71