<?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.15 Exception handling (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 13:23:21 +0000</pubDate>
		<generator>MyBB</generator>
		<item>
			<title><![CDATA[Memory allocation when throwing an exception.]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1442</link>
			<pubDate>Tue, 24 Jul 2018 12:15:31 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5088">apasternak</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1442</guid>
			<description><![CDATA[Dear all<br />
<br />
While doing a memory audit of our framework for autonomous driving cars we realized that throwing any kind of exception dynamically allocates memory on compilers using the Itanium C++ ABI (notably gcc and clang). Further, throwing an exception may block for which is not acceptable for safety-critical real-time systems.<br />
<br />
Neither Misra 2008 nor Autosar 14 or other guidelines forbid exceptions in safety-critical environments. What were the considerations when investigating this issue? <br />
<br />
To solve this issue we created a library which replaces the dynamic memory allocation with a memory pool. This library is freely available here: <a href="https://github.com/ApexAI/static_exception" target="_blank" rel="noopener" class="mycode_url">https://github.com/ApexAI/static_exception</a><br />
<br />
Any inputs or other approaches would be very welcome!<br />
<br />
Best regards,<br />
<br />
Andreas]]></description>
			<content:encoded><![CDATA[Dear all<br />
<br />
While doing a memory audit of our framework for autonomous driving cars we realized that throwing any kind of exception dynamically allocates memory on compilers using the Itanium C++ ABI (notably gcc and clang). Further, throwing an exception may block for which is not acceptable for safety-critical real-time systems.<br />
<br />
Neither Misra 2008 nor Autosar 14 or other guidelines forbid exceptions in safety-critical environments. What were the considerations when investigating this issue? <br />
<br />
To solve this issue we created a library which replaces the dynamic memory allocation with a memory pool. This library is freely available here: <a href="https://github.com/ApexAI/static_exception" target="_blank" rel="noopener" class="mycode_url">https://github.com/ApexAI/static_exception</a><br />
<br />
Any inputs or other approaches would be very welcome!<br />
<br />
Best regards,<br />
<br />
Andreas]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Proverbially Playing the `Devil's Advocate' for Rule 15-1-2]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=581</link>
			<pubDate>Thu, 11 Sep 2008 18:46:48 +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=581</guid>
			<description><![CDATA[The text and rationale of rule 15-1-2 make no mention of the word `macro'.  Let us suppose an application made no reference to the Standard Library within it's source code.  Consequently, the `NULL' macro has not been defined.  Would the following code violate rule 15-1-2?<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>class A {};<br />
A NULL;<br />
void f()<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;throw NULL;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</code></div></div>The very premise of the rationale for the rule is invalid.  In this case a `throw NULL;' is <span style="font-style: italic;" class="mycode_i">not</span> equivalent to `throw (0);' and is not a throwing of an integer expression.<br />
<br />
Furthermore, the wording of the rationale is slightly incorrect:<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>throw(NULL) (equivalent to throw(0)) is never a throw of the null-pointer-constant ...</blockquote>
To quote ISO C++, 18.1, para. 4, (lib.support.types):<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>The macro NULL is an implementation-defined C++ null pointer constant in this International Standard<br />
(4.10).</blockquote>
Granted, the referenced `4.10' states a null pointer constant has integer (not pointer) type.  However, the macro <span style="font-style: italic;" class="mycode_i">is</span> a null pointer constant.]]></description>
			<content:encoded><![CDATA[The text and rationale of rule 15-1-2 make no mention of the word `macro'.  Let us suppose an application made no reference to the Standard Library within it's source code.  Consequently, the `NULL' macro has not been defined.  Would the following code violate rule 15-1-2?<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>class A {};<br />
A NULL;<br />
void f()<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;throw NULL;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</code></div></div>The very premise of the rationale for the rule is invalid.  In this case a `throw NULL;' is <span style="font-style: italic;" class="mycode_i">not</span> equivalent to `throw (0);' and is not a throwing of an integer expression.<br />
<br />
Furthermore, the wording of the rationale is slightly incorrect:<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>throw(NULL) (equivalent to throw(0)) is never a throw of the null-pointer-constant ...</blockquote>
To quote ISO C++, 18.1, para. 4, (lib.support.types):<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>The macro NULL is an implementation-defined C++ null pointer constant in this International Standard<br />
(4.10).</blockquote>
Granted, the referenced `4.10' states a null pointer constant has integer (not pointer) type.  However, the macro <span style="font-style: italic;" class="mycode_i">is</span> a null pointer constant.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Misleading statements in Rule 15â€“1â€“3.]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=553</link>
			<pubDate>Mon, 14 Jul 2008 20:21:55 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=624">James Widman</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=553</guid>
			<description><![CDATA[Hi all,<br />
<br />
In the Rationale for Rule 15â€“1â€“3, we have:<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>However, syntactically, there is nothing to prevent throw; being used outside a catch handler, where there is no exception object to re-throw. This may lead to an implementation-defined program termination. </blockquote>
There are a couple of problems with this.<br />
<br />
First, it't not quite right to refer to a *place* where an exception object can be re-thrown so much as it is to refer to a *time* when such an object can be re-thrown.  Refer to 15.1 except.throw.  For example, this program:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>extern "C" int printf( const char*, ... );<br />
<br />
struct A<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;A() { (void)printf( "::A::A()&#92;n" ); }<br />
&nbsp;&nbsp;&nbsp;&nbsp;~A() { (void)printf( "::A::~A()&#92;n" ); }<br />
&nbsp;&nbsp;&nbsp;&nbsp;};<br />
<br />
void rethrow() { throw; }<br />
<br />
void f(&nbsp;&nbsp;) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw A();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;catch (A&amp; p) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void)printf( "entering f()'s handler...&#92;n" );<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rethrow();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void)printf( "exiting f()'s handler without throwing...&#92;n" );<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
<br />
int main (void) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;catch( A&amp; p ) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void)printf( "exiting main()'s handler...&#92;n" );<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br />
}</code></div></div><br />
... yields this output:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>::A::A()<br />
entering f()'s handler...<br />
exiting main()'s handler...<br />
::A::~A()</code></div></div>(This is specified in the Standard; this is not implementation-defined behavior.)<br />
<br />
So the text "where there is no exception object to re-throw" should be changed to something like, "where control could pass at a time when there is no exception object to re-throw".<br />
<br />
Second, the rationale text ends with, "This may lead to an implementation-defined program termination."<br />
<br />
When you squint at the standardese in just the right way, this sentence is true: it *may* lead to implementation-defined behavior, but only if the non-libary part of the program does not set a terminate_handler.  But given the normative text in 15.5.1 except.terminate, it would be much better to change this sentence to:<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>If an empty throw is used at a time when there is no exception object to re-throw, std::terminate() is implicitly called.  (The stack is not unwound before this call.)</blockquote>
]]></description>
			<content:encoded><![CDATA[Hi all,<br />
<br />
In the Rationale for Rule 15â€“1â€“3, we have:<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>However, syntactically, there is nothing to prevent throw; being used outside a catch handler, where there is no exception object to re-throw. This may lead to an implementation-defined program termination. </blockquote>
There are a couple of problems with this.<br />
<br />
First, it't not quite right to refer to a *place* where an exception object can be re-thrown so much as it is to refer to a *time* when such an object can be re-thrown.  Refer to 15.1 except.throw.  For example, this program:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>extern "C" int printf( const char*, ... );<br />
<br />
struct A<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;A() { (void)printf( "::A::A()&#92;n" ); }<br />
&nbsp;&nbsp;&nbsp;&nbsp;~A() { (void)printf( "::A::~A()&#92;n" ); }<br />
&nbsp;&nbsp;&nbsp;&nbsp;};<br />
<br />
void rethrow() { throw; }<br />
<br />
void f(&nbsp;&nbsp;) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw A();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;catch (A&amp; p) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void)printf( "entering f()'s handler...&#92;n" );<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rethrow();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void)printf( "exiting f()'s handler without throwing...&#92;n" );<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
<br />
int main (void) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;catch( A&amp; p ) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void)printf( "exiting main()'s handler...&#92;n" );<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br />
}</code></div></div><br />
... yields this output:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>::A::A()<br />
entering f()'s handler...<br />
exiting main()'s handler...<br />
::A::~A()</code></div></div>(This is specified in the Standard; this is not implementation-defined behavior.)<br />
<br />
So the text "where there is no exception object to re-throw" should be changed to something like, "where control could pass at a time when there is no exception object to re-throw".<br />
<br />
Second, the rationale text ends with, "This may lead to an implementation-defined program termination."<br />
<br />
When you squint at the standardese in just the right way, this sentence is true: it *may* lead to implementation-defined behavior, but only if the non-libary part of the program does not set a terminate_handler.  But given the normative text in 15.5.1 except.terminate, it would be much better to change this sentence to:<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>If an empty throw is used at a time when there is no exception object to re-throw, std::terminate() is implicitly called.  (The stack is not unwound before this call.)</blockquote>
]]></content:encoded>
		</item>
	</channel>
</rss>