嵌入式C ++:使用异常? [英] Embedded C++ : to use exceptions or not?

查看:239
本文介绍了嵌入式C ++:使用异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到这可能是主观的,所以会问一个具体的问题,但第一,背景:

I realize this may be subjective, so will ask a concrete question, but first, background:

我一直是一个嵌入式软件工程师, 3或2的OSI堆栈。我不是一个硬件的家伙。我通常总是做电信产品,通常是手/手机,这通常意味着像ARM 7处理器。

I have always been an embedded software engineer, but usually at Layer 3 or 2 of the OSI stack. I am not really a hardware guy. I have generally always done telecoms products, usually hand/cell-phones, which generally means something like an ARM 7 processor.

现在我发现自己在更通用的嵌入式世界,在一个小的初创公司,我可能会移动到不那么强大的处理器(有主观的一点) - 我不能预测哪个。

Now I find myself in a more generic embedded world, in a small start-up, where I might move to "not so powerful" processors (there's the subjective bit) - I cannot predict which.

关于在嵌入式系统中的C ++中的异常处理的讨论,没有明确的答案。有一些小的担心可移植性和一些关于运行时间,但它大多数似乎下降到代码大小(或我读错误的辩论。)

I have read quite a bit about debate about exception handling in C++ in embedded systems and there is no clear cut answer. There are some small worries about portability and a few about run-time, but it mostly seems to come down to code size (or am i reading the wrong debates?).

现在我必须决定是否使用或放弃异常处理 - 对于整个公司,永远(它会进入一些非常核心的/ W)。

Now I have to make the decision whether to use or forego exception handling - for the whole company, for ever (it's going into some very core s/w).

这可能听起来像一个字符串多长时间,但有人可能回答如果你的字符串是一个8051,那么不,如果,OTOH,它是...。

That may sound like "how long is a piece of string", but someone might reply "if your piece of string is an 8051, then don't. If, OTOH, it is ...".

我该跳哪条路?超级安全

Which way do I jump? Super-safe & lose a good feature, or exceptional code and maybe run into problems later?

推荐答案

在性能方面,我的理解是,例外情况实际上减小了代码的正常执行路径的大小并提高了性能,但是使异常/错误路径更昂贵。 (通常是很多更贵)。

In terms of performance, my understanding is that exceptions actually reduce the size and increase the performance of the normal execution paths of code, but make the exceptional/error paths more expensive. (often a lot more expensive).

所以如果你唯一关心的是性能,我会说稍后不要担心。如果今天的CPU可以处理它,那么明天也会。

So if your only concern is performance, I would say don't worry about later. If today's CPU can handle it, then tomorrows will as well.

。在我看来,异常是那些要求程序员比所有程序员都更聪明的特征之一。所以我说 - 如果你可以远离基于异常的代码。离开。

However. In my opinion, exceptions are one of those features that require programmers to be smarter all of the time than programmers can be reasonably be expected to be. So I say - if you can stay away from exception based code. Stay away.

查看Raymond Chen的清洁,更优雅,更难以识别。他说比我更好。

Have a look at Raymond Chen's Cleaner, more elegant, and harder to recognize. He says it better than I could.

这篇关于嵌入式C ++:使用异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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