C++ IB & UB

● **IB: Implementation-defined Behaviour. **The standard leaves it up to the particular compiler/platform to define the precise behaviour, but requires that it be defined.

○ Using implementation-defined behaviour can be useful, but makes your code less portable.

● **UB: Unspecified Behavior. **behavior, for a well-formed program construct and correct data, that depends on the implementation.

○ Note: The implementation is not required to document which behavior occurs.

● **UB: Undefined Behaviour. **The standard does not specify how a program invoking undefined behaviour should behave. Also known as "nasal demons" because theoretically it could make demons fly out of your nose.

○ Using undefined behaviour is nearly always a bad idea. Even if it seems to work sometimes, any change to environment, compiler or platform can randomly break your code.

IB: Implementation-defined Behaviour

  • the exact size of fundamental types such as int and long
  • definition of the NULL macro (possibly 0, ((void*)0), or nullptr)
  • conversions between pointers and integers (reinterpret_caststd::uintptr_t(...))
  • nesting limit for #include directives
  • choice of larger or smaller value for float literals (round up or down if necessary)
  • operating systems on which implementation depends
  • value of pow(0,0) (usually 1)

UB: Unspecified Behavior

  • whether all string literals are distinct (they might have different addresses)
  • the initial value of storage allocated by new (often zero when getting new memory from the OS)
  • evaluation order of operands of some operators (for A + B, B might be evaluated before A)
  • amount of time it takes for a thread to make progress (but eventual progress is guaranteed)
  • whether references require any storage (they usually do, just like pointers)

UB: Undefined Behaviour

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
  • Calling exit during the destruction of a program with static storage duration

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
相关推荐
cdut_suye10 分钟前
Linux工具使用指南:从apt管理、gcc编译到makefile构建与gdb调试
java·linux·运维·服务器·c++·人工智能·python
波音彬要多做34 分钟前
41 stack类与queue类
开发语言·数据结构·c++·学习·算法
捕鲸叉35 分钟前
C++软件设计模式之外观(Facade)模式
c++·设计模式·外观模式
只做开心事2 小时前
C++之红黑树模拟实现
开发语言·c++
程序员老冯头3 小时前
第十五章 C++ 数组
开发语言·c++·算法
程序猿会指北4 小时前
【鸿蒙(HarmonyOS)性能优化指南】启动分析工具Launch Profiler
c++·性能优化·harmonyos·openharmony·arkui·启动优化·鸿蒙开发
无 证明8 小时前
new 分配空间;引用
数据结构·c++
别NULL12 小时前
机试题——疯长的草
数据结构·c++·算法
CYBEREXP200813 小时前
MacOS M3源代码编译Qt6.8.1
c++·qt·macos
yuanbenshidiaos13 小时前
c++------------------函数
开发语言·c++