MISRA Discussion Forums
8.7 wording? - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17)
+---- Forum: 6.8 Declarations and Definitions (https://forum.misra.org.uk/forumdisplay.php?fid=35)
+---- Thread: 8.7 wording? (/showthread.php?tid=709)



8.7 wording? - gs - 25-02-2010

Am I correct in presuming "Objects shall be defined at block scope if they are only accessed from within a single function," should read declared instead of defined?


Re: 8.7 wording? - jbezem - 02-03-2010

I don't think so.
If an object is only accessed from within a single function, the issue is to "pull" the definition into that function. It avoids having both a declaration and a definition with possible differences between the two, initialization issues, scoping issues, etc. And since it isn't used anywhere else, there's no reason not to define the object at block scope.
Just my 5 cents, of course.

FWIW,

Johan


Re: 8.7 wording? - gs - 04-03-2010

However, if the object is not used outside of that function, does a reason exist to declare the object outside the body of said function?


Re: 8.7 wording? - jbezem - 08-03-2010

No, it does not. But the rule doesn't say either.
The rule doesn't state that a declaration outside said function would be required or even tolerated.
If you use an object just within the scope of one function, define it within the scope of that function, and don't mention it anywhere else.

AFAICT, the rule 8.7 doesn't contradict any of this.

Bests,

Johan


Re: 8.7 wording? - misra-c - 10-03-2010

In practice, the choice of word doesn't matter, as there will only be a single combined defining declaration

Should a declaration be made at a wider scope, this has the effect of creating a seperate unused identifier, of wider scope - not a reference to the inner scope. This is (probably) not what the programmer intended, and would trigger a violation of Rule 5.2.