Essentially, if I create multiple instances of a new class, do I need to call the destructor for each instance, or will invoking it once destroy each instance(I apologize if I'm using vague/wrong terms, constructors/destructors are concepts I don't fully grasp yet).
To be more specific, this is some code I'm working with(I'll have to apologize again if the style is bad, I had an idea for a school problem and wanted to get the code down quickly).
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, assume this'll loop three times. Will I only have to invoke the destructor of "account" once to destroy all three instances of new account, or will one call leave the other two? Is this even a good practice?
Edit: I noticed some of my post got cut off, so I added the last few lines, but people have already addressed that issue. The reason I'm user pointers is purely because the assignment dictates I do so; quite frankly I don't see the point in using them at this moment, but I assume somewhere down the line they become useful. I should also add that dynamic memory allocation is also supposed to be used in the assignment.
Destructors are called automatically and you usually don't have to worry about it unless you're allocating memory dynamically with new
.
In that case, you will have to call delete
for each allocated memory once you don't need it any more.
Note that if you're allocating an array with new []
, you will have to use delete[]
for desallocation:
int *a = new int[10]; // allocating an array of 10 integers
//...
delete[] a; // release memory
In modern C++, you should consider managed pointers which will do the job for you. Something like:
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 a std::unique_ptr
which will call delete on the associated memory when destroyed.
Last point : are you sure you really need pointers? Hard to say from your example, but depending on your usage you may as well 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;
}
Every new
should be balanaced with a delete
If you have an array of pointers, and new
each pointer, you'd need to delete
each instance.
If, on the otherhand you new
an array of objects, you can then delete []
the whole array.
As a side point, for your code instead of using the ptr
(which you haven't told us the details of, consider just using a std::vector<account>
and using push_back
then it will automatically size as required, instead of ptr[i] = new...
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With