|
|
[16.12] Can I drop the [] when deleting array of some built-in type
(char, int, etc)?
No!
Sometimes programmers think that the [] in the delete[] p only exists so
the compiler will call the appropriate destructors for all elements in the
array. Because of this reasoning, they assume that an arrayof some built-in
type such as char or int can be deleted without the []. E.g., they assume the
following is valid code:
void userCode(int n)
{
char* p = new char[n];
// ...
delete p;// <-- ERROR! Should be delete[] p !
}
|
|