[previous] 4140 [next] Address of automatic object exported in function return value.
Pointers REFERENCE - ISO:C90-6.1.2.4 Storage Durations of Objects

The address of an automatic object is being used as the return expression of a function. This is dangerous. On return from this function, the object will go out of scope and the value obtained by dereferencing the pointer will be undefined. Perhaps the object needs to be declared static ?

For example:


/*PRQA S 2017,3408,3447 ++*/

int *func ( void )
{
    int a = 0;

    return &a;                   /* Message 4140 */
}


MISRA-C:2004 Rules applicable to message 4140:

Rule  17.6  (Required) 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.


See also:

QA·C Source Code Analyser 8.1
MISRA-C:2004 Compliance Module 3.2
© 2012 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents