<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title><![CDATA[MISRA Discussion Forums - 6.16 Preprocessing directives (C++)]]></title>
		<link>https://forum.misra.org.uk/</link>
		<description><![CDATA[MISRA Discussion Forums - https://forum.misra.org.uk]]></description>
		<pubDate>Thu, 05 Nov 2026 20:03:07 +0000</pubDate>
		<generator>MyBB</generator>
		<item>
			<title><![CDATA[Initialisation of multiple constant arrays WITHOUT #define]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1577</link>
			<pubDate>Thu, 22 Jul 2021 14:22:58 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=601">misra cpp</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1577</guid>
			<description><![CDATA[Note this post was made by <span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Tahoma, Verdana, Arial, sans-serif;" class="mycode_font">jonesthechip, but was lost when the bulletin board was migrated to MISRA's new website. It's been resubmitted by  misra cpp</span></span></span><br />
<br />
<br />
Unread post by jonesthechip » Wed May 26, 2021 10:27 am<br />
<br />
A code base derived from an earlier C product has a large number of #define XYZ = {{1,2,3}} statements in a header.<br />
The string defined by XYZ is used to initialise part of a ROM constant array, as follows:-<br />
<br />
SpecialType ABC = {XYZ, 0, 1, pointer};<br />
<br />
'SpecialType' is a structure with a three byte array that gets loaded with the replacement data from XYZ, and other sundry components.<br />
<br />
This gets expanded to SpecialType ABC = {{{1,2,3}}, 0, 1, pointer};<br />
<br />
The header file groups a number of replacements strings defined as XYZ, PQR, UTC, etc to keep the data (access parameters for comms) in one 'logical' place. Some replacement strings are used once, some in several other files to initialise other structures.<br />
<br />
To achieve MISRA compliance means getting rid of all of these *&amp;!^%&#36; macros. (Or more deviations than you can shake a stick at... Not ideal!)<br />
<br />
However, it appears to be a classic case of "you don't really want to start from here"...<br />
<br />
The optimum fix would be to re-write the code to use a pointer to a fixed string and bin the macros, putting all of the three byte arrays into a set of constant structures and sharing pointers to individual elements. This would lead to an unholy amount of retesting (and possibly my head on a stake, to deter others).<br />
<br />
The least worst practical approach appears to be to bin the macros and edit the text replacement directly into all of the files, which although time-consuming does generate identical output binary files and therefore requires no re-testing. However, this does break the link between copies of the same data sets, so that a change to a particular set would require a search and edit through the code base.<br />
<br />
So, today's question is: any better suggestions, please?<br />
<br />
Regard<br />
<br />
Sid Jones<br />
<hr class="mycode_hr" />
You are right, this is currently non-compliant<br />
<br />
See MISRA compliance 2020 for how this sort of issue can be managed.<br />
<br />
The rule 16-2-2 is currently under review for the next issue]]></description>
			<content:encoded><![CDATA[Note this post was made by <span style="color: #333333;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Tahoma, Verdana, Arial, sans-serif;" class="mycode_font">jonesthechip, but was lost when the bulletin board was migrated to MISRA's new website. It's been resubmitted by  misra cpp</span></span></span><br />
<br />
<br />
Unread post by jonesthechip » Wed May 26, 2021 10:27 am<br />
<br />
A code base derived from an earlier C product has a large number of #define XYZ = {{1,2,3}} statements in a header.<br />
The string defined by XYZ is used to initialise part of a ROM constant array, as follows:-<br />
<br />
SpecialType ABC = {XYZ, 0, 1, pointer};<br />
<br />
'SpecialType' is a structure with a three byte array that gets loaded with the replacement data from XYZ, and other sundry components.<br />
<br />
This gets expanded to SpecialType ABC = {{{1,2,3}}, 0, 1, pointer};<br />
<br />
The header file groups a number of replacements strings defined as XYZ, PQR, UTC, etc to keep the data (access parameters for comms) in one 'logical' place. Some replacement strings are used once, some in several other files to initialise other structures.<br />
<br />
To achieve MISRA compliance means getting rid of all of these *&amp;!^%&#36; macros. (Or more deviations than you can shake a stick at... Not ideal!)<br />
<br />
However, it appears to be a classic case of "you don't really want to start from here"...<br />
<br />
The optimum fix would be to re-write the code to use a pointer to a fixed string and bin the macros, putting all of the three byte arrays into a set of constant structures and sharing pointers to individual elements. This would lead to an unholy amount of retesting (and possibly my head on a stake, to deter others).<br />
<br />
The least worst practical approach appears to be to bin the macros and edit the text replacement directly into all of the files, which although time-consuming does generate identical output binary files and therefore requires no re-testing. However, this does break the link between copies of the same data sets, so that a change to a particular set would require a search and edit through the code base.<br />
<br />
So, today's question is: any better suggestions, please?<br />
<br />
Regard<br />
<br />
Sid Jones<br />
<hr class="mycode_hr" />
You are right, this is currently non-compliant<br />
<br />
See MISRA compliance 2020 for how this sort of issue can be managed.<br />
<br />
The rule 16-2-2 is currently under review for the next issue]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rules 16-2-3]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1466</link>
			<pubDate>Tue, 05 Feb 2019 03:52:18 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=4992">nishiyama</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1466</guid>
			<description><![CDATA[Rules 16-2-3 Rationale has the following description.<br />
<br />
If this multiple inclusion leads to multiple or conflicting definitions,<br />
 then this can result in undefined or erroneous behaviour.<br />
<br />
I understood that it would be a multiple definition.<br />
<br />
However, I do not understand the case of conflicting definition. <br />
What kind of cases are there?]]></description>
			<content:encoded><![CDATA[Rules 16-2-3 Rationale has the following description.<br />
<br />
If this multiple inclusion leads to multiple or conflicting definitions,<br />
 then this can result in undefined or erroneous behaviour.<br />
<br />
I understood that it would be a multiple definition.<br />
<br />
However, I do not understand the case of conflicting definition. <br />
What kind of cases are there?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rules 16-2-1 and 16-2-2]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1239</link>
			<pubDate>Thu, 28 Apr 2016 09:28:37 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=4389">dg1980</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1239</guid>
			<description><![CDATA[Hi,<br />
<br />
strictly interpreted, these rules ban conditional compilation, rendering rules 16-1-1 and 16-1-2 mute.<br />
E.g.:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#ifdef FEATURE_X<br />
// some code<br />
#endif</code></div></div><br />
In addition, there appears to be no such rule in MISRA C 2012 (presumably, because it is a widely used feature in embedded systems):<br />
<br />
Rule	                       Mapping to MISRA C 2012<br />
16-0-2 [Required]	n.a. but namespaces are not available in C<br />
16-1-1 [Required]	n.a.<br />
16-2-1 [Required]	n.a.prohibits conditional compilation which is allowed in C<br />
16-2-2 [Required]	n.a.prohibits conditional compilation which is allowed in C<br />
<br />
I would expect that a feature like the preprocessor, which is equal in C and C++, is treated equally across all MISRA standards.<br />
Is there any chance this will be corrected/clarified in a new version or technical corrigendum?]]></description>
			<content:encoded><![CDATA[Hi,<br />
<br />
strictly interpreted, these rules ban conditional compilation, rendering rules 16-1-1 and 16-1-2 mute.<br />
E.g.:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#ifdef FEATURE_X<br />
// some code<br />
#endif</code></div></div><br />
In addition, there appears to be no such rule in MISRA C 2012 (presumably, because it is a widely used feature in embedded systems):<br />
<br />
Rule	                       Mapping to MISRA C 2012<br />
16-0-2 [Required]	n.a. but namespaces are not available in C<br />
16-1-1 [Required]	n.a.<br />
16-2-1 [Required]	n.a.prohibits conditional compilation which is allowed in C<br />
16-2-2 [Required]	n.a.prohibits conditional compilation which is allowed in C<br />
<br />
I would expect that a feature like the preprocessor, which is equal in C and C++, is treated equally across all MISRA standards.<br />
Is there any chance this will be corrected/clarified in a new version or technical corrigendum?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 16-0-1 and extern "C"]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1222</link>
			<pubDate>Thu, 14 Jan 2016 08:45:20 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=655">detra</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1222</guid>
			<description><![CDATA[Hello,<br />
if we want to use the same Header files for "C" and "C++" we must use the "extern C" constuct. But the "extern C" construct must be placed before the include of the Header and so our tool reports a MISRA 16-0-1 violation. Is this violation valid? And if so, is there an other solution?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#ifndef _MYHEADER_<br />
#define _MYHEADER_<br />
<br />
extern "C"<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;#include "firstheader.h"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// MISRA 16-0-1 violation<br />
}</code></div></div><br />
Thank you for help,<br />
Stefan]]></description>
			<content:encoded><![CDATA[Hello,<br />
if we want to use the same Header files for "C" and "C++" we must use the "extern C" constuct. But the "extern C" construct must be placed before the include of the Header and so our tool reports a MISRA 16-0-1 violation. Is this violation valid? And if so, is there an other solution?<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#ifndef _MYHEADER_<br />
#define _MYHEADER_<br />
<br />
extern "C"<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;#include "firstheader.h"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// MISRA 16-0-1 violation<br />
}</code></div></div><br />
Thank you for help,<br />
Stefan]]></content:encoded>
		</item>
	</channel>
</rss>