The difference between delete and delete[] in C++

SIMEL picture SIMEL · Jan 12, 2011 · Viewed 29.6k times · Source

Possible Duplicate:
delete vs delete[] operators in C++

I've written a class that contains two pointers, one is char* color_ and one in vertexesset* vertex_ where vertexesset is a class I created. In the destractor I've written at start

delete [] color_;
delete [] vertex_;

When It came to the destructor it gave me a segmentation fault.

Then I changed the destructor to:

delete [] color_;
delete vertex_;

And now it works fine. What is the difference between the two?

Answer

etarion picture etarion · Jan 12, 2011

You delete [] when you newed an array type, and delete when you didn't. Examples:

typedef int int_array[10];

int* a = new int;
int* b = new int[10];
int* c = new int_array;

delete a;
delete[] b;
delete[] c; // this is a must! even if the new-line didn't use [].