Click to See Complete Forum and Search --> : C++ Memory Management: What is the difference between 'delete' and 'delete[]'?


Yves M
February 13th, 2003, 01:05 PM
Q: What is the difference between 'delete' and 'delete[]'?

A: Whenever you allocate memory with 'new[]', you have to free the memory using 'delete[]'. When you allocate memory with 'new', then use 'delete' without the brackets. You use 'new[]' to allocate an array of values (always starting at the index 0).


int *pi = new int; // allocates a single integer
int *pi_array = new int[10]; // allocates an array of 10 integers
delete pi;
pi = 0;
delete [] pi_array;
pi_array = 0;

<br><br>