Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.STDLIB.MEMORY

Use of dynamic heap memory allocation.

MISRA C 2012 Rule 21.3: The memory allocation and deallocation functions of <stdlib.h> shall not be used

C90 [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

Amplification

The identifiers calloc, malloc, realloc and free shall not be used and no macro with one of these names shall be expanded.

Rationale

Use of dynamic memory allocation and deallocation routines provided by The Standard Library can lead to undefined behaviour, for example:
  • Memory that was not dynamically allocated is subsequently freed;
  • A pointer to freed memory is used in any way;
  • Accessing allocated memory before storing a value into it.
Note: this rule is a specific instance of Dir 4.12.

See also

Dir 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.

Rationale

The 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.

Example

void f1 ( )
{
   int32_t * i = new int32_t; // Non-compliant

   delete i;
}