如何识别未定义行为 [英] How to spot undefined behavior

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

问题描述

有没有什么办法++知道你是否程序有不确定的用C行为(甚至C),短期记忆整个规范的?

Is there any way to know if you program has undefined behavior in C++ (or even C), short of memorizing the entire spec?

我想问的原因是,我注意到很多程序案件调试工作但没有释放是由于未定义行为。这将是很好,如果有一个工具,至少可以帮助现货UB,所以我们知道有潜在的问题。

The reason I ask is that I've noticed a lot of cases of programs working in debug but not release being due to undefined behavior. It would be nice if there were a tool to at least help spot UB, so we know there's the potential for problems.

推荐答案

良好的编码标准。从自己保护你。这里有一些想法:

Good coding standards. Protect you from yourself. Here are some ideas:


  1. 在code的必须的编译在最高级别的警告... 无警告。 (换句话说,不能衬托你的code的任何的警告,当设置为最高级别的。)打开错误的所有项目的警告标志。

  1. The code must compile at the highest warning level... without warnings. (In other words, your code must not set off any warnings at all when set to the highest level.) Turn on the error on warning flag for all projects.

当您使用其他人的图书馆,因为他们可能没有这样做这是否意味着一些额外的工作。你还会发现有一些警告,这是毫无意义的...关闭它们分别为您的团队决定。

This does mean some extra work when you use other peoples' libraries since they may not have done this. You will also find there are some warnings which are pointless... turn those off individually as your team decides.

总是的使用 RAII

从不使用C语言风格转换!从来没有! - 我觉得有像情侣罕见的情况下,当你必须打破这个,但你可能永远不会发现它们

Never use C style casts! Never! - I think there's like a couple rare cases when you have to break this but you will probably never find them.

如果您必须 reinter pret_cast 或转换为无效然后使用一个包装,使确保你总是铸造/来自同一类型。换句话说,包住指针/对象在的boost ::任何投出指针它变成你需要什么,并在另一侧做同样的。为什么?因为你将永远知道什么类型 reinter pret_cast 从和的boost ::任何将实施您'已经转换为正确的类型之后。这是你可以得到最安全的。

If you must reinterpret_cast or cast to void then use a wrapper to make sure you're always casting to/from the same type. In other words, wrap your pointer/object in a boost::any and cast a pointer to it into whatever you need and on the other side do the same. Why? Because you will always know what type to reinterpret_cast from and the boost::any will enforce that you've cast to the correct type after that. It's the safest you can get.

总是是在声明的时候初始化变量(或者在构造函数初始化一个类时)。

Always initialize your variables at the point of declaration (or in constructor initializers when in a class).

有更多,但这些都是开始与一些非常重要的。

There are more but those are some very important ones to start with.

没人能记住的标准。我们中级到高级C ++程序员做的是,我们知道是安全和保护自己从我们的人性结构的使用......我们不使用并不安全,除非我们要构造,然后我们格外小心,以确保在被测试到地狱和背部一个不错的安全接口危险是所有包裹起来。

Nobody can memorize the standard. What we intermediate to advanced C++ programmers do is use constructs we know are safe and protect ourselves from our human nature... and we don't use constructs that are not safe unless we have to and then we take extra care to make sure the danger is all wrapped up in a nice safe interface that is tested to hell and back.

要记住这是在所有语言的通用一个重要的事情是:

One important thing to remember which is universal across all languages is to:

让你的结构易于正确使用,而难以错误使用

make your constructs easy to use correctly and difficult to use incorrectly

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

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