<?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 - 7.4 Code design]]></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:34 +0000</pubDate>
		<generator>MyBB</generator>
		<item>
			<title><![CDATA[Are Memory Pools allowed by MISRA-C?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1644</link>
			<pubDate>Tue, 14 Mar 2023 14:23:56 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=6093">jpature</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1644</guid>
			<description><![CDATA[Hi,<br />
<br />
<span style="color: #1d1c1d;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Slack-Lato, Slack-Fractions, appleLogo, sans-serif;" class="mycode_font">Would a memory pool (to handle a variable number of objects) based on a statically allocated chunk of memory be MISRA C compliant ? Or would it be banned because considered as dynamic allocation ?</span></span></span><br />
<br />
<span style="color: #1d1c1d;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Slack-Lato, Slack-Fractions, appleLogo, sans-serif;" class="mycode_font">Thanks.</span></span></span>]]></description>
			<content:encoded><![CDATA[Hi,<br />
<br />
<span style="color: #1d1c1d;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Slack-Lato, Slack-Fractions, appleLogo, sans-serif;" class="mycode_font">Would a memory pool (to handle a variable number of objects) based on a statically allocated chunk of memory be MISRA C compliant ? Or would it be banned because considered as dynamic allocation ?</span></span></span><br />
<br />
<span style="color: #1d1c1d;" class="mycode_color"><span style="font-size: small;" class="mycode_size"><span style="font-family: Slack-Lato, Slack-Fractions, appleLogo, sans-serif;" class="mycode_font">Thanks.</span></span></span>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Question about Dir 4.3 when a static function contains only variable declaration, asm and return statement]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1493</link>
			<pubDate>Sat, 29 Jun 2019 09:39:30 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5215">chenzhuowansui</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1493</guid>
			<description><![CDATA[Hi,<br />
Given the following case:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>static inline uint32_t test(void)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;uint32_t tmp;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;__asm__ __volatile__( <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mrc p15, 0, %0, c1, c0, 2&#92;n&#92;t"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: "=r" (tmp)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;return tmp;<br />
}</code></div></div><br />
i wonder if we could take the above case as compliant, as we cannot have variable declaration and return value in assembly. Thanks]]></description>
			<content:encoded><![CDATA[Hi,<br />
Given the following case:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>static inline uint32_t test(void)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;uint32_t tmp;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;__asm__ __volatile__( <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mrc p15, 0, %0, c1, c0, 2&#92;n&#92;t"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: "=r" (tmp)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;return tmp;<br />
}</code></div></div><br />
i wonder if we could take the above case as compliant, as we cannot have variable declaration and return value in assembly. Thanks]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[A question on Dir-4.9, in a case when a function-like macro and a function are not interchangeable.]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1492</link>
			<pubDate>Fri, 21 Jun 2019 07:26:13 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5198">mz99</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1492</guid>
			<description><![CDATA[Is the use of the function-like macro compliant (shown in Example 1 and 2 below) with Dir-4.9, in a case when a function-like macro and a function are not interchangeable?<br />
<br />
Example 1:<br />
-----------------------------------------------<br />
#define ASSIGN( a, b ) ( ( a ) = ( b ) )<br />
-----------------------------------------------<br />
<br />
Example 2:<br />
-----------------------------------------------<br />
#define MACRO( a, b )<br />
<br />
int32_t var1;<br />
MACRO(var1,var2)<br />
-----------------------------------------------<br />
<br />
The macros are not interchangeable with functions, <br />
because in Example 1, the arguments can't be modified if it is defined as a function, <br />
and in Example 2, compile error occurs because var2 is not defined.]]></description>
			<content:encoded><![CDATA[Is the use of the function-like macro compliant (shown in Example 1 and 2 below) with Dir-4.9, in a case when a function-like macro and a function are not interchangeable?<br />
<br />
Example 1:<br />
-----------------------------------------------<br />
#define ASSIGN( a, b ) ( ( a ) = ( b ) )<br />
-----------------------------------------------<br />
<br />
Example 2:<br />
-----------------------------------------------<br />
#define MACRO( a, b )<br />
<br />
int32_t var1;<br />
MACRO(var1,var2)<br />
-----------------------------------------------<br />
<br />
The macros are not interchangeable with functions, <br />
because in Example 1, the arguments can't be modified if it is defined as a function, <br />
and in Example 2, compile error occurs because var2 is not defined.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Dir 4.14 - How to check pointer to structure]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1471</link>
			<pubDate>Fri, 15 Feb 2019 07:52:33 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5204">apereira</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1471</guid>
			<description><![CDATA[For example:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>typedef struct myStruct{<br />
&nbsp;&nbsp;&nbsp;&nbsp;int myParam;<br />
} myStruct;<br />
<br />
void fcn(myStruct *const ptr)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;ptr-&gt;myParam = 1;<br />
}</code></div></div><br />
What is the recommended way to check the struct pointer in this function?]]></description>
			<content:encoded><![CDATA[For example:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>typedef struct myStruct{<br />
&nbsp;&nbsp;&nbsp;&nbsp;int myParam;<br />
} myStruct;<br />
<br />
void fcn(myStruct *const ptr)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;ptr-&gt;myParam = 1;<br />
}</code></div></div><br />
What is the recommended way to check the struct pointer in this function?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Dir 4.7 Unclear exactly what the critera is?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1465</link>
			<pubDate>Thu, 31 Jan 2019 18:57:20 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5197">chuck.cannon</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1465</guid>
			<description><![CDATA[What does it mean to be "tested in a meaningful manner"?<br />
<br />
Is there any requirement on what is done once tested? For example, if there is no corrective action is it sufficient to just log the error?<br />
<br />
Does returning the error to the caller qualify? In an API, the implementation of a API function may call subsequent functions that return an error. If only one such function is called then the only thing to do with the error is return it to the original caller.<br />
<br />
err_t func1(void)<br />
{<br />
}<br />
<br />
err_t func2(void)<br />
{<br />
    return func1();<br />
}<br />
<br />
In our project, almost the entire code base running on the uC is a library exposed via an IPC link.  All errors are just propagated back up and eventually sent back across the link to the caller.]]></description>
			<content:encoded><![CDATA[What does it mean to be "tested in a meaningful manner"?<br />
<br />
Is there any requirement on what is done once tested? For example, if there is no corrective action is it sufficient to just log the error?<br />
<br />
Does returning the error to the caller qualify? In an API, the implementation of a API function may call subsequent functions that return an error. If only one such function is called then the only thing to do with the error is return it to the original caller.<br />
<br />
err_t func1(void)<br />
{<br />
}<br />
<br />
err_t func2(void)<br />
{<br />
    return func1();<br />
}<br />
<br />
In our project, almost the entire code base running on the uC is a library exposed via an IPC link.  All errors are just propagated back up and eventually sent back across the link to the caller.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Dir 4.8 if struct is only visible through a function declaration]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1452</link>
			<pubDate>Wed, 29 Aug 2018 12:22:47 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=4209">LMende</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1452</guid>
			<description><![CDATA[Hi there,<br />
I humbly ask for help with compliance to Dir 4.8.<br />
(Borrowing from cxlin, thx)<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>/* def.h */<br />
struct SomeType<br />
{<br />
/* Object implementation */<br />
}<br />
<br />
/* other.h */<br />
extern void usetype(someType* p);<br />
<br />
/* no-use.c */<br />
#include def.h<br />
#include other.h<br />
/* Do something. But no use of SomeType at all. */</code></div></div>Given a structure SomeType which implementation is visible to no-use.c.<br />
A pointer to SomeType is only visible through the included other.h where the usetype function is declared.<br />
Is the implementation of no-use.c compliant with Dir 4.8 or not?]]></description>
			<content:encoded><![CDATA[Hi there,<br />
I humbly ask for help with compliance to Dir 4.8.<br />
(Borrowing from cxlin, thx)<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>/* def.h */<br />
struct SomeType<br />
{<br />
/* Object implementation */<br />
}<br />
<br />
/* other.h */<br />
extern void usetype(someType* p);<br />
<br />
/* no-use.c */<br />
#include def.h<br />
#include other.h<br />
/* Do something. But no use of SomeType at all. */</code></div></div>Given a structure SomeType which implementation is visible to no-use.c.<br />
A pointer to SomeType is only visible through the included other.h where the usetype function is declared.<br />
Is the implementation of no-use.c compliant with Dir 4.8 or not?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Dir 4.10: Is a different form of the include guard considered a violation of the directive?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1411</link>
			<pubDate>Thu, 29 Mar 2018 12:36:57 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=980">GerlindeKettl</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1411</guid>
			<description><![CDATA[The example of Directive 4.10 "Precautions shall be taken in order to prevent the contents of a <span style="font-style: italic;" class="mycode_i">header file</span> being included more than once" says that in order to facilitate checking, the contents of the header should be protected from being included more than once using one of the following two forms:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#if !defined ( identifier )<br />
#define identifier<br />
/* Contents of file */<br />
#endif<br />
<br />
<br />
<br />
#ifndef identifier<br />
#define identifier<br />
/* Contents of file */<br />
#endif</code></div></div>I got software from a third party which uses the first form, but the parentheses are a bit differently placed:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#if ( !defined identifier )</code></div></div>As I consider this include guard correct from a functional point of view, is this nevertheless a violation of Dir 4.10 because it does not use one of the two forms listed in the example?]]></description>
			<content:encoded><![CDATA[The example of Directive 4.10 "Precautions shall be taken in order to prevent the contents of a <span style="font-style: italic;" class="mycode_i">header file</span> being included more than once" says that in order to facilitate checking, the contents of the header should be protected from being included more than once using one of the following two forms:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#if !defined ( identifier )<br />
#define identifier<br />
/* Contents of file */<br />
#endif<br />
<br />
<br />
<br />
#ifndef identifier<br />
#define identifier<br />
/* Contents of file */<br />
#endif</code></div></div>I got software from a third party which uses the first form, but the parentheses are a bit differently placed:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#if ( !defined identifier )</code></div></div>As I consider this include guard correct from a functional point of view, is this nevertheless a violation of Dir 4.10 because it does not use one of the two forms listed in the example?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Dir 4.8 If no pointer is declared but implementation is visible]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1410</link>
			<pubDate>Thu, 29 Mar 2018 11:11:25 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=3849">cxlin</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1410</guid>
			<description><![CDATA[For Dir 4.8 the document is not clear about whether the following case should be considered as non-compliant.<br />
<br />
/* def.h */<br />
struct SomeType<br />
{<br />
  /* Object implementation */<br />
}<br />
<br />
/* no-use.c */<br />
#include def.h<br />
/* Do something. But no use of SomeType at all. */<br />
<br />
The example doesn't involve any pointer but indeed goes against the rationale since the implementation details of SomeType is not needed in no-use.c and it should at least be opaque. Also, logically, "no pointer" implies "a pointer is never dereferenced", since there is no such pointer to begin with.<br />
<br />
Please kindly help to advise. Thanks!]]></description>
			<content:encoded><![CDATA[For Dir 4.8 the document is not clear about whether the following case should be considered as non-compliant.<br />
<br />
/* def.h */<br />
struct SomeType<br />
{<br />
  /* Object implementation */<br />
}<br />
<br />
/* no-use.c */<br />
#include def.h<br />
/* Do something. But no use of SomeType at all. */<br />
<br />
The example doesn't involve any pointer but indeed goes against the rationale since the implementation details of SomeType is not needed in no-use.c and it should at least be opaque. Also, logically, "no pointer" implies "a pointer is never dereferenced", since there is no such pointer to begin with.<br />
<br />
Please kindly help to advise. Thanks!]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Dir 4.6 and usage of "fast" types in stdint.h]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1377</link>
			<pubDate>Thu, 12 Oct 2017 20:31:44 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=2549">michael.metivier</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1377</guid>
			<description><![CDATA[Directive 4.6 indicates "typedefs that indicate sign and signedness should be used in place of the basic numerical types" and suggests that, for C99-compliant systems, the types of  should be used.<br />
<br />
stdint.h also defines "fast" types which are at least of the indicated length, but may be larger if such a length could be handled more efficiently by the processor.<br />
<br />
Is it a violation of Directive 4.6 to utilize these types?  They are not "basic numerical types", but also do not necessarily adhere to a fixed length as specified by their names.]]></description>
			<content:encoded><![CDATA[Directive 4.6 indicates "typedefs that indicate sign and signedness should be used in place of the basic numerical types" and suggests that, for C99-compliant systems, the types of  should be used.<br />
<br />
stdint.h also defines "fast" types which are at least of the indicated length, but may be larger if such a length could be handled more efficiently by the processor.<br />
<br />
Is it a violation of Directive 4.6 to utilize these types?  They are not "basic numerical types", but also do not necessarily adhere to a fixed length as specified by their names.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Valid typedef name rule for float/double data type]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1351</link>
			<pubDate>Tue, 13 Jun 2017 07:17:44 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=4765">Anitha Rajasekar</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1351</guid>
			<description><![CDATA[We are appending some prefix for the typedef name as below<br />
typedef float UI_FLOAT_32.<br />
<br />
It shows the warning for the above line of code as<br />
1. misra_violation: [Advisory] MISRA C-2012 Directive 4.6 violation: typedefs that indicate size and signedness should be used in place of the basic float numerical type.<br />
<br />
MISRA Violation<br />
Typedefs Of Basic Types With Names That Do Not Indicate Size And Signedness<br />
MISRA advises compliance with this rule.<br />
<br />
The same issue is not coming if we use FLOAT32.. <br />
Is there any rule for the typedef name used for float and double.]]></description>
			<content:encoded><![CDATA[We are appending some prefix for the typedef name as below<br />
typedef float UI_FLOAT_32.<br />
<br />
It shows the warning for the above line of code as<br />
1. misra_violation: [Advisory] MISRA C-2012 Directive 4.6 violation: typedefs that indicate size and signedness should be used in place of the basic float numerical type.<br />
<br />
MISRA Violation<br />
Typedefs Of Basic Types With Names That Do Not Indicate Size And Signedness<br />
MISRA advises compliance with this rule.<br />
<br />
The same issue is not coming if we use FLOAT32.. <br />
Is there any rule for the typedef name used for float and double.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[MISRA: Code Design (MISRA C-2012 Directive 4.6)]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1350</link>
			<pubDate>Tue, 13 Jun 2017 06:48:52 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=4765">Anitha Rajasekar</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1350</guid>
			<description><![CDATA[I have a function with the below prototype<br />
void functionName(void)<br />
{<br />
}<br />
It doesn't use any int data type. Even though it shows the below warning for this code<br />
MISRA: Code Design (MISRA C-2012 Directive 4.6)<br />
1. misra_violation: [Advisory] MISRA C-2012 Directive 4.6 violation: typedefs that indicate size and signedness should be used in place of the basic int numerical type. <br />
<br />
Can anyone please help me to resolve this issue?]]></description>
			<content:encoded><![CDATA[I have a function with the below prototype<br />
void functionName(void)<br />
{<br />
}<br />
It doesn't use any int data type. Even though it shows the below warning for this code<br />
MISRA: Code Design (MISRA C-2012 Directive 4.6)<br />
1. misra_violation: [Advisory] MISRA C-2012 Directive 4.6 violation: typedefs that indicate size and signedness should be used in place of the basic int numerical type. <br />
<br />
Can anyone please help me to resolve this issue?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Interpretation of Dir 4.3]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1307</link>
			<pubDate>Mon, 23 Jan 2017 06:45:08 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=688">nakagawat</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1307</guid>
			<description><![CDATA[Hi All,<br />
<br />
Would you please advise me if a macro containing both assembly and C code<br />
is a violation of Dir 4.3?<br />
<br />
Example:<br />
  #define M_def do { asm( "mv r0,r1" ); use_uint32( 0 ); } while( 0 ) <br />
<br />
Please note that 'use_uint32( 0 );' is a function call.<br />
<br />
Regards,<br />
Tad]]></description>
			<content:encoded><![CDATA[Hi All,<br />
<br />
Would you please advise me if a macro containing both assembly and C code<br />
is a violation of Dir 4.3?<br />
<br />
Example:<br />
  #define M_def do { asm( "mv r0,r1" ); use_uint32( 0 ); } while( 0 ) <br />
<br />
Please note that 'use_uint32( 0 );' is a function call.<br />
<br />
Regards,<br />
Tad]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Directive 4.5]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1270</link>
			<pubDate>Thu, 01 Sep 2016 11:59:35 +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=1270</guid>
			<description><![CDATA[Dear MISRA team,<br />
<br />
please read the text carefully and then read MISRA C++ 2008 Rule 2-10-1.<br />
It is virtually identical but in C++ it is rated required and in C it is rated advisory.<br />
Was that a change on purpose or an accident?<br />
Thanks.]]></description>
			<content:encoded><![CDATA[Dear MISRA team,<br />
<br />
please read the text carefully and then read MISRA C++ 2008 Rule 2-10-1.<br />
It is virtually identical but in C++ it is rated required and in C it is rated advisory.<br />
Was that a change on purpose or an accident?<br />
Thanks.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[How do I create a module in MISRAC:2012 that follows Dir 4.12 and 4.8?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1227</link>
			<pubDate>Thu, 18 Feb 2016 08:52:00 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=4252">crisls1</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1227</guid>
			<description><![CDATA[I asked the following question on Stack Overflow and received a helpful answer which I have integrated into my question here. (<a href="http://stackoverflow.com/questions/35341893/how-do-i-create-a-module-in-misrac2012-that-follows-dir-4-12-and-4-8" target="_blank" rel="noopener" class="mycode_url">http://stackoverflow.com/questions/35341...12-and-4-8</a>)  The following is an extended version of the question I asked. This question relates to coding in ISO C99 following the MISRAC:2012 guidelines.<br />
<br />
I am looking for guidance on Dir 4.8 â€œIf a pointer to a structure or union is never dereferenced within a translation unit, then the implementation of the object should be hiddenâ€ in conjunction with Dir 4.12 â€œDynamic memory allocation shall not be usedâ€.<br />
<br />
When implementing an Abstract Data Type in C it is common to refer to the ADT using a handle that is a pointer to a structure describing the internal state of the ADT. This can be done using an opaque pointer as per Dir 4.8 with the benefit that the internal details remain hidden from the user.<br />
<br />
Generally more than one of these ADTs could exist so there must be a way to create mulitple handles. This can be solved by allocating memory for the internal details referenced by the handle in an initialisation function, however, this is not allowed under Dir 4.12.<br />
<br />
Another option is that the initialisation routine receives a pointer to a statically allocated handle provided by the user, however, this cannot be accomplished using opaque pointers.<br />
<br />
I illustrate the problem below.<br />
<br />
<br />
      Module.h <br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>/* Module handle is only available to&nbsp;&nbsp;the world as an incomplete type.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; This allows us to satisfy MISRAC 2012 Dir 4.8.*/<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct module; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;typedef struct module module_t;</code></div></div>        Module.c<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#include "module.h"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct module<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uint8_t value;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module_t* module_get_a_handle(void)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* MISRAC 2012 Dir 4.12 disallows dynamic memory allocation.*/<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (module_t*)malloc(sizeof(struct module)); <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</code></div></div>        User.c<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#include "module.h"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module_t* module_handle;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module_handle = module_get_a_handle();</code></div></div><br />
One solution is to use a statically allocated pool of handles within the ADT that are made available to client code. This solution seems to be technically compliant; however, it seems that the concept of dynamic memory allocation still exists here. I would argue that although the handles are statically allocated, it cannot be determined by the compiler during compilation whether there will be enough handles available for the software to function correctly.<br />
<br />
I understand the argument that if a module runs out of statically allocated handles then it is a design error. However, it seems advantageous if the compiler can also determine in many cases whether there is enough space at compile time (this forces developers to fix the problem early rather than discovering it in code reviews or testing later on).  The compiler can determine the problem if a statically allocated handle is passed from client code to the ADT as it is then clear to the compiler how many handles in total will be required by the SW. This also feels like a cleaner separation of concerns to me. Is it the responsibility of the ADT to know how many handles may be required, and if so does this not complicate any module reuse somewhat?  <br />
<br />
Additionally, in my case we are defining a company template that all new modules should be based on. The level of C proficiency of our developers ranges from beginner to intermediate. I am concerned that requiring each ADT to manage a static buffer of handles will be seen as overly complicated (and therefore a maintenance issue) and be met with resistance. Clearly raising the C skill level of developers is desirable but this will not happen immediately.<br />
<br />
My solution to this problem would be to deviate on the advisory directive 4.8 and use non-opaque pointers and a strong naming convention that makes it clear to users that the internal details of the ADT must not be changed.<br />
<br />
I am curious whether there is a well accepted method to solve this issue that satisfies Dir 4.8, and Dir 4.12, and does not break any other MISRAC:2012 rules. Is it common practice to define a static array of handles in every ADT?<br />
<br />
I'm also interested in a critique of <br />
<br />
a) deviating Dir 4.8 and using a naming convention that makes it obvious that the handle points to internal details that should not be accessed directly coupled with educating developers not to access the internals details. <br />
<br />
b) deviating Dir 4.12(!) to allow dynamic memory allocation in a well defined initialisation phase only. For b), I believe that if it can be shown that the memory allocation is deterministic in this initialisation phase and the initialisation phase can be tested at a system level successfully once then it follows that the memory allocation will always be successful.<br />
<br />
My preference is to deviate on the advisory directive 4.8 in this particular case as it seems to have the least disadvantages.<br />
<br />
Any comments would be greatly appreciated.]]></description>
			<content:encoded><![CDATA[I asked the following question on Stack Overflow and received a helpful answer which I have integrated into my question here. (<a href="http://stackoverflow.com/questions/35341893/how-do-i-create-a-module-in-misrac2012-that-follows-dir-4-12-and-4-8" target="_blank" rel="noopener" class="mycode_url">http://stackoverflow.com/questions/35341...12-and-4-8</a>)  The following is an extended version of the question I asked. This question relates to coding in ISO C99 following the MISRAC:2012 guidelines.<br />
<br />
I am looking for guidance on Dir 4.8 â€œIf a pointer to a structure or union is never dereferenced within a translation unit, then the implementation of the object should be hiddenâ€ in conjunction with Dir 4.12 â€œDynamic memory allocation shall not be usedâ€.<br />
<br />
When implementing an Abstract Data Type in C it is common to refer to the ADT using a handle that is a pointer to a structure describing the internal state of the ADT. This can be done using an opaque pointer as per Dir 4.8 with the benefit that the internal details remain hidden from the user.<br />
<br />
Generally more than one of these ADTs could exist so there must be a way to create mulitple handles. This can be solved by allocating memory for the internal details referenced by the handle in an initialisation function, however, this is not allowed under Dir 4.12.<br />
<br />
Another option is that the initialisation routine receives a pointer to a statically allocated handle provided by the user, however, this cannot be accomplished using opaque pointers.<br />
<br />
I illustrate the problem below.<br />
<br />
<br />
      Module.h <br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>/* Module handle is only available to&nbsp;&nbsp;the world as an incomplete type.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; This allows us to satisfy MISRAC 2012 Dir 4.8.*/<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct module; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;typedef struct module module_t;</code></div></div>        Module.c<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#include "module.h"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct module<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uint8_t value;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module_t* module_get_a_handle(void)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* MISRAC 2012 Dir 4.12 disallows dynamic memory allocation.*/<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (module_t*)malloc(sizeof(struct module)); <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</code></div></div>        User.c<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>#include "module.h"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module_t* module_handle;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module_handle = module_get_a_handle();</code></div></div><br />
One solution is to use a statically allocated pool of handles within the ADT that are made available to client code. This solution seems to be technically compliant; however, it seems that the concept of dynamic memory allocation still exists here. I would argue that although the handles are statically allocated, it cannot be determined by the compiler during compilation whether there will be enough handles available for the software to function correctly.<br />
<br />
I understand the argument that if a module runs out of statically allocated handles then it is a design error. However, it seems advantageous if the compiler can also determine in many cases whether there is enough space at compile time (this forces developers to fix the problem early rather than discovering it in code reviews or testing later on).  The compiler can determine the problem if a statically allocated handle is passed from client code to the ADT as it is then clear to the compiler how many handles in total will be required by the SW. This also feels like a cleaner separation of concerns to me. Is it the responsibility of the ADT to know how many handles may be required, and if so does this not complicate any module reuse somewhat?  <br />
<br />
Additionally, in my case we are defining a company template that all new modules should be based on. The level of C proficiency of our developers ranges from beginner to intermediate. I am concerned that requiring each ADT to manage a static buffer of handles will be seen as overly complicated (and therefore a maintenance issue) and be met with resistance. Clearly raising the C skill level of developers is desirable but this will not happen immediately.<br />
<br />
My solution to this problem would be to deviate on the advisory directive 4.8 and use non-opaque pointers and a strong naming convention that makes it clear to users that the internal details of the ADT must not be changed.<br />
<br />
I am curious whether there is a well accepted method to solve this issue that satisfies Dir 4.8, and Dir 4.12, and does not break any other MISRAC:2012 rules. Is it common practice to define a static array of handles in every ADT?<br />
<br />
I'm also interested in a critique of <br />
<br />
a) deviating Dir 4.8 and using a naming convention that makes it obvious that the handle points to internal details that should not be accessed directly coupled with educating developers not to access the internals details. <br />
<br />
b) deviating Dir 4.12(!) to allow dynamic memory allocation in a well defined initialisation phase only. For b), I believe that if it can be shown that the memory allocation is deterministic in this initialisation phase and the initialisation phase can be tested at a system level successfully once then it follows that the memory allocation will always be successful.<br />
<br />
My preference is to deviate on the advisory directive 4.8 in this particular case as it seems to have the least disadvantages.<br />
<br />
Any comments would be greatly appreciated.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Dir 4.8 2 struct pointers in a Translation unit]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1145</link>
			<pubDate>Fri, 13 Feb 2015 09:46:25 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=196">misra-c</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1145</guid>
			<description><![CDATA[What happens if there are 2 pointers to a struct/union in a translation unit.  One which accesses the details of the struct/union and the other does not.   For Example:<br />
File1.h<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>struct X { int32_t i1 };<br />
&nbsp;&nbsp;typedef struct X * ptrX;<br />
&nbsp;&nbsp;extern&nbsp;&nbsp;void use_ptrX ( ptrX p );</code></div></div> <br />
File1.c<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>void fn ( ptrX a, ptrX b )<br />
&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp; /* no reference to a-&gt;i1 in translation unit&nbsp;&nbsp;- does the type of "a" need to be opaque? */<br />
&nbsp;&nbsp;&nbsp;&nbsp; use_ptrX ( a );<br />
&nbsp;&nbsp;&nbsp;&nbsp; use_int32 ( b -&gt; i1 ) ;<br />
 }</code></div></div>]]></description>
			<content:encoded><![CDATA[What happens if there are 2 pointers to a struct/union in a translation unit.  One which accesses the details of the struct/union and the other does not.   For Example:<br />
File1.h<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>struct X { int32_t i1 };<br />
&nbsp;&nbsp;typedef struct X * ptrX;<br />
&nbsp;&nbsp;extern&nbsp;&nbsp;void use_ptrX ( ptrX p );</code></div></div> <br />
File1.c<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>void fn ( ptrX a, ptrX b )<br />
&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp; /* no reference to a-&gt;i1 in translation unit&nbsp;&nbsp;- does the type of "a" need to be opaque? */<br />
&nbsp;&nbsp;&nbsp;&nbsp; use_ptrX ( a );<br />
&nbsp;&nbsp;&nbsp;&nbsp; use_int32 ( b -&gt; i1 ) ;<br />
 }</code></div></div>]]></content:encoded>
		</item>
	</channel>
</rss>