“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. sublime3 多行编辑.摘抄

    Sublime text 3是一个非常强大的网站编辑工具. 这里小云深深的被它的快速编辑多行内容功能所吸引. 先说下,使用下面的功能要安装一个叫emmet的插件.没有的话,自行度娘吧. 下面就来看下具 ...

  2. Windows10(uwp)开发中的侧滑

    还是在持续的开发一款Windows10的应用中,除了上篇博客讲讲我在Windows10(uwp)开发中遇到的一些坑,其实还有很多不完善的地方,比如(UIElement.Foreground).(Gra ...

  3. 2018.09.30 bzoj2223: [Coci 2009]PATULJCI(主席树)

    传送门 主席树经典题目. 直接利用主席树差分的思想判断区间中数的个数是否合法然后决定左走右走就行了. 实际上跟bzoj3524是同一道题. 代码: #include<bits/stdc++.h& ...

  4. 用org.mybatis.generator 生成代码

    1:引入pom 2:增加生成配置xml: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...

  5. Python+Android开发

    1 下载Scripting Layer for Android (SL4A) Scripting Layer for Android (SL4A) 是一个开源项目,目标是为android系统提供脚本语 ...

  6. nexus 组件下载和上传

    一. 重写 super pom 修改 maven 的 settings.xml Configuring Maven to Use a Single Repository Group <setti ...

  7. Codeforces735A Ostap and Grasshopper 2016-12-13 11:53 78人阅读 评论(0) 收藏

    A. Ostap and Grasshopper time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  8. STL中的algorithm

    STL中的algorithm #include<algorithm>中的泛函算法,需要添加头文件. 搜索算法:find() .search() .count() .find_if() .s ...

  9. Win7_Ultimate + VS2010 + openGL 配置

    Win7_Ultimate + VS2010 + openGL 配置 0. 前言 OpenGL作为当前主流的图形API之一,它在一些场合具有比DirectX更优越的特性. (1)与C语言紧密结合. O ...

  10. easyui-combobox url绑定后台json数据问题

    <input id="line" name="line" style="max-width:120px;" class="e ...