Rule 8.5: Declarations vs Redeclarations in MISRA C versus MISRA C++

Moderators: misra-c, david ward

Post Reply
RichardC
Posts: 7
Joined: Thu May 05, 2016 1:32 pm
Company: Programming Researrch

Rule 8.5: Declarations vs Redeclarations in MISRA C versus MISRA C++

Post by RichardC » Thu Mar 02, 2017 12:45 pm

MISAR C++ 2008 provides a Glossary entry for 'declaration' with:
For the purposes of this standard, in headline rule text a
declaration is the first introduction of a name into a translation
unit. All subsequent “declarations” (as per ISO/IEC 14882:2003 [1]
§3.2(1)) are re-declarations.
This appears to differ from the MISRA C 2012 interpretation. Rule 8.5
includes the text:
... shall be declared once in one and only one file.
According to the MISRA C++ glossary entry, the use of "once" in the headline
text is redundant, as any additional declarations are "re-declarations".

Is the MISRA C meaning intended to be different to that of MISRA C++ 2008?

If so, could you provide an example where it is dangerous to use re-declarations?

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

Re: Rule 8.5: Declarations vs Redeclarations in MISRA C versus MISRA C++

Post by misra-c » Thu Mar 30, 2017 10:51 am

The intent of the MISRA-C and MISRA-C++ rules is the same in this area, however C and C++ use different terminology.
C does not use the term "re-declaration" and "declarations" refers both the first and subsequent declarations.

The following is a C example where it is dangerous to have multiple declarations.

Code: Select all

File 1:
    int n;
File 2
    extern long n;
The linker may fail to notice the type mismatch.
---
Posted by and on behalf of
the MISRA C Working Group

RichardC
Posts: 7
Joined: Thu May 05, 2016 1:32 pm
Company: Programming Researrch

Re: Rule 8.5: Declarations vs Redeclarations in MISRA C versus MISRA C++

Post by RichardC » Fri Mar 31, 2017 1:06 pm

Thank you for your response, however, it appears that there has been a slight misunderstanding on what I was requesting.

MISRA C++ defines 'declaration' as the first introduction of a name in a translation unit and all subsequent 'declarations' in that translation unit are 'redeclarations'. Both MISRA versions catch the example of declarations in different translation units and I understand why that is a problem.

However, MISRA C goes further than MISRA C++, as at least my reading of the wording results in non-defining re-declarations as also being non-compliant:

Code: Select all

  /* file.h */
  extern int i;
  extern int i; // non-compliant
In my opinion, this is unnecessarily restrictive, hence, I am requesting an example that shows why multiple non-defining (re)declarations in the same translation unit should be avoided.

RichardC
Posts: 7
Joined: Thu May 05, 2016 1:32 pm
Company: Programming Researrch

Re: Rule 8.5: Declarations vs Redeclarations in MISRA C versus MISRA C++

Post by RichardC » Thu Apr 06, 2017 2:03 pm

On a slightly related topic, the following 'contrived' example was highlighted to me:

Code: Select all

/* t.c */

extern int i1;
int i1 = 0;

extern int i2;
int i2 = 0;

extern int i3;
int i3 = 0;

/*...*/
It's not clear to me that such declarations/definitions are non-compliant, maybe 4.5 or maybe 4.7?

Does the group have any suggestions?

Post Reply

Return to “8.8 Declarations and defnitions”