MISRA C++:2008 Rule 7-5-2
The address of an object with automatic storage shall not be assigned to another object that may persist after the first object has ceased to exist
Description
Rule Definition
The address of an object with automatic storage shall not be assigned to another object that may persist after the first object has ceased to exist.
Rationale
If an object continues to point to another object after the latter object ceases to exist, dereferencing the first object leads to undefined behavior.
Polyspace Implementation
Polyspace® reports a violation of this rule if you assign the address of an automatic storage object to an object with greater lifetime. Objects with greater lifetime include:
Global pointer variables
Local objects declared at an outer scope
The checker does not raise violations of this rule if a function returns the address of
a local variable. MISRA C++:2008 Rule
7-5-1
covers this situation.
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: Declarations |
Category: Required |
Version History
Introduced in R2013b