Rule A18-5-7 (required, implementation, non-automated)

If non-realtime implementation of dynamic memory management functions is used in the project, then memory shall only be allocated and deallocated during non-realtime program phases.

Rationale

If worst-case execution time of memory management functions can not be determined, then dynamic memory usage is prohibited during realtime program phase, but it can be used e.g. during initialization or non-realtime state transitions. See: Real-time.

Example

//% $Id: A18-5-7.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
#include <memory>
#include <vector>
std::int8_t AppMainLoop() noexcept
{
std::int8_t retCode = 0;
std::int32_t* arr[10];

while (true)
{
for (std::int8_t i = 0; i < 10; ++i)
{
arr[i] = new std::int32_t{
i}; // Non-compliant - allocation in a phase that
// requires real-time
}
// Implementation
for (auto& i : arr)
{
delete i; // Non-compliant - deallocation in a phase that requires
// real-time
}
}
return retCode;
}
static std::int32_t* object =

new std::int32_t{0}; // Compliant- allocating in start-up phase

int main(int, char**)

{
std::unique_ptr<std::int32_t> ptr =
std::make_unique<std::int32_t>(0); // Compliant

std::vector<std::int32_t> vec;
vec.reserve(10);

// Compliant
// Compliant

std::int8_t code = AppMainLoop();
return code;
}

See also

none