Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Destructors with Vectors, Pointers,

Tags:

c++

destructor

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 and std::strings: 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 of myClass? 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?

like image 702
ISTB Avatar asked Aug 22 '12 08:08

ISTB


People also ask

Do you need a destructor for vectors?

Yes. std::vector and std::string are automatically when they finish out of scope, calling also the destructor of the objects contained (for std::vector ).

Does vector have a destructor C++?

One way of deleting a vector is to use the destructor of the vector. In this case, all the elements are deleted, but the name of the vector is not deleted. The second way to delete a vector is just to let it go out of scope. Normally, any non-static object declared in a scope dies when it goes out of scope.

Do pointers have destructors?

What would the pointer's destructor do? Nothing. Only class types have destructors.

Does vector erase call destructor?

Yes. vector::erase destroys the removed object, which involves calling its destructor.


2 Answers

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 deleted (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 deleted.

What happens if I have a pointer to another class inside a class

The B must be explicitly deleted. Again, a smart pointer could be used to handle the destruction of B.

like image 66
hmjd Avatar answered Sep 22 '22 06:09

hmjd


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;}; } 
like image 42
Alejandro Alcalde Avatar answered Sep 21 '22 06:09

Alejandro Alcalde