Why is this an issue?
If the address of an automatic object is assigned to another automatic object of larger scope, or to a static object, or returned from a function
then the object containing the address may exist beyond the time when the original object ceases to exist (and its address becomes invalid).
Noncompliant code example
int* f(void) {
int local_auto;
return &local_auto; // Noncompliant, returning address of an object allocated on the stack.
}
Resources
- MISRA C:2004, 17.6
- MISRA C++:2008, 7-5-2
- MISRA C:2012, 18.6
- CERT, DCL30-C. - Declare objects with appropriate storage durations