AUTOSAR C++14 Rule A18-5-1
Functions malloc, calloc, realloc and free shall not be used
Description
Rule Definition
Functions malloc, calloc, realloc and free shall not be used.
Rationale
C-style memory allocation and deallocation using malloc
,
calloc
, realloc
, or free
is not
type safe and does not invoke class's constructors/destructor to create/delete
objects.
For instance, malloc
allocates memory to an object and returns a
pointer to the allocated memory of type void*
. A program can then
implicitly cast the returned pointer to a different type that might not match the intended
type of the object.
The use of these allocation and deallocation functions can result in undefined behavior if:
You use
free
to deallocate memory allocated with operatornew
.You use operator
delete
to deallocate memory allocated withmalloc
,calloc
, orrealloc
.
The rule is not violated when you perform dynamic memory allocation or
deallocation using overloaded new
and delete
operators, or custom implementations of malloc
and
free
.
Troubleshooting
If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: 18 Language Support Library |
Category: Required, Automated |
Version History
Introduced in R2019b