Do I need to destroy every instance of a new class?

In fact, if I create several instances of a new class, I need to call a destructor for each instance or call it after destroying each instance (I apologize if I use indefinite / incorrect terms, constructors / destructors are concepts that I still do not fully understand).

To be more specific, this is some kind of code that I work with (I will have to apologize again if the style is bad, I had an idea for a school problem and I want to quickly get the code).

while(read >> f >> l >> t >> s >> sta >> acct >> bal) { cout << "test" << endl; ptr[i] = new account(f,l,t,s,sta,acct,bal); ptr[i]->printcontents(); cout << "test" << endl; i++; cout << i << endl; } 

So, for the sake of the question, suppose it will be a cycle three times. Do I need to call the "account" destructor only once to destroy all three instances of the new account, or will one call leave the other two? Is this even good practice?

Edit: I noticed that part of my post was disabled, so I added the last few lines, but people have already addressed this issue. The reason I am a pointer to a user is due solely to the fact that the task dictates that I do this; frankly, I see no reason to use them at the moment, but I assume that somewhere along the line they become useful. I should also add that dynamic memory allocation is also supposed to be used in assignment.

+5
source share
5 answers

Destructors are called automatically, and you usually don't need to worry about it if you dynamically allocate memory with new .

In this case, you will have to call delete for each allocated memory if you no longer need it.

Note that if you allocate an array using new [] , you need to use delete[] to allocate:

 int *a = new int[10]; // allocating an array of 10 integers //... delete[] a; // release memory 

In modern C ++, you should consider managed pointers that will do the job for you. Sort of:

 while(read >> f >> l >> t >> s >> sta >> acct >> bal) { cout << "test" << endl; ptr[i] = std::make_unique<account>(f,l,t,s,sta,acct,bal); ptr[i]->printcontents(); cout << "test" << endl; i++; cout << i << endl; } 

Here std::make_unique will return std::unique_ptr , which will cause deletion in the associated memory upon destruction.


Last question: are you sure you really need pointers? It's hard to say from your example, but depending on your use, you can also create statically allocated objects:

 while(read >> f >> l >> t >> s >> sta >> acct >> bal) { cout << "test" << endl; ptr[i] = account(f,l,t,s,sta,acct,bal); ptr[i].printcontents(); cout << "test" << endl; i++; cout << i << endl; } 
+5
source

Each new must be balanced with delete

If you have an array of pointers and new each pointer, you need to delete each instance.

If, on the other hand, you are a new array of objects, you can delete [] entire array.

As a side item for your code, instead of using ptr (which you did not tell us about, consider just using std::vector<account> and using push_back , then it will automatically change as needed, instead of ptr[i] = new...

+3
source

Typically, you need to delete each instance created. Exactly how you do it, it depends a bit ... but a simple rule of thumb:

For each use of new , the use of delete must be used.

For each use of new[] , the use of delete[] should be used.

If you get an instance from an automatic declaration, you donโ€™t have to do anything because it will be destroyed with its stack frame; but if you created it, you destroy it.

+1
source

As others have noted, if you use new , you will need delete . However, your real problem is conceptual. You are asking a good question for which new and delete are peripheral.

The usual use of destructors, over 99.5% of the time, should allow the compiler to automatically call them. (The exception is 0.5%, and we do not need to worry about it right now.)

Consider this piece of code:

  int n = 5; { int m = 2; ++m; n *= m; // The variable m is automatically destructed here. } // Here, n == 15. 

See how it works? The m destructor is automatically called when m reaches the end of its life, which (because m not done with new ) happens at the end of the block in which m is created.

Of course, m is int , so its destructor does nothing (or, if you want, m does not have a destructor). Other types that you define yourself, such as your account type, may have destructors. The destructor is automatically called against one object at a time when this single object, an instance of account , reaches the end of its life.

NEW & DELETE

So what does this have to do with new and delete ?

In my code snippet, I did not use new . Therefore, the lifetime of my object was controlled by the attached braces.

In the code snippet, you used new . Therefore, the lifetime of your object is controlled by your use of delete .

Of course, you forgot to use delete , so your object has never been destroyed (except, perhaps, when your entire program ends).

Others have noted that modern C ++ provides improved new alternatives that you should use in most cases. You can read their answers for them. However, mechanically, itโ€™s still very useful to understand what ordinary new does and how delete combines with it.

DESTROYERS MANAGEMENT GUIDE

You almost certainly should not worry about this, because cases where the destructor is started manually are rare and advanced. Basically, if you control the memory in which the object is stored (which you almost never do, but there are rare exceptions), then the compiler cannot say when to destroy, so you should say it when. See this for a typical esoteric example.

+1
source

Only pointers require a call to the destructor to remove them or close the program

0
source

Source: https://habr.com/ru/post/1261339/


All Articles