Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I delete a class, are its member variables automatically deleted?

I have been researching, and nothing relevant has come up, so I came here.

I am trying to avoid memory leaks, so I am wondering:

Say I have class MyClass with member ints a and b, and an int array c, which are filled in a member function:

class MyClass {     public:         int a, b;         int c[2];         void setVariables()          {             a, b = 0;             for (int i = 0; i < 2; i++)              {                 c[i] = 3;             }         } }; int main(int argc, char* argv[]) {     MyClass* mc = new MyClass();     mc->setVariables();     delete mc; }  

Now, after I call delete mc, will a, b, and all the contents of c be deleted as well? Or will I have to do that explicitly in the destructor of MyClass?

like image 995
Keelx Avatar asked Jun 06 '11 18:06

Keelx


People also ask

Do classes contain member variables?

A class is a blueprint that has member variables and functions in C#. This describes the behavior of an object. Member variables are the attributes of an object (from design perspective) and they are kept private to implement encapsulation.

What happens when you use Delete in C++?

When delete is used to deallocate memory for a C++ class object, the object's destructor is called before the object's memory is deallocated (if the object has a destructor). If the operand to the delete operator is a modifiable l-value, its value is undefined after the object is deleted.

Can a class have public member variables?

Public membersPublic member variables and functions are accessible outside of the class. For example, you can change the values of variables through statements or functions outside of the class and you can call functions within the class from statements or other functions outside of the class.

Can a class have itself as a member?

No, because the object would be infinitely large (because every Node has as members two other Node objects, which each have as members two other Node objects, which each... well, you get the point).


2 Answers

The rule is very simple: every object created with new must be destroyed exactly once with delete; every array created with new[] must be destroyed exactly once with delete[]; everything else must not be deleted. So your code is correct; you are deleting mc after creating it with new, and not deleting the members which were not created with new.

Applying the rule can be quite tricky when the program flow gets complicated (especially when exceptions are involved); for that reason, it is much better not to delete objects yourself, but to immediately use the result of new to initialise a smart pointer to manage the object for you.

like image 193
Mike Seymour Avatar answered Sep 18 '22 17:09

Mike Seymour


When delete mc is executed, the compiler calls the destructor of the object (MyClass::~MyClass()) and then deallocates the memory associated with it.

The default destructor (when you don't declare your own) calls the destructors of all member variables, in order from last to first by declaration (that is, in this case, c, then b, then a). Since those members in this example are POD types (they do not have a destructor), no work is done.

like image 41
greyfade Avatar answered Sep 19 '22 17:09

greyfade