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. 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[].

Feedback about page:

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


Undefined behavior:
* Incorrect pairing of memory allocation and deallocation

Table Of Contents
8 Arrays
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
71 Undefined behavior
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype