8.7 wording?

6.8 Declarations and Definitions

Moderators: david ward, misra-c

8.7 wording?

Postby gs » Thu Feb 25, 2010 8: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?
gs
 
Posts: 51
Joined: Thu Nov 18, 2004 2:39 am

Re: 8.7 wording?

Postby jbezem » Tue Mar 02, 2010 11: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: j.bezem@computer.org
Tel: +49 172 5463210
Web: http://www.bezem.de/
jbezem
 
Posts: 20
Joined: Mon Aug 01, 2005 8:24 pm
Location: Germany

Re: 8.7 wording?

Postby gs » Thu Mar 04, 2010 4: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?
gs
 
Posts: 51
Joined: Thu Nov 18, 2004 2:39 am

Re: 8.7 wording?

Postby jbezem » Mon Mar 08, 2010 9: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: j.bezem@computer.org
Tel: +49 172 5463210
Web: http://www.bezem.de/
jbezem
 
Posts: 20
Joined: Mon Aug 01, 2005 8:24 pm
Location: Germany

Re: 8.7 wording?

Postby misra-c » Wed Mar 10, 2010 11: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.
MISRA C Committee
misra-c
 
Posts: 159
Joined: Thu Jan 05, 2006 2:11 pm


Return to 6.8 Declarations and Definitions

Who is online

Users browsing this forum: No registered users and 1 guest

cron