Destructor and deleting memory

Apr 3, 2020 at 12:11am
What is the best practice associated with memory allocation?

If I add a delete function to my destructor to a class with memory allocated pointers, when would the destructor actually initiate and delete the memory?

For example I have something like:

1
2
3
4
5
6
7
8
9
class A {

public:
int *p = new int [10]

A(){p[0]=1;p[1]=2;}
~A(){ delete [] p; }

};


But, the variables in class A are inherited to other classes. How can I make sure to delete the memory after all classes have used the variables?
Last edited on Apr 3, 2020 at 12:11am
Apr 3, 2020 at 12:20am
The destructor of class A will be called when an object of a class which inherits from A is getting murdered.

Just make sure if you've dynamically allocated the object itself that you delete it.

You can add statements like this to make sure your destructors are being called:

std::cout << "Destructor for Class A Called!\n";
Apr 3, 2020 at 12:30am
virtual ~A() { delete [] p; }
Apr 3, 2020 at 1:17am
@mbozzi maybe I'm missing something but what would the point of that be if there are no virtual functions shown in the class?
Apr 3, 2020 at 5:25am
virtual functions aren't required for Liskov substitution.

That is, it's still possible to treat a derived as a base and run into problems, even if this is less likely when other virtual functions aren't involved.

In any event, OP's class is contrived, and I figured a reminder was in order. Personally I forget to declare my destructors virtual way too often.
Last edited on Apr 13, 2020 at 12:28am
Apr 3, 2020 at 12:59pm
What is the best practice associated with memory allocation?
Decide who owns the memory (who deletes it). Document that in your comments and code to that standard. If you do this then memory management isn't hard.

You can also just use unique_ptr<> and shared_ptr<>
Apr 3, 2020 at 1:31pm
..or a vector<int>, in this case
Apr 3, 2020 at 2:05pm
the variables in class A are inherited to other classes. How can I make sure to delete the memory after all classes have used the variables?

Is this a question about resource management or about inheritance?

Every constructor has (pseudo):
1
2
3
4
5
6
id ( args )
 : constructors of bases,
   constructors of members
{
  body
}

The constructor of base class completes before any of the members are initialized.
The body executes after all bases and members have initialized.

The destructor operates in reverse order:
1. Execute the body
2. Destruct members (in reverse)
3. Call destructor of base class


Therefore, the derived part of an object is no more by the time you reach the body of A::~A()
Apr 3, 2020 at 2:05pm
mbozzi wrote:
That is, it's still possible to treat a derived as a base and run into problems [by not having virtual destructors]
Oh, I think I see what you're saying now. I never thought about that.
1
2
    Base* base = new Derived;
    delete base;

Will only call the Base destructor if Base's dtor is not virtual, but will call both the Derived and Base destructor (in that order) if it's virtual (which sounds obvious in hindsight, though, as you said, this is kinda weird to do if the Base class can't be used polymorphically for any other purpose anyway.)
Last edited on Apr 3, 2020 at 2:08pm
Topic archived. No new replies allowed.