# Simple way to reduce code size when using FreeRTOS from C++ with exceptions enabled

**URL:** <https://forums.freertos.org/t/simple-way-to-reduce-code-size-when-using-freertos-from-c-with-exceptions-enabled/12309>\
**Category:** Kernel\
**Created:** [April 14, 2021, 7:54am UTC](https://forums.freertos.org/t/simple-way-to-reduce-code-size-when-using-freertos-from-c-with-exceptions-enabled/12309 "2021-04-14T07:54:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dc42](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/dc42/32/487_2.png) [@dc42](https://forums.freertos.org/u/dc42)\
**Post date:** [April 14, 2021, 7:54am UTC](https://forums.freertos.org/t/simple-way-to-reduce-code-size-when-using-freertos-from-c-with-exceptions-enabled/12309/1 "2021-04-14T07:54:48Z")

</div>

We use FreeRTOS from a large C++ application. We mostly use C++ wrappers around the FreeRTOS API, although some of these are inline functions, and sometimes we make kernel calls directly. Our application is compiled with exceptions enabled, although most of its functions are declared ‘noexcept’.

We have observed that when a ‘noexcept’ C++ function calls a C or C++ function that is not declared ‘noexcept’, GCC creates a small exception-handling table for that function, and sometimes also adjusts the stack alignment around the call to that function. Both of these add to the code space. Therefore, it is desirable to declare called functions ‘noexcept’ wherever possible, even C functions.

Fortunately, FreeRTOS appears to declare all its API functions with a trailing PRIVILEGED\_FUNCTION macro call. So I changed the definition of this macro in file mpu\_wrappers.h from an empty definition to the following:

```
# ifdef __cplusplus
	#define PRIVILEGED_FUNCTION noexcept
# else
	#define PRIVILEGED_FUNCTION
# endif

```

This simple change saved 2316 bytes of code space in our application.

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [April 14, 2021, 8:04am UTC](https://forums.freertos.org/t/simple-way-to-reduce-code-size-when-using-freertos-from-c-with-exceptions-enabled/12309/2 "2021-04-14T08:04:12Z")

</div>

Thanks for the observation, David!

Whereever I have a say in system design, I insist in disabling C++ exceptions altogether (add -fno-exceptions to both compiler and linker options). One reason being that, as you observe, additional sections are being created that must (among other things) be sorted into the RAM layout to ensure bootloader compatibility AND cause (mostly unneeded) footprint.

The point for SEH in embedded systems is debatable anyways, but that is an entirely different discussion.

Thanks!

---

<div class="post-metadata">

**Author:** ![dc42](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/dc42/32/487_2.png) [@dc42](https://forums.freertos.org/u/dc42)\
**Post date:** [April 14, 2021, 8:14am UTC](https://forums.freertos.org/t/simple-way-to-reduce-code-size-when-using-freertos-from-c-with-exceptions-enabled/12309/3 "2021-04-14T08:14:24Z")

</div>

I compile with exceptions disabled whenever possible too. In this case, one of the tasks has to do complicated parsing of input. It proved much simpler to handle parsing errors using exceptions, instead of every parsing function returning an error object, which had to be tested by all its callers. So that part of the system uses exceptions, and all functions in the rest of the system (which includes all the hard real-time parts) are declared ‘noexcept’.

I published a blog entry on using C++ exceptions in embedded software at [Using exceptions in C++ embedded software | David Crocker's Solutions blog](https://miscsolutions.wordpress.com/2019/12/27/using-exceptions-in-c-embedded-software/).

---

<div class="post-metadata">

**Author:** ![dnadler](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/dnadler/32/35_2.png) [@dnadler](https://forums.freertos.org/u/dnadler)\
**Post date:** [April 16, 2021, 12:33am UTC](https://forums.freertos.org/t/simple-way-to-reduce-code-size-when-using-freertos-from-c-with-exceptions-enabled/12309/4 "2021-04-16T00:33:27Z")

</div>

@dc42 - FYI: [GCC ARM exception bug](https://bugs.launchpad.net/gcc-arm-embedded/+bug/1905459)  
We don’t use exceptions in embedded code (though I sure would like to)…  
Hope that helps,  
Best Regards, Dave

PS: You didn’t say your platform, but you might also want to review this: [https://community.arm.com/developer/tools-software/oss-platforms/f/gnu-toolchain-forum/11581/gcc-7-2-1-on-cortex-m4---c-exceptions-not-being-caught](https://community.arm.com/developer/tools-software/oss-platforms/f/gnu-toolchain-forum/11581/gcc-7-2-1-on-cortex-m4---c-exceptions-not-being-caught)
