Why do embedded platform developers continuosly attempt to remove usage C++ exceptions
from their SDKs
?
For example, Bada SDK
suggests the following workaround for the exception usage, which looks exceptionally ugly:
result MyApp::InitTimer() { result r = E_SUCCESS; _pTimer = new Timer; r = _pTimer->Construct(*this); if (IsFailed(r)) { goto CATCH; } _pTimer->Start(1000); if (IsFailed(r)) { goto CATCH; } return r; CATCH: return r; }
What are the reasons for this behavior?
As far as I know, ARM
compilers fully support C++ exceptions
and this couldn't actually be the matter. What else? Is the overhead of the exception usage and unwindings on ARM
platforms really that BIG to spend a lot time making such workarounds?
Maybe something else I'm not aware of?
Thank you.
It's an implementation detail for programming languages. E.g. C++ and Java have zero-cost exception handling.
So we clearly see there is an extra cost for exception handling that increases the deeper the stack trace goes. This is because when an exception is thrown the runtime needs to search up the stack until it hits a method than can handle it. The further it has to look up the stack, the more work it has to do.
It costs nothing on some implementations. All the cost is incurred when you throw an exception: that is, “normal code” is faster than code using error-return codes and tests. You incur cost only when you have an error.
"exceptions is always slow compared to other basic operations in the language, regardless of the programming language"... except in languages designed to compile use of exceptions into ordinary flow control.
Just my 2 cents...
I consult exclusively on embedded systems, most of them hard real-time and/or safety/life critical. Most of them run in 256K of flash/ROM or less - in other words, these are not "PC-like" VME bus systems with 1GB+ of RAM/flash and a 1GHz+ CPU. They are deeply embedded, somewhat resource-constrained systems.
I would say at least 75% of the products which use C++ disable exceptions at the compiler (i.e., code compiled with compiler switches that disable exceptions). I always ask why. Believe it or not, the most common answer is NOT the runtime or memory overhead / cost.
The answers are usually some mix of:
Also - there is often some nebulous uncertainty/fear about overhead, but almost always it's unquantified / unprofiled, it's just kind of thrown out there & taken at face value. I can show you reports / articles that state that the overhead of exceptions is 3%, 10%-15%, or ~30% - take your pick. People tend to quote the figure that forwards their own viewpoint. Almost always, the article is outdated, the platform/toolset is completely different, etc. so as Roddy says, you must measure yourself on your platform.
I'm not necessarily defending any of these positions, I'm just giving you real-world feedback / explanations I've heard from many firms working with C++ on embedded systems, since your question is "why do so many embedded developers avoid exceptions?"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With