...
Do not call a deallocation function on anything other than nullptr, or a pointer returned by the corresponding allocation function described by:
| Allocator | Deallocator |
|---|---|
global operator new()/new | global operator delete()/delete |
global operator new[]()/new[] | global operator delete[]()/delete[] |
class-specific operator new()/new | class-specific operator delete()/delete |
class-specific operator new[]()/new[] | class-specific operator delete[]()/delete[] |
placement operator new() | N/A |
allocator<T>::allocate() |
|
std::malloc(), std::calloc(),std::realloc() | std::free() |
std::get_temporary_buffer() | std::return_temporary_buffer() |
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <cstdlib>
void f() {
int *i = static_cast<int *>(std::malloc(sizeof(int)));
// ...
delete i;
}
|
Additionally, this code violates MEM08-CPP. Use new and delete rather than raw memory allocation and deallocation. However, it This code does not violate MEM53-CPP. Explicitly construct and destruct objects when manually managing object lifetime because it complies with the MEM53-CPP-EX1 exception.
...