An object can only be deallocated by delete
if it was allocated by new
and is not an array. If the argument to delete
was not returned by new
or is an array, the behavior is undefined.
An object can only be deallocated by delete[]
if it was allocated by new
and is an array. If the argument to delete[]
was not returned by new
or is not an array, the behavior is undefined.
If the argument to free
was not returned by malloc
, the behavior is undefined.
int* p1 = new int;
delete p1; // correct
// delete[] p1; // undefined
// free(p1); // undefined
int* p2 = new int[10];
delete[] p2; // correct
// delete p2; // undefined
// free(p2); // undefined
int* p3 = static_cast<int*>(malloc(sizeof(int)));
free(p3); // correct
// delete p3; // undefined
// delete[] p3; // undefined
Such issues can be avoided by completely avoiding malloc
and free
in C++ programs, preferring the standard library smart pointers over raw new
and delete
, and preferring std::vector
and std::string
over raw new
and delete[]
.