Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 8.5: Declarations vs Redeclarations in MISRA C versus MISRA C++
#1
MISAR C++ 2008 provides a Glossary entry for 'declaration' with:

Quote: 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:

Quote:... 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?
<t></t>
Reply
#2
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:
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
Reply
#3
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:
/* 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.
<t></t>
Reply
#4
On a slightly related topic, the following 'contrived' example was highlighted to me:

Code:
/* 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?
<t></t>
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)