delete[] on object arrays

I had a question about delete[].

If you had an array of objects, say something like this (I know i'm using some classes and haven't provided their source code, but I think you'll get the idea):

1
2
3
4
5
6
7
8
int main()
{
     Shape b** = new Shape*[2];
     b[0] = new Rectangle(2,1);
     b[1] = new Circle(3);
     delete[] b;
     // ... other code after
}


Would the memory for the Rectangle and Circle be de-allocated? Or must I do something like this:

1
2
3
4
5
6
7
8
9
10
int main()
{
     Shape b*[] = new Shape*[2];
     b[0] = new Rectangle(2,1);
     b[1] = new Circle(3);
     delete b[0];
     delete b[1];
     delete[] b;
     // ... other code after
}
Last edited on
You have to delete all b's elements
If you call delete[] on a 2d array you'd have to deallocate each array in your array and then deallocate the whole array thereafter. See the article on multidimensional arrays.
the compiler is a good teacher.
Topic archived. No new replies allowed.