如何发现未定义的行为 [英] How to spot undefined behavior

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

问题描述

有什么方法知道你的程序在C ++(甚至C)中是否有未定义的行为,没有记住整个规范?



我问的原因是我注意到了很多程序的案例在调试中工作,但不是发布由于未定义的行为。

解决方案

如果有一个工具至少帮助找到UB,那么这将是很好的,所以我们知道有潜在的问题。良好的编码标准。保护你免受自己。以下是一些想法:


  1. 代码必须 strong>无警告。 (换句话说,您的代码必须在设置为最高级别时,不要设置任何任何警告。)打开所有项目的警告标志错误。



    当你使用别人的图书馆时,这的确意味着一些额外的工作,因为他们可能没有这样做。

    总是使用 RAII


  2. 永远不要使用C风格的演员!从不!


  3. 如果你必须,那么你可能永远不会找到它们。 reinterpret_cast 或转换为 void ,然后使用包装器确保您始终投放到/从相同类型。换句话说,将你的指针/对象包装在 boost :: any 中,并将一个指针转换为你需要的指针,并在另一边做同样的。为什么?因为你总是知道 reinterpret_cast 的类型和 boost :: any 将强制你转换正确类型之后。

    始终 在声明时初始化变量(或


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



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



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


使您的结构易于正确使用并且难以正确使用



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

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. 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.

  2. Always use RAII.

  3. 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.

  4. 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.

  5. 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.

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天全站免登陆