[previous] MISRA-C:2004  Rule  20.4:  (Required) [next] Dynamic heap memory allocation shall not be used.
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, non-deterministic 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.

Example Code:


/* PRQA S 2983 ++ */

#include <stdlib.h>
#include "misra.h"
#include "m2cmex.h"

#define L 4U

extern S16 test_2004( void )
{
   S8 *p;

   p = ( S8 * )malloc( L );                /* MISRA Violation */
   free( p );                              /* MISRA Violation */

   p = ( S8 * )calloc( 10U, L );           /* MISRA Violation */
   p = ( S8 * )realloc( p, L );            /* MISRA Violation */

   return 0;
}



QAC messages that encompass this guideline:

5118 Dynamic heap memory allocation shall not be used.



(c) The Motor Industry Research Association, 2004
QA C Source Code Analyser 8.1.2
MISRA-C:2004 Compliance Module 3.2
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents