MISRA.STDLIB.MEMORYUse of dynamic heap memory allocation. MISRA C 2012 Rule 21.3: The memory allocation and deallocation functions of <stdlib.h> shall not be usedC90 [Unspecified 19; Undefined 9, 91, 92; Implementation 69] C99 [Unspecified 39, 40; Undefined 8, 9, 168–171; Implementation J.3.12(35)] Category: Required Analysis: Decidable, Single Translation Unit Applies to: C90, C99 AmplificationThe identifiers calloc, malloc, realloc and free shall not be used and no macro with one of these names shall be expanded. RationaleUse of dynamic memory allocation and deallocation routines provided by The Standard Library can lead to undefined behaviour, for example:
See alsoDir 4.12, Rule 18.7, Rule 22.1, Rule 22.2 MISRA-C 2004 Rule 20.4 (required): Dynamic heap memory allocation shall not be used.[Unspecified 19; Undefined 91, 92; Implementation 69; Koenig 32] This precludes the use of the functions calloc, malloc, realloc and free. There is a whole range of unspecified, undefined and implementation-defined behaviour associated with dynamic memory allocation, as well as a number of other potential pitfalls. Dynamic heap memory allocation may lead to memory leaks, data inconsistency, memory exhaustion, nondeterministic behaviour. Note that some implementations may use dynamic heap memory allocation to implement other functions (for example functions in the library string.h). If this is the case then these functions shall also be avoided. MISRA-C++ 2008 Rule 18-4-1 (required): Dynamic heap memory allocation shall not be used.RationaleThe use of dynamic memory can lead to out-of-storage run-time failures, which are undesirable. The built-in new and delete operators, other than the placement versions, use dynamic heap memory. The functions calloc, malloc, realloc and free also use dynamic heap memory. There is a range of unspecified, undefined and implementation-defined behaviour associated with dynamic memory allocation, as well as a number of other potential pitfalls. Dynamic heap memory allocation may lead to memory leaks, data inconsistency, memory exhaustion, non-deterministic behaviour, etc. Note that some implementations may use dynamic heap memory allocation to implement other functions (for example, functions in the library cstring). If this is the case, then these functions shall also be avoided. Examplevoid f1 ( ) { int32_t * i = new int32_t; // Non-compliant delete i; } |