C ++异常处理添加了多少脚本 [英] How much footprint does C++ exception handling add

查看:144
本文介绍了C ++异常处理添加了多少脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题对于嵌入式开发尤为重要。异常处理为生成的二进制输出增加了一些空间。另一方面,没有例外,错误需要以其他方式处理,这需要额外的代码,最终也会增加二进制大小。

This issue is important especially for embedded development. Exception handling adds some footprint to generated binary output. On the other hand, without exceptions the errors need to be handled some other way, which requires additional code, which eventually also increases binary size.

我对你的感兴趣经验,特别是:

I'm interested in your experiences, especially:


  1. 您的编译器为异常处理添加的平均占用量是多少?
  2. 在二进制输出大小方面,异常处理是否比其他错误处理策略更昂贵(很多人说)?

  3. 建议嵌入式开发?

请将我的问题仅作为指导。欢迎任何输入。

Please take my questions only as guidance. Any input is welcome.

补充:任何人都有一个具体的方法/脚本/工具,对于特定的C ++对象/可执行文件,将显示加载的百分比

Addendum: Does any one have a concrete method/script/tool that, for a specific C++ object/executable, will show the percentage of the loaded memory footprint that is occupied by compiler-generated code and data structures dedicated to exception handling?

推荐答案

发生异常时发生异常会有时间开销,这取决于您如何实现异常处理。但是,作为轶事,应该导致异常的事件的严重性将需要同样多的时间来处理使用任何其他方法。为什么不使用高度支持的基于语言的方法来处理这些问题?

When an exception occurs there will be time overhead which depends on how you implement your exception handling. But, being anecdotal, the severity of an event that should cause an exception will take just as much time to handle using any other method. Why not use the highly supported language based method of dealing with such problems?

GNU C ++编译器默认使用零成本模型,即没有时间开销

The GNU C++ compiler uses the zero–cost model by default i.e. there is no time overhead when exceptions don't occur.


由于关于异常处理代码和局部对象偏移量的信息可以在编译时计算一次,因此这些信息可以保存在与每个功能相关联的单个位置中,但不在每个ARI中。您基本上从每个ARI中删除异常开销,从而避免额外的时间将它们推送到堆栈。这种方法称为异常处理的零成本模型,前面提到的优化存储称为影子堆栈。 - Bruce Eckel,C ++ Volume 2中的思考

Since information about exception-handling code and the offsets of local objects can be computed once at compile time, such information can be kept in a single place associated with each function, but not in each ARI. You essentially remove exception overhead from each ARI and thus avoid the extra time to push them onto the stack. This approach is called the zero-cost model of exception handling, and the optimized storage mentioned earlier is known as the shadow stack. - Bruce Eckel, Thinking in C++ Volume 2

复杂度开销的大小难以量化,但Eckel平均为5和15百分。这将取决于您的异常处理代码的大小与应用程序代码的大小的比率。如果你的程序很小,那么异常将是二进制文件的很大一部分。如果你使用的是零成本模型,比异常需要更多的空间来消除时间开销,所以如果你关心空间而不是时间比不使用零成本编译。

The size complexity overhead isn't easily quantifiable but Eckel states an average of 5 and 15 percent. This will depend on the size of your exception handling code in ratio to the size of your application code. If your program is small then exceptions will be a large part of the binary. If you are using a zero–cost model than exceptions will take more space to remove the time overhead, so if you care about space and not time than don't use zero-cost compilation.

我的意见是大多数嵌入式系统有足够的内存,如果你的系统有一个C ++编译器,你有足够的空间来包括异常。我的项目使用的PC / 104计算机有几GB的辅助存储器,512 MB的主存储器,因此没有空间问题的例外 - 虽然,我们的微控制器在C编程。我的启发是如果有一个主流的C ++编译器它,使用异常,否则使用C。

My opinion is that most embedded systems have plenty of memory to the extent that if your system has a C++ compiler you have enough space to include exceptions. The PC/104 computer that my project uses has several GB of secondary memory, 512 MB of main memory, hence no space problem for exceptions - though, our micorcontrollers are programmed in C. My heuristic is "if there is a mainstream C++ compiler for it, use exceptions, otherwise use C".

这篇关于C ++异常处理添加了多少脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆