[previous] MISRA-C:2004  Rule  8.7:  (Required) [next] Objects shall be defined at block scope if they are only accessed from within a single function.
The scope of objects shall be restricted to functions where possible. File scope shall only be used where objects need to have either internal or external linkage. Where objects are declared at file scope Rule applies. It is considered good practice to avoid making identifiers global except where necessary. Whether objects are declared at the outermost or innermost block is largely a matter of style. "Accessing" means using the identifier to read from, write to, or take the address of the object.

Example Code:


#include "misra.h"
#include "m2cmex.h"


static S16 s0807a;               /* MISRA Violation - Only referenced in function test_0807a   */
static S16 s0807b;               /* MISRA Violation - Only referenced in function test_0807b   */
extern S16 obj_0807c = 0;        /* MISRA Violation - Only referenced in function test_0807b   */

extern S16 test_0807a( void )
{
   s0807a += s16a;

   return s0807a;
}

extern S16 test_0807b( void )
{
   s0807b = s16b;
   obj_0807c = s16a;

   return s0807b + obj_0807c;
}


QAC messages that encompass this guideline:

1514 The object '%1s' is only referenced by function '%2s', in the translation unit where it is defined
3218 File scope static, '%s', is only accessed in one function.



(c) The Motor Industry Research Association, 2004
QA C Source Code Analyser 8.1.2
MISRA-C:2004 Compliance Module 3.2
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents