The delete Operator With Arrays

From RAD Studio
Jump to: navigation, search

Go Up to The new And delete Operators Index

Arrays are deleted by operator delete[](). You must use the syntax

delete [] expression 

when deleting an array.

char * p;
void func()
{
   p = new char[10];    //  allocate 10 chars
   delete[] p;          // delete 10 chars
}

Early C++ compilers required the array size to be named in the delete expression. In order to handle legacy code, the compiler issues a warning and simply ignores any size that is specified. For example, if the preceding example reads delete[10] p and is compiled, the warning is as follows:

Warning: Array size for delete ignored in function func()

See Also