限制由未定义行为引起的混淆? [英] Limit the confusion caused by undefined-behavior?

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

问题描述

正如我从我的阅读中理解的,未定义的行为是编译时留下编译器有几个不相同的选择的结果。然而,这不意味着,如果一个人遵循严格的编码实践(如把每个任务和每个平等在一个单独的语句,适当的调试和评论),那么它不应该在寻找未定义的源的重大问题-behavior。

As I understand from my reading, undefined-behavior is the result of leaving the compiler with several non-identical alternatives at compile time. However, wouldn't that mean that if one were to follow strict coding practice (like putting each assignment and each equality in a separate statement, proper debugging and commenting) then it shouldn't pose a significant problem in finding the source of the undefined-behavior.

此外,对于出现的每个错误,如果您识别代码,您应该知道在该特定语句中可以使用什么语句,正确?

Further, there are, for each error that comes up, if you identify the code, you should know what statements can be used in that particular statement's stead, correct?

编辑:我不感兴趣的地方,你写的代码,你不是想写。我感兴趣的例子,其中通过数学逻辑声音的代码无法工作。

I'm not interested in places where you have written code that you didn't mean to write. I'm interested in examples where code that is sound by mathematical logic fails to work.

此外,我认为良好的编码实践是强烈的信息性意见每几行,正确的缩进和定期调试转储。

Also, I consider 'good coding practice' to be strong informative comments every few lines, proper indentation, and debugging dumps on a regular basis.

推荐答案

未定义的行为不一定离开编译器有多个选择。

Undefined behavior isn't necessarily leaving the compiler with multiple alternatives. Most commonly it is simply doing something that doesn't make sense.

例如,取这个代码:

int arr[2];
arr[200] = 42;

这是未定义的行为。这不是编译器被给予多个选择。这只是我所做的没有意义。理想情况下,它不应该被允许在第一位,但没有潜在的昂贵的运行时检查,我们不能保证类似这样的东西不会发生在我们的代码。所以在C ++中,规则简单地说,语言只指定遵守规则的程序的行为。如果它在上面的例子中出现了错误,那么只需要未定义

this is undefined behavior. It's not that the compiler was given multiple alternatives to choose from. it's just that what I'm doing does not make sense. Ideally, it should not be allowed in the first place, but without potentially expensive runtime checking, we can't guarantee that something like this won't occur in our code. So in C++, the rule is simply that the language specifies only the behavior of a program that sticks to the rules. If it does something erroneous like in the above example, it is simply undefined what should happen.

现在,检测此错误。它如何表面?它可能永远不会似乎导致任何问题。也许我们只是这样碰巧写入到映射到进程的内存(所以我们没有得到访问冲突),但从来没有使用过(所以没有其他部分的程序将读取我们的垃圾值,或覆盖我们写的)。然后看起来这个程序是没有bug的,工作正常。

Now, imagine how you're going to detect this error. How is it going to surface? It might never seem to cause any problems. Perhaps we just so happen to write into memory that's mapped to the process (so we don't get an access violation), but is never otherwise used (so no other part of the program will read our garbage value, or overwrite what we wrote). Then it'll seem like the program is bug-free and works just fine.

或者它可能会碰到一个甚至没有映射到我们的进程的地址。然后程序会立即崩溃。

Or it might hit an address that's not even mapped to our process. Then the program will crash immediately.

或者它可能会遇到映射到我们的进程的地址,但稍后将在某些时候使用。然后我们知道的是,迟早,从该地址读取的函数将获得一个意想不到的值,它会表现得很奇怪。这部分很容易在调试器中发现,但它不会告诉我们何时或从 写入垃圾值。因此,没有简单的方法来将错误追溯到其来源。

Or it might hit an address that's mapped to our process, but at some point later will be used for something. Then all we know is that sooner or later, the function reading from that address will get an unexpected value, and it'll behave weird. That part is easy to spot in the debugger, but it doesn't tell us anything about when or from where that garbage value was written. So there's no simple way to trace the error back to its source.

这篇关于限制由未定义行为引起的混淆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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