<?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.9 Initialisation]]></title>
		<link>https://forum.misra.org.uk/</link>
		<description><![CDATA[MISRA Discussion Forums - https://forum.misra.org.uk]]></description>
		<pubDate>Thu, 05 Nov 2026 15:13:22 +0000</pubDate>
		<generator>MyBB</generator>
		<item>
			<title><![CDATA[Rule 9.2 zero initialization of array of struct]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1617</link>
			<pubDate>Wed, 27 Apr 2022 11:28:05 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5801">sowisojh</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1617</guid>
			<description><![CDATA[I Have the following code:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#include &lt;stdint.h&gt;<br />
<br />
typedef struct {<br />
    int16_t foo;<br />
    int16_t bar;<br />
} foobar_t;<br />
<br />
static foobar_t foobars[42] = {0};</code></div></div><br />
In rule 9.2 the following is given:<br />
<br />
<blockquote class="mycode_quote"><cite>Quote:</cite><span style="font-size: large;" class="mycode_size"><span style="font-family: serif;" class="mycode_font">Note also that all the elements of arrays or structures can be initialised (to zero or</span></span> <span style="font-size: large;" class="mycode_size"><span style="font-family: serif;" class="mycode_font">NULL</span></span><span style="font-size: large;" class="mycode_size"><span style="font-family: serif;" class="mycode_font">) by giving an</span></span><br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: serif;" class="mycode_font">explicit initialiser for the first element only. If this method of initialisation is chosen then the first</span></span><br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: serif;" class="mycode_font">element should be initialised to zero (or</span></span> <span style="font-size: large;" class="mycode_size"><span style="font-family: serif;" class="mycode_font">NULL</span></span><span style="font-size: large;" class="mycode_size"><span style="font-family: serif;" class="mycode_font">), and nested braces need not be used.</span></span></blockquote>
<br />
<br />
Is the zero initialization of foobars compliant with Rule 9.2? <br />
<br />
Lint is complaining about this kind of initialization, but I'm not sure if it is right or not<br />
Warning: LINT [W940] omitted braces within an initializer [MISRA 2004 Rule 9.2]<br />
<br />
<br />
The rule mentions that the first element should be initialized to zero. The first element, which can be listed in the initialization list, would be the member foo of the first struct of the first array element. Setting this to 0 would, in my understanding, comply with rule 9.2. As in the last sentence "... nested braces need not be used" is mentioned, no further braces around the 0 are required.]]></description>
			<content:encoded><![CDATA[I Have the following code:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#include &lt;stdint.h&gt;<br />
<br />
typedef struct {<br />
    int16_t foo;<br />
    int16_t bar;<br />
} foobar_t;<br />
<br />
static foobar_t foobars[42] = {0};</code></div></div><br />
In rule 9.2 the following is given:<br />
<br />
<blockquote class="mycode_quote"><cite>Quote:</cite><span style="font-size: large;" class="mycode_size"><span style="font-family: serif;" class="mycode_font">Note also that all the elements of arrays or structures can be initialised (to zero or</span></span> <span style="font-size: large;" class="mycode_size"><span style="font-family: serif;" class="mycode_font">NULL</span></span><span style="font-size: large;" class="mycode_size"><span style="font-family: serif;" class="mycode_font">) by giving an</span></span><br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: serif;" class="mycode_font">explicit initialiser for the first element only. If this method of initialisation is chosen then the first</span></span><br />
<span style="font-size: large;" class="mycode_size"><span style="font-family: serif;" class="mycode_font">element should be initialised to zero (or</span></span> <span style="font-size: large;" class="mycode_size"><span style="font-family: serif;" class="mycode_font">NULL</span></span><span style="font-size: large;" class="mycode_size"><span style="font-family: serif;" class="mycode_font">), and nested braces need not be used.</span></span></blockquote>
<br />
<br />
Is the zero initialization of foobars compliant with Rule 9.2? <br />
<br />
Lint is complaining about this kind of initialization, but I'm not sure if it is right or not<br />
Warning: LINT [W940] omitted braces within an initializer [MISRA 2004 Rule 9.2]<br />
<br />
<br />
The rule mentions that the first element should be initialized to zero. The first element, which can be listed in the initialization list, would be the member foo of the first struct of the first array element. Setting this to 0 would, in my understanding, comply with rule 9.2. As in the last sentence "... nested braces need not be used" is mentioned, no further braces around the 0 are required.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[9.1 Variable may not have been initialized]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1400</link>
			<pubDate>Mon, 19 Feb 2018 11:51:19 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=4977">ankitshah413</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1400</guid>
			<description><![CDATA[Hello,<br />
<br />
I have an issue with MISRA 9.1 rule. In my code i have a "if" loop and inside that a variable is initialized and then is read after that. But when i do static analysis using pclint then i have a warning for 9.1 rule. Below is my code :<br />
<br />
struct mgm2_info_return msg_ascu_info;<br />
   <div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>if(klr_ecuConf_t-&gt;feat_multislave)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg_ascu_info = mgmF_massage_get_ascu_info();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;msg_info = mgmF_massage_get_info();</code></div></div><br />
The line where it is read and where warning occurs:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>if( (((vF_mcms_fl_on((tm_uint8)V_FLONCMD_ACK)) ||<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(msg_info.pmcu_state ==&nbsp;&nbsp;LDF_MASSSQC_STATE_ACTIVE))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;((klr_ecuConf_t-&gt;feat_multislave)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|| (msg_ascu_info.ascu_state ==&nbsp;&nbsp;LDF_MASSSQC_STATE_ACTIVE))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;) &amp;&amp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(msg_info.pmcu_trig_source == RqSource_ActvComf))</code></div></div><br />
Do i need to write a justification or i need to initialize the entire structure first?]]></description>
			<content:encoded><![CDATA[Hello,<br />
<br />
I have an issue with MISRA 9.1 rule. In my code i have a "if" loop and inside that a variable is initialized and then is read after that. But when i do static analysis using pclint then i have a warning for 9.1 rule. Below is my code :<br />
<br />
struct mgm2_info_return msg_ascu_info;<br />
   <div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>if(klr_ecuConf_t-&gt;feat_multislave)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg_ascu_info = mgmF_massage_get_ascu_info();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;msg_info = mgmF_massage_get_info();</code></div></div><br />
The line where it is read and where warning occurs:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>if( (((vF_mcms_fl_on((tm_uint8)V_FLONCMD_ACK)) ||<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(msg_info.pmcu_state ==&nbsp;&nbsp;LDF_MASSSQC_STATE_ACTIVE))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;((klr_ecuConf_t-&gt;feat_multislave)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|| (msg_ascu_info.ascu_state ==&nbsp;&nbsp;LDF_MASSSQC_STATE_ACTIVE))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;) &amp;&amp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(msg_info.pmcu_trig_source == RqSource_ActvComf))</code></div></div><br />
Do i need to write a justification or i need to initialize the entire structure first?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[9.1 and Addressing]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=881</link>
			<pubDate>Mon, 13 Feb 2012 22:37:34 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=64">gs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=881</guid>
			<description><![CDATA[Suppose I have two functions:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>void f( int * );<br />
void g( const int * );</code></div></div>and the following usage:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>void h()<br />
{<br />
int i;<br />
int j;<br />
<br />
f( &amp;i );<br />
g( &amp;j );<br />
}</code></div></div>Do these violate rule #9.1? The variable is used. Furthermore, in the case of the call to g(), no possibility exists of j becoming initialized by the call, unlike the call to f() with respect to i.]]></description>
			<content:encoded><![CDATA[Suppose I have two functions:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>void f( int * );<br />
void g( const int * );</code></div></div>and the following usage:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>void h()<br />
{<br />
int i;<br />
int j;<br />
<br />
f( &amp;i );<br />
g( &amp;j );<br />
}</code></div></div>Do these violate rule #9.1? The variable is used. Furthermore, in the case of the call to g(), no possibility exists of j becoming initialized by the call, unlike the call to f() with respect to i.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Multiple local initialsaions in a single line]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=849</link>
			<pubDate>Tue, 20 Sep 2011 06:10:05 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=1748">alon</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=849</guid>
			<description><![CDATA[Hello <br />
<br />
dose a  line of the form <br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>int i=0,j=0,k,l;</code></div></div>considered a misuse of the comma operator.  <br />
meaning,  the line is considered as composed of multiple statements in a single line.<br />
if so then what about,  just plain <br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>int i,jk;</code></div></div><br />
Thanks <br />
<br />
-Alon]]></description>
			<content:encoded><![CDATA[Hello <br />
<br />
dose a  line of the form <br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>int i=0,j=0,k,l;</code></div></div>considered a misuse of the comma operator.  <br />
meaning,  the line is considered as composed of multiple statements in a single line.<br />
if so then what about,  just plain <br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>int i,jk;</code></div></div><br />
Thanks <br />
<br />
-Alon]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 9.2 - ISO/IEC reference sought]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=658</link>
			<pubDate>Tue, 26 May 2009 12:56:03 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=833">Renishug</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=658</guid>
			<description><![CDATA[In the comments about rule 9.2 it says, "[...] all the elements of arrays or structures can be initialised (to zero or NULL) by giving an explicit initialiser for the first element only."<br />
<br />
I didn't know this, and have looked and looked for where this is said in the ISO/IEC 9899:1990 standard. (I have looked on several occasions now.) Maybe it was right under my nose, but for the life of me I could not find it. Could someone put me out of my misery and tell me where this is stated? Thank you.]]></description>
			<content:encoded><![CDATA[In the comments about rule 9.2 it says, "[...] all the elements of arrays or structures can be initialised (to zero or NULL) by giving an explicit initialiser for the first element only."<br />
<br />
I didn't know this, and have looked and looked for where this is said in the ISO/IEC 9899:1990 standard. (I have looked on several occasions now.) Maybe it was right under my nose, but for the life of me I could not find it. Could someone put me out of my misery and tell me where this is stated? Thank you.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 9.2 in TC1]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=461</link>
			<pubDate>Wed, 20 Feb 2008 14:53:53 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=436">Lundin</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=461</guid>
			<description><![CDATA[I question the practical use of the text added to rule 9.2 in TC1. According to that rule, zero initialization of an array or struct can only occur at top level.<br />
<br />
How do you write MISRA-C compatible code for this case:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>typedef struct<br />
{<br />
&nbsp;&nbsp;BOOL enabled;<br />
&nbsp;&nbsp;uint8_t buffer [5000];<br />
<br />
} DataBuffer;<br />
<br />
<br />
DataBuffer db = {TRUE};&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* not compliant, non-zero initialization */<br />
DataBuffer db = {TRUE, {0} };&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* not compliant, zero initialization at sub level*/<br />
<br />
DataBuffer db = {TRUE, /* what goes here? */ };</code></div></div><br />
As the rule is now, the programmer who happens to have a data structure like the one above will have to type out 5000 zeroes in his source file to conform to MISRA-C.<br />
<br />
I don't see why the second of those lines can't be allowed. The two non-compliant examples above are perfectly safe and will set the whole buffer to zero.  ISO C is clear:<br />
<br />
<blockquote class="mycode_quote"><cite>ISO 9899:1999, chapter 6.7.8 Initialization Wrote:</cite>21 If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or <br />
fewer characters in a string literal used to initialize an array of known size than there are elements in the array, <br />
the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.</blockquote>(I can only cite C99, not C90, though they are identical in this case)<br />
<br />
The buffer is implicitly initialized as if it had static storage duration and is therefore set to zero.<br />
<br />
The argument "the struct can be initialized to zero and the Boolean member can then be set to TRUE in runtime" is not valid, as the struct might be const.]]></description>
			<content:encoded><![CDATA[I question the practical use of the text added to rule 9.2 in TC1. According to that rule, zero initialization of an array or struct can only occur at top level.<br />
<br />
How do you write MISRA-C compatible code for this case:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>typedef struct<br />
{<br />
&nbsp;&nbsp;BOOL enabled;<br />
&nbsp;&nbsp;uint8_t buffer [5000];<br />
<br />
} DataBuffer;<br />
<br />
<br />
DataBuffer db = {TRUE};&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* not compliant, non-zero initialization */<br />
DataBuffer db = {TRUE, {0} };&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* not compliant, zero initialization at sub level*/<br />
<br />
DataBuffer db = {TRUE, /* what goes here? */ };</code></div></div><br />
As the rule is now, the programmer who happens to have a data structure like the one above will have to type out 5000 zeroes in his source file to conform to MISRA-C.<br />
<br />
I don't see why the second of those lines can't be allowed. The two non-compliant examples above are perfectly safe and will set the whole buffer to zero.  ISO C is clear:<br />
<br />
<blockquote class="mycode_quote"><cite>ISO 9899:1999, chapter 6.7.8 Initialization Wrote:</cite>21 If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or <br />
fewer characters in a string literal used to initialize an array of known size than there are elements in the array, <br />
the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.</blockquote>(I can only cite C99, not C90, though they are identical in this case)<br />
<br />
The buffer is implicitly initialized as if it had static storage duration and is therefore set to zero.<br />
<br />
The argument "the struct can be initialized to zero and the Boolean member can then be set to TRUE in runtime" is not valid, as the struct might be const.]]></content:encoded>
		</item>
	</channel>
</rss>