Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
8.7 wording?
#1
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?
Reply
#2
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
<r>Johan Bezem<br/>
Email: <EMAIL email="[email protected]">[email protected]</EMAIL><br/>
Tel: +49 172 5463210<br/>
Web: <URL url="http://www.bezem.de/">http://www.bezem.de/</URL></r>
Reply
#3
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?
Reply
#4
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
<r>Johan Bezem<br/>
Email: <EMAIL email="[email protected]">[email protected]</EMAIL><br/>
Tel: +49 172 5463210<br/>
Web: <URL url="http://www.bezem.de/">http://www.bezem.de/</URL></r>
Reply
#5
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.
Posted by and on behalf of the MISRA C Working Group
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)