C ++程序员应该知道的所有常见的未定义的行为是什么? [英] What are all the common undefined behaviours that a C++ programmer should know about?

查看:115
本文介绍了C ++程序员应该知道的所有常见的未定义的行为是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++程序员应该知道的所有常见的未定义的行为是什么?



说:

  a [i] = i ++; 

解决方案>

Pointer




  • 取消引用 NULL 指针

  • 使用指向其生命周期已结束的对象的指针(例如,堆栈分配的对象或删除的对象)
  • li>
  • 解除尚未明确初始化的指针

  • 执行指针运算,产生数组外边界(上方或下方)的结果。

  • 将指针指向超出数组末尾的位置。

  • 将指针转换为不兼容类型的对象

  • 使用 memcpy 复制重叠缓冲区



缓冲区溢出




  • 读取或写入对象或数组,其偏移量为负数或超出该对象的大小(堆栈/堆溢出)。



整数溢出




  • 有符号整数溢出

  • 评估未经数学定义的表达式

  • 将值左移一个负值(向右移动负值,实现已定义)

  • 将值移动大于或等于数字位数的值 int64_t i = 1;

    未定义)



类型,Cast和Const




  • 将数值投射到目标类型无法表示的值中(直接或通过static_cast)

  • 在明确分配之前使用自动变量(例如, int i; i ++; cout

  • 在接收到信号时使用 volatile sig_atomic_t 以外的任何类型的对象的值

  • 尝试在其生命周期内修改字符串文字或任何其他const对象

  • 在预处理期间将一个窄字符串与一个宽字符串文字连接起来



函数和模板




  • 不从返回值返回值

  • 对同一个实体(类,模板,枚举,内联函数,静态成员函数等)的多个不同的定义
  • li>
  • 模板实例化中的无限递归

  • 使用不同的参数或者链接到函数定义为使用的参数和链接来调用函数。 li>


OOP




  • 存储持续时间

  • 分配给部分重叠对象的结果

  • 在静态对象初始化期间递归重新输入函数

  • 从其构造函数或析构函数对虚拟函数调用对象的纯虚函数

  • 引用尚未构建或已被破坏的对象的非静态成员



源文件和预处理




  • - 不以换行符结尾或以反斜杠结尾(C ++ 11之前)的空源文件

  • 反斜杠后跟一个不属于指定

  • 超出实现限制(嵌套块的数量,程序中的函数数量,可用的堆栈)空格...)

  • 无法用 long int

  • 表示的预处理器数值
  • 在函数式宏定义左侧的预处理指令

  • #if 表达式中动态生成定义的标记



待分类




  • 在具有静态存储持续时间


的程序销毁期间

What are all the common undefined behaviours that a C++ programmer should know about?

Say, like:

a[i] = i++;

解决方案

Pointer

  • Dereferencing a NULL pointer
  • Dereferencing a pointer returned by a "new" allocation of size zero
  • Using pointers to objects whose lifetime has ended (for instance, stack allocated objects or deleted objects)
  • Dereferencing a pointer that has not yet been definitely initialized
  • Performing pointer arithmetic that yields a result outside the boundaries (either above or below) of an array.
  • Dereferencing the pointer at a location beyond the end of an array.
  • Converting pointers to objects of incompatible types
  • Using memcpy to copy overlapping buffers.

Buffer overflows

  • Reading or writing to an object or array at an offset that is negative, or beyond the size of that object (stack/heap overflow)

Integer Overflows

  • Signed integer overflow
  • Evaluating an expression that is not mathematically defined
  • Left-shifting values by a negative amount (right shifts by negative amounts are implementation defined)
  • Shifting values by an amount greater than or equal to the number of bits in the number (e.g. int64_t i = 1; i <<= 72 is undefined)

Types, Cast and Const

  • Casting a numeric value into a value that can't be represented by the target type (either directly or via static_cast)
  • Using an automatic variable before it has been definitely assigned (e.g., int i; i++; cout << i;)
  • Using the value of any object of type other than volatile or sig_atomic_t at the receipt of a signal
  • Attempting to modify a string literal or any other const object during its lifetime
  • Concatenating a narrow with a wide string literal during preprocessing

Function and Template

  • Not returning a value from a value-returning function (directly or by flowing off from a try-block)
  • Multiple different definitions for the same entity (class, template, enumeration, inline function, static member function, etc.)
  • Infinite recursion in the instantiation of templates
  • Calling a function using different parameters or linkage to the parameters and linkage that the function is defined as using.

OOP

  • Cascading destructions of objects with static storage duration
  • The result of assigning to partially overlapping objects
  • Recursively re-entering a function during the initialization of its static objects
  • Making virtual function calls to pure virtual functions of an object from its constructor or destructor
  • Referring to nonstatic members of objects that have not been constructed or have already been destructed

Source file and Preprocessing

  • A non-empty source file that doesn't end with a newline, or ends with a backslash (prior to C++11)
  • A backslash followed by a character that is not part of the specified escape codes in a character or string constant (this is implementation-defined in C++11).
  • Exceeding implementation limits (number of nested blocks, number of functions in a program, available stack space ...)
  • Preprocessor numeric values that can't be represented by a long int
  • Preprocessing directive on the left side of a function-like macro definition
  • Dynamically generating the defined token in a #if expression

To be classified

  • Calling exit during the destruction of a program with static storage duration

这篇关于C ++程序员应该知道的所有常见的未定义的行为是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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