deleting new operator memory
Mar 20, 2019 at 2:16am UTC
Hi, I have a zybooks online assignment and I'm having trouble doing what it's asking me to do.
It wants the allocated memory in kitchenPaint deleted, but something I'm doing is wrong. I feel like my solution looks like the topic about deletion though, so I'm confused.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <iostream>
using namespace std;
class PaintContainer {
public :
~PaintContainer();
double gallonPaint;
};
PaintContainer::~PaintContainer() { // Covered in section on Destructors.
cout << "PaintContainer deallocated." << endl;
}
int main() {
PaintContainer* kitchenPaint;
kitchenPaint = new PaintContainer;
kitchenPaint->gallonPaint = 26.3;
//------------------
delete [] kitchenPaint; // my solution
/* Your solution goes here */
//------------------
return 0;
}
Mar 20, 2019 at 3:04am UTC
Use the non-array form of the delete expression:
1 2
// delete[] kitchenPaint; // my solution
delete kitchenPaint;
Mar 20, 2019 at 4:41am UTC
Good grief. I am certain that I read 'not all compilers require the brackets, but even those that don't allow for them'
Yet that was the solution.
It's frustrating.
Thank you though. I can at least move on now.
Mar 20, 2019 at 7:41am UTC
delete[] should be used when deleting an array .
delete (without [] ) should be used when deleting an object that is not an array.
Last edited on Mar 20, 2019 at 7:41am UTC
Topic archived. No new replies allowed.