deleting new operator memory

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;
}
Use the non-array form of the delete expression:
1
2
// delete[] kitchenPaint; // my solution
delete kitchenPaint;
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.
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
Topic archived. No new replies allowed.