“undefined behavior:
behavior for which this International Standard imposes no requirements.”

example of Undefined Behavior

Use of an uninitialized variable

Misaligned pointers

Access to an object past end of lifetime

The Compiler and Undefined Behavior

为了提高效率,编译器有做了一些假设,如下是几个例子。

编译器不同优化会引起不同的结果

优化1,先去掉多余的 NULL 检查,再去掉多余的语句

优化2,先去掉多余的语句,再去掉多余的 NULL 检查

这样子,仅仅是不同的优化顺序,最终得到的结果是不同的。

此外,还有不同的优化等级、不同的设备、不同的编译器版本等。

Undefined Behavior 的特征

  1. 不可预测
  2. 可能影响整个程序
  3. 会导致潜伏的 bug
  4. 是很多安全问题的原因

如何解决

工具篇

  • Compiler
  • Static Analyzer
  • Address Sanitizer
  • Thread Sanitizer
  • Undefined Behavior Sanitizer

tips

  • Modernize your project (Editor → Validate Settings)

  • Run the Static Analyzer
    把开关打开。

  • Use the Runtime Sanitizers
    不同的工具可以解决不同的问题。

参考

Understanding Undefined Behavior的更多相关文章

  1. UISearchController Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior

    Attempting to load the view of a view controller while it is deallocating is not allowed and may res ...

  2. 【转载】C语言中的undefined behavior/unspecified behavior - 序

    嗷嗷的话: 这都是一些细枝末节的东西,我想不做编译器的话,大部分都很难碰到.研究学习这些只是出于对C语言一种偏执狂. 写出来是为了找到和我一样的偏执狂. 在随后的的文章中,首先我写一写191种unde ...

  3. 情景剧:C/C++中的未定义行为(undefined behavior)

    写在前面 本文尝试以情景剧的方式,轻松.直观地解释C/C++中未定义行为(undefined behavior)的概念.设计动机.优缺点等内容1,希望读者能够通过阅读本文,对undefined beh ...

  4. C++ Undefined Behavior 详细列表

    Undefined Behavior,即未定义的行为,指程序不可预测的执行效果,一般由错误的代码实现引起.出于效率.兼容性等多方面原因,语言标准不便于定义错误程序的明确行为,而是将其统称为" ...

  5. About Undefined Behavior[译文]

    原文:blog.llvm.org/2011/05/what-every-c-programmer-should-know.html 人们偶尔会问为什么LLVM的汇编代码有时会在优化器打开时产生SIGT ...

  6. C语言中的undefined behavior

    参考: http://www.cnblogs.com/aoaoblogs/archive/2010/08/31/1813982.html

  7. IAR运行程序警告:undefined behavior: the order of volatile accesses is undefined in this statement

    运算符两边都是volatile变量的警告,将IAR设置如下即可:

  8. Understanding C++ Modules In C++20 (1)

    Compiling evironment: linux (ubuntu 16.04)+ gcc-10.2. The Post will clarify and discuss what modules ...

  9. WARNING: Calls to any function that may require a gradient calculation inside a conditional block may return undefined results

    GLES2.0: Some device will give a warning on compling shaders(yet the compling will succeed), and the ...

随机推荐

  1. word2vec相关资源

    word2vec官网:https://code.google.com/p/word2vec/ 利用中文数据跑Google开源项目word2vec:http://www.cnblogs.com/hebi ...

  2. Java知识系列 -- 反射

    原理 要想理解 Java 反射,首先要弄清类的加载过程. 比如这行代码 Person p = new Person();. 我们想要创建一个 Person 对象,并用 p 作为对象的引用. 在 Jav ...

  3. 博客停更转战简书http://www.jianshu.com/u/7ac4047c9cfa

    博客停更转战简书 http://www.jianshu.com/u/7ac4047c9cfa

  4. s28 LNMP架构服务搭建

    nginx-location使用 location语法 location使用的语法例子为: location [=|~|~*|^~] uri{ 对location语法列表说明. |1ocation | ...

  5. html转jsp部分css不可用

    解决方法 <%String path = request.getContextPath();String basePath = request.getScheme()+"://&quo ...

  6. CIC仿真

    在调用CIC时发现的,明确告诉调用那些库. CIC的设置窗口. 该CIC滤波器有两个输入一个输出.与下面的相比,同样要输出两路信号,但输出速率要高. 修改成以下的设置 设置一个接口,两个通道. 该设置 ...

  7. OpenGL中位图的操作(glReadPixels,glDrawPixels等)

    OpenGL中位图的操作 OpenGL中位图的操作(glReadPixels,glDrawPixels和glCopyPixels应用举例). 1. BMP文件格式简单介绍 BMP文件是一种像素文件,它 ...

  8. follow me

    IM InfoSphere Information Server for Data Integration Fundamentals Bootcamp 7月15日 5 北京 IM InfoSphere ...

  9. Java IO流详尽解析(转)

    流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

  10. 四则运算 Java 实现 刘丰璨,王翠鸾

    四则运算 GitHub仓库 功能实现 [x] 使用 -n 参数控制生成题目的个数,并且根据解空间限制用户设定的范围(如 range == 2 时,用户却要求生成 10000 道题目,这明显不合理) [ ...