8.7 wording?

6.8 Declarations and Definitions

Moderators: misra-c, david ward

Post Reply
gs
Posts: 87
Joined: Thu Nov 18, 2004 1:39 am

8.7 wording?

Post by gs » Thu Feb 25, 2010 7:47 pm

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?

jbezem
Posts: 20
Joined: Mon Aug 01, 2005 8:24 pm
Company: JB Enterprises
Location: Germany
Contact:

Re: 8.7 wording?

Post by jbezem » Tue Mar 02, 2010 10:52 am

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
Johan Bezem
Email: [email protected]
Tel: +49 172 5463210
Web: http://www.bezem.de/

gs
Posts: 87
Joined: Thu Nov 18, 2004 1:39 am

Re: 8.7 wording?

Post by gs » Thu Mar 04, 2010 3:53 pm

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?

jbezem
Posts: 20
Joined: Mon Aug 01, 2005 8:24 pm
Company: JB Enterprises
Location: Germany
Contact:

Re: 8.7 wording?

Post by jbezem » Mon Mar 08, 2010 8:24 pm

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
Johan Bezem
Email: [email protected]
Tel: +49 172 5463210
Web: http://www.bezem.de/

misra-c
Posts: 556
Joined: Thu Jan 05, 2006 1:11 pm

Re: 8.7 wording?

Post by misra-c » Wed Mar 10, 2010 10:50 am

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

Post Reply

Return to “6.8 Declarations and Definitions”