MISRA Discussion Forums

Full Version: 8.7 wording?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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
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?
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
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.