Rule A18-5-1 (required, implementation, automated)

Functions malloc, calloc, realloc and free shall not be used.

Rationale

C-style allocation/deallocation using malloc/calloc/realloc/free functions is not type safe and does not invoke class’s constructors and destructors. Note that invoking free function on a pointer allocated with new, as well as invoking delete on a pointer allocated with malloc/realloc/calloc function, result in undefined behavior. Also, note that realloc function should only be used on memory allocated via malloc or calloc functions.

Exception

This rule does not apply to dynamic memory allocation/deallocation performed in user-defined overloads of new and delete operators or malloc and free functions custom implementations.

Example

// $Id: A18-5-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
#include <cstdlib>
void F1() noexcept(false)
{
// Non-compliant
std::int32_t* p1 = static_cast<std::int32_t*>(malloc(sizeof(std::int32_t)));

*p1 = 0;

// Compliant
std::int32_t* p2 = new std::int32_t(0);

// Compliant
delete p2;

// Non-compliant
free(p1);

// Non-compliant
std::int32_t* array1 =
static_cast<std::int32_t*>(calloc(10, sizeof(std::int32_t)));

// Non-compliant
std::int32_t* array2 =
static_cast<std::int32_t*>(realloc(array1, 10 * sizeof(std::int32_t)));

// Compliant
std::int32_t* array3 = new std::int32_t[10];

// Compliant
delete[] array3;

// Non-compliant
free(array2);

// Non-compliant
free(array1);
}
void F2() noexcept(false)
{
// Non-compliant
std::int32_t* p1 = static_cast<std::int32_t*>(malloc(sizeof(std::int32_t)));

// Non-compliant - undefined behavior
delete p1;

std::int32_t* p2 = new std::int32_t(0); // Compliant

free(p2); // Non-compliant - undefined behavior
}
void operator delete(void* ptr) noexcept

{
std::free(ptr); // Compliant by exception
}

See also

HIC++ v4.0 [9]: 5.3.2 Allocate memory using new and release it using delete. C++ Core Guidelines [11]: R.10: Avoid malloc() and free().