How much footprint does C++ exception handling add

后端 未结 8 1238
有刺的猬
有刺的猬 2020-11-28 05:50

This issue is important especially for embedded development. Exception handling adds some footprint to generated binary output. On the other hand, without exceptions the err

8条回答
  •  我在风中等你
    2020-11-28 06:01

    In my opinion exception handling is not something that's generally acceptable for embedded development.

    Neither GCC nor Microsoft have "zero-overhead" exception handling. Both compilers insert prologue and epilogue statements into each function that track the scope of execution. This leads to a measurable increase in performance and memory footprint.

    The performance difference is something like 10% in my experience, which for my area of work (realtime graphics) is a huge amount. The memory overhead was far less but still significant - I can't remember the figure off-hand but with GCC/MSVC it's easy to compile your program both ways and measure the difference.

    I've seen some people talk about exception handling as an "only if you use it" cost. Based on what I've observed this just isn't true. When you enable exception handling it affects all code, whether a code path can throw exceptions or not (which makes total sense when you consider how a compiler works).

    I would also stay away from RTTI for embedded development, although we do use it in debug builds to sanity check downcasting results.

提交回复
热议问题