是未定义的行为值得吗? [英] Is undefined behavior worth it?

查看:106
本文介绍了是未定义的行为值得吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多不好的事情发生了,并且由于未定义的行为继续发生(或不知道,任何事情都可能发生)。我理解这是为了留下一些摆动的空间为编译器优化,也许也使C ++更容易移植到不同的平台和架构。然而,由未定义的行为引起的问题似乎太大了,不能通过这些参数来证明。未定义行为的其他参数是什么?如果没有,为什么未定义的行为仍然存在?



编辑为我的问题添加一些动机: C ++ - crafty co-workers我已经习惯了使我的代码尽可能安全。断言每个参数,严格的const正确性和这样的东西。我试图离开,因为小房间有可能使用我的代码的错误的方式,因为经验表明,如果有漏洞,人们会使用他们,然后他们会打电话给我的代码是坏的。我认为让我的代码尽可能安全一个好的做法。这就是为什么我不明白为什么不确定的行为存在。有人可以给我一个未定义的行为的例子,在运行时或编译时无法检测到没有相当大的开销?

解决方案

关注的核心来自C / C ++的速度哲学。



这些语言是在原始权力稀疏的时候创建的,你需要获得所有



指定如何处理UB意味着首先检测它,然后指定适当的处理。但是检测它是违反语言的速度第一哲学!



今天,我们还需要快速程序吗?是的,对于我们工作的资源非常有限(嵌入式系统)或非常苛刻的约束(每秒的响应时间或事务),我们确实需要尽可能多地挤出。



我知道座右铭在问题上抛出更多的硬件。我们有一个我在工作的应用程序:




  • 小于100ms,数据库调用在中间(比如感谢memcached)。

  • 每秒的事务数?



它运行约40个怪物:8双核opteron(2800MHz),32GB随机存取存储器。在这一点上,很难用更多的硬件更快,所以我们需要优化的代码和一种允许它的语言(我们没有在那里抛出汇编代码)。



我必须说,我不在乎UB。如果你得到你的程序调用UB的点,那么它需要修复任何实际发生的行为。当然,如果立即报告这些问题会更容易解决:这是调试版本。



因此,也许不是专注于UB,请使用以下语言:




  • 不使用未选中的通话



$ b p>一切都突然更好:)


Many bad things happened and continue to happen (or not, who knows, anything can happen) due to undefined behavior. I understand that this was introduced to leave some wiggle-room for compilers to optimize, and maybe also to make C++ easier to port to different platforms and architectures. However the problems caused by undefined behavior seem to be too large to be justified by these arguments. What are other arguments for undefined behavior? If there are none, why does undefined behavior still exist?

Edit To add some motivation for my question: Due to several bad experiences with less C++-crafty co-workers I have gotten used to making my code as safe as possible. Assert every argument, rigorous const-correctness and stuff like that. I try to leave as little room has possible to use my code the wrong way, because experience shows that, if there are loopholes, people will use them, and then they will call me about my code being bad. I consider making my code as safe as possible a good practice. This is why I do not understand why undefined behavior exists. Can someone please give me an example of undefined behavior that cannot be detected at runtime or compile time without considerable overhead?

解决方案

I think the heart of the concern comes from the C/C++ philosophy of speed above all.

These languages were created at a time when raw power was sparse and you needed to get all the optimizations you could just to have something usable.

Specifying how to deal with UB would mean detecting it in the first place and then of course specifying the handling proper. However detecting it is against the speed first philosophy of the languages!

Today, do we still need fast programs ? Yes, for those of us working either with very limited resources (embedded systems) or with very harsh constraints (on response time or transactions per second), we do need to squeeze out as much as we can.

I know the motto throw more hardware at the problem. We have an application where I work:

  • expected time for an answer ? Less than 100ms, with DB calls in the midst (say thanks to memcached).
  • number of transactions per second ? 1200 in average, peaks at 1500/1700.

It runs on about 40 monsters: 8 dual core opteron (2800MHz) with 32GB of RAM. It gets difficult to be "faster" with more hardware at this point, so we need optimized code, and a language that allows it (we did restrain to throw assembly code in there).

I must say that I don't care much for UB anyway. If you get to the point that your program invokes UB then it needs fixing whatever the behavior that actually occurred. Of course it would be easier to fix them if it was reported straight away: that's what debug builds are for.

So perhaps that instead of focusing on UB we should learn to use the language:

  • don't use unchecked calls
  • (for experts) don't use unchecked calls
  • (for gurus) are you sure you really need an unchecked call here ?

And everything is suddenly better :)

这篇关于是未定义的行为值得吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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