AUTOSAR C++14 Rule A18-5-8
Objects that do not outlive a function shall have automatic storage duration
Since R2021b
Description
Rule Definition
Objects that do not outlive a function shall have automatic storage duration.
Rationale
A dynamically allocated object results in additional allocation and deallocation costs and makes your program vulnerable to memory leaks if, for instance, the program returns due to an exception throw before the deallocation operation.
Instead, use an object with automatic storage duration, which has a lifetime that is bound to the enclosing scope of that object. The object is automatically destroyed when that scope exits.
The rule allows an exception for local objects that are dynamically allocated to optimize stack memory usage because the objects use a large amount of memory and might otherwise cause a stack overflow.
Polyspace Implementation
Polyspace® flags objects that are created in a function scope and that do not have automatic storage duration when any of the following is true:
The object is a smart pointer (
std::shared_ptr
orstd::unique_ptr
) that is never copied, moved, reassigned, reset, or passed to a callee.The object is not flagged if it is a non-array and, at compilation time, its size is greater than 4 KB or its size is unknown.
The object is dynamically allocated by using operators
new
ornew[]
and then deallocated through all possible paths within the function.The object is not flagged if it is a non-array and, at compilation time, its size is greater than 4 KB or its size is unknown.
The object is a wrapper class that contains at least one data member with a fixed size larger than 16 KB.
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: Language support library |
Category: Required, Partially automated |
Version History
Introduced in R2021b