Incorrect pairing of memory allocation and deallocation

suggest change

An object can only be deallocated by delete if it was allocated by new and is not an array. A null pointer may be passed to delete. Otherwise, if the argument 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[]. A null pointer may be passed to delete[]. Otherwise, if the argument was not returned by new[] or is not an array, the behavior is undefined.

free does nothing when given a null pointer. Otherwise, if the argument was not returned by malloc, calloc, realloc, or aligned_alloc, or if it has already been freed, 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[].

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents