在C和c++中,什么是未定义的行为(UB) ?未指定的行为和实现定义的行为呢?它们之间的区别是什么?
当前回答
未定义的行为是丑陋的——比如“好的、坏的和丑陋的”。
好:一个能够编译并正常工作的程序。
坏的:有错误的程序,这种错误编译器可以检测到并报错。
丑陋的:一个程序有一个错误,编译器不能检测和警告,这意味着程序编译,可能在某些时候正确工作,但也在某些时候奇怪地失败。这就是未定义行为。
一些程序语言和其他正式系统努力限制“不确定性的鸿沟”——也就是说,它们试图安排事情,使大多数或所有程序不是“好”就是“坏”,只有极少数程序是“丑”的。然而,这是C语言的一个特征,它的“不确定性的鸿沟”相当宽。
其他回答
未定义行为vs.未指定行为有一个简短的描述。
他们最后的总结是:
总而言之,未指定的行为通常是不应该发生的 担心,除非你的软件被要求是可移植的。 相反,未定义的行为总是不受欢迎的,也不应该这样做 发生。
好吧,这基本上是直接从标准复制粘贴
3.4.1 1 implementation-defined behavior unspecified behavior where each implementation documents how the choice is made 2 EXAMPLE An example of implementation-defined behavior is the propagation of the high-order bit when a signed integer is shifted right. 3.4.3 1 undefined behavior behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements 2 NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). 3 EXAMPLE An example of undefined behavior is the behavior on integer overflow. 3.4.4 1 unspecified behavior use of an unspecified value, or other behavior where this International Standard provides two or more possibilities and imposes no further requirements on which is chosen in any instance 2 EXAMPLE An example of unspecified behavior is the order in which the arguments to a function are evaluated.
未定义的行为是丑陋的——比如“好的、坏的和丑陋的”。
好:一个能够编译并正常工作的程序。
坏的:有错误的程序,这种错误编译器可以检测到并报错。
丑陋的:一个程序有一个错误,编译器不能检测和警告,这意味着程序编译,可能在某些时候正确工作,但也在某些时候奇怪地失败。这就是未定义行为。
一些程序语言和其他正式系统努力限制“不确定性的鸿沟”——也就是说,它们试图安排事情,使大多数或所有程序不是“好”就是“坏”,只有极少数程序是“丑”的。然而,这是C语言的一个特征,它的“不确定性的鸿沟”相当宽。
来自官方的C基本原理文档
The terms unspecified behavior, undefined behavior, and implementation-defined behavior are used to categorize the result of writing programs whose properties the Standard does not, or cannot, completely describe. The goal of adopting this categorization is to allow a certain variety among implementations which permits quality of implementation to be an active force in the marketplace as well as to allow certain popular extensions, without removing the cachet of conformance to the Standard. Appendix F to the Standard catalogs those behaviors which fall into one of these three categories. Unspecified behavior gives the implementor some latitude in translating programs. This latitude does not extend as far as failing to translate the program. Undefined behavior gives the implementor license not to catch certain program errors that are difficult to diagnose. It also identifies areas of possible conforming language extension: the implementor may augment the language by providing a definition of the officially undefined behavior. Implementation-defined behavior gives an implementor the freedom to choose the appropriate approach, but requires that this choice be explained to the user. Behaviors designated as implementation-defined are generally those in which a user could make meaningful coding decisions based on the implementation definition. Implementors should bear in mind this criterion when deciding how extensive an implementation definition ought to be. As with unspecified behavior, simply failing to translate the source containing the implementation-defined behavior is not an adequate response.
实现定义的,
实现者希望,应该有良好的文档,标准给出了选择,但一定要编译
未指定的,
与实现定义的相同,但没有文档化
未定义的,
不管发生什么事,都要小心。
推荐文章
- 为什么STL如此严重地基于模板而不是继承?
- 查找当前可执行文件的路径,不包含/proc/self/exe
- 未定义对静态constexpr char的引用[]
- 在c++中,restrict关键字是什么意思?
- 如何创建可变参数宏(参数数量可变)
- c++中类似于java的instanceof
- include_directories和target_include_directories在CMake中的区别是什么?
- std::make_pair与std::pair的构造函数的目的是什么?
- 如何追加一个字符到std::字符串?
- 为什么C语言这么快,为什么其他语言没有这么快或更快?
- 转发C中可变函数的调用
- 为什么要在c++中使用嵌套类?
- 如何处理11000行c++源文件?
- 使用g++编译多个.cpp和.h文件
- 当尝试用const初始化变量时,错误“初始化元素不是常量”