MISRA Discussion Forums
Rule 8.5: Declarations vs Redeclarations in MISRA C versus MISRA C++ - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA C:2012 and MISRA C:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=21)
+---- Forum: 8.8 Declarations and defnitions (https://forum.misra.org.uk/forumdisplay.php?fid=163)
+---- Thread: Rule 8.5: Declarations vs Redeclarations in MISRA C versus MISRA C++ (/showthread.php?tid=1321)



Rule 8.5: Declarations vs Redeclarations in MISRA C versus MISRA C++ - RichardC - 02-03-2017

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?


Re: Rule 8.5: Declarations vs Redeclarations in MISRA C versus MISRA C++ - misra-c - 30-03-2017

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.


Re: Rule 8.5: Declarations vs Redeclarations in MISRA C versus MISRA C++ - RichardC - 31-03-2017

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.


Re: Rule 8.5: Declarations vs Redeclarations in MISRA C versus MISRA C++ - RichardC - 06-04-2017

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?