Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Embedded C++ compilers not support exceptions?

I was writing a library to embedded systems and I bumped into no STL standard library easy found.

But the worst news I receive is no exception support by compiler. Atmel reference manual show this

Why not support exceptions on embedded environment?

It's simple make impossible to use a lot of libraries written in C++. C++ is closely linked with exceptions, like with the new operator!

like image 879
David Kennedy Avatar asked Dec 05 '22 19:12

David Kennedy


2 Answers

Obviously, nobody but the people that produce that compiler can really answer that question.

My best guess is that exceptions are both space- and time-consuming (time is only a real problem when the code throws, but space is needed for the unwind tables always, and can easily be about the same size as the overall code) for the compiled code, coupled with "rather difficult to implement", it's perhaps not the highest item on the list for the compiler developers, and thus not yet implemented. Whether it WILL be at some point in the future is obviously up to Atmel or whoever they subcontract to make their compiler.

I'm not an expert on Atmel C++ implementation, but I would be VERY surprised if the compiler supports throw, but not try/catch - since that would be about as useful as a screwdriver made from chocolate, when trying to fix the heater in a sauna that is stuck on full heat.

If you use the -fno-exceptions, the compiler should error if you have any throw in the code. And the STL can be compiled with -fno-exceptions - as that's how I compile my compiler code.

like image 176
Mats Petersson Avatar answered Dec 08 '22 08:12

Mats Petersson


The 8 bit AtmelAVR with its limited ROM and RAM resources is not suited to the use of the STL which imposes a considerable load on both. Moreover C++ exceptions are intrinsically non-deterministic and therefore unsuited to many real-time applications.

A compiler may implement the EC++ (Embedded C++) "standard". The aim of EC++ was two fold:

  • to support the use of C++ in embedded systems at a time before ISO standardisation of the language, by restricting the language to a common subset available on all compilers.
  • to avoid non-deterministic (in both memory and timing) language/library features unsuited to hard-real-time applications.

Things missing from EC++ include:

  • namespace
  • templates
  • RTTI
  • exceptions

The omission of namespaces is entirely down to the first aim of EC++ and now essentially obsolete. The omission of the others are justified by both aims, and preclude the use of the STL and std::string libraries.

EC++ itself is now largely obsolete, but the subset it defines is nonetheless applicable to severely resource constrained targets, and many compilers support whole or partial EC++ enforcement.

Note that perhaps not all compilers for AVR impose these restrictions, but if you attempt to use these features extensively you will probably soon discover whey they are ill advised in most cases on targets very limited CPU and memory resources.

With respect to the use of the new operator, default dynamic memory allocation (as opposed to placement new or an override), is intrinsically non-deterministic and often best avoided in real-time embedded systems, and especially so where minimal heap is available. To use new without the assumption of exception handling, use new (std::nothrow) (#include <new>) which will not throw an exception but return a null pointer like malloc(). In EC++ new and new (std::nothrow) are essentially the same thing, but the latter is portable and explicit.

Lack of support for C++ in Atmel's maintained open-source library for GCC-AVR does not mean that there is no support for embedded systems in general, or indeed for AVR. IAR's AVR compiler supports what it refers to as Extended Embedded C++ (EEC++) as well as EC++. EEC++ does support a C++ library including STL with some modifications - no RTTI, no exceptions, and not in std:: namespace (although namespaces are supported). Support for ISO C++ as opposed to EC++ on most 32 bit targets is generally more comprehensive.

like image 23
Clifford Avatar answered Dec 08 '22 09:12

Clifford