未定义,未指定和实施定义的行为

发布于 2025-02-10 05:22:06 字数 77 浏览 3 评论 0 原文

C和C ++中的不确定的行为(ub)? 未指定的行为实现定义行为呢?它们有什么区别?

What is undefined behavior (UB) in C and C++? What about unspecified behavior and implementation-defined behavior? What is the difference between them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

终止放荡 2025-02-17 05:22:07

实施定义 -

实施者的希望,应该有充分的记录,标准提供了选择,但一定要编译

未指定的 -

与实现定义但未记录的

相同

未定义 -

可能会发生任何事情,请照顾好。

Implementation defined-

Implementors wish,should be well documented,standard gives choices but sure to compile

Unspecified -

Same as implementation-defined but not documented

Undefined-

Anything might happen,take care of it.

长安忆 2025-02-17 05:22:07

从历史上看,实施定义的行为和不确定的行为代表了标准的作者期望,他们撰写质量实施的人们将使用判断来确定哪些行为保证(如果有的话)对在该计划中运行的计划字段中有用是有用的。预期目标。高端数字处理代码的需求与低级系统代码的需求完全不同,UB和IDB都使编译器作家灵活地满足这些不同的需求。既不要求任何类别的实施方式以任何特定目的有用的方式,甚至对任何目的有用。但是,声称适合特定目的的质量实施应以适合此目的的方式行事是否需要标准。

实施定义的行为和未定义的行为之间的唯一区别在于,前者要求实施定义和记录一致的行为,即使在实施不可能使用的情况下,实现行为也是有用的。它们之间的分界线不是通常对于实现行为是否有用(编译器作者应定义有用的行为,而当标准是否要求它们是否要求它们时),而是),而是 是否可能存在定义行为的实现同时成本高昂,无用。关于这种实现可能存在的判断并非以任何方式,形状或形式,这意味着关于支持其他平台上确定行为的有用性的任何判断。

不幸的是,自1990年代中期以来,编译器作家已经开始将缺乏行为命令解释为判断力,即即使在其重要的应用领域,甚至在他们几乎没有成本的系统中,行为保证也不值得成本。编译器作家并没有将UB视为行使合理判断的邀请,而是开始将其视为借口不是

例如,给定以下代码:

int scaled_velocity(int v, unsigned char pow)
{
  if (v > 250)
    v = 250;
  if (v < -250)
    v = -250;
  return v << pow;
}

两个组合的实现将不必花费任何努力
任何处理表达式 v&lt;&lt; POW 作为两个组合班
不考虑 v 是正面还是负面。

但是 V 的of。即使对每个意义上的编译器都支持负值的左移动,并且大量现有代码依赖于这种行为,现代哲学也会解释这样一个事实,即标准表明左移动负值是UB暗示编译器作家应该随意忽略这一点。

Historically, both Implementation-Defined Behavior and Undefined Behavior represented situations in which the authors of the Standard expected that people writing quality implementations would use judgment to decide what behavioral guarantees, if any, would be useful for programs in the intended application field running on the intended targets. The needs of high-end number-crunching code are quite different from those of low-level systems code, and both UB and IDB give compiler writers flexibility to meet those different needs. Neither category mandates that implementations behave in a way that's useful for any particular purpose, or even for any purpose whatsoever. Quality implementations that claim to be suitable for a particular purpose, however, should behave in a manner befitting such purpose whether the Standard requires it or not.

The only difference between Implementation-Defined Behavior and Undefined Behavior is that the former requires that implementations define and document a consistent behavior even in cases where nothing the implementation could possibly do would be useful. The dividing line between them is not whether it would generally be useful for implementations to define behaviors (compiler writers should define useful behaviors when practical whether the Standard requires them to or not) but whether there might be implementations where defining a behavior would be simultaneously costly and useless. A judgment that such implementations might exist does not in any way, shape, or form, imply any judgment about the usefulness of supporting a defined behavior on other platforms.

Unfortunately, since the mid 1990s compiler writers have started to interpret the lack of behavioral mandates as an judgment that behavioral guarantees aren't worth the cost even in application fields where they're vital, and even on systems where they cost practically nothing. Instead of treating UB as an invitation to exercise reasonable judgment, compiler writers have started treating it as an excuse not to do so.

For example, given the following code:

int scaled_velocity(int v, unsigned char pow)
{
  if (v > 250)
    v = 250;
  if (v < -250)
    v = -250;
  return v << pow;
}

a two's-complement implementation would not have to expend any effort
whatsoever to treat the expression v << pow as a two's-complement shift
without regard for whether v was positive or negative.

The preferred philosophy among some of today's compiler writers, however, would suggest that because v can only be negative if the program is going to engage in Undefined Behavior, there's no reason to have the program clip the negative range of v. Even though left-shifting of negative values used to be supported on every single compiler of significance, and a large amount of existing code relies upon that behavior, modern philosophy would interpret the fact that the Standard says that left-shifting negative values is UB as implying that compiler writers should feel free to ignore that.

黒涩兲箜 2025-02-17 05:22:07

C ++标准N3337 § 1.3.10
实施定义的行为

行为,用于形成良好的程序构建和正确的数据,
取决于实施,每个实施文档

有时C ++标准不会在某些构造上强加特定的行为,而是说必须选择特定的,定义明确的行为,并通过特定的实现(版本图书馆)。因此,用户仍然可以确切地知道程序的行为,即使标准没有描述这一点。


C ++标准N3337 § 1.3.24
未定义的行为

该国际标准没有任何要求的行为
[注意:当国际
标准省略了对行为的任何明确定义或程序
使用错误的构造或错误的数据。允许的不确定
行为范围从完全忽略情况
不可预测的结果,在翻译或程序中行为
以有记录的方式执行环境的特征
(有或没有发出诊断消息),以终止
翻译或执行(发出诊断
信息)。许多错误的程序构造并未导致未定义
行为;他们必须被诊断。 - 终注]

当程序遇到未根据C ++标准定义的构造时,允许执行您想做的任何事情(也许向我发送电子邮件或向您发送电子邮件或完全忽略代码)。


C ++标准N3337 § 1.3.25
未指定的行为

行为,用于形成良好的程序构建和正确的数据,
取决于实现[注意:实现不是
需要记录发生哪些行为。可能的范围
行为通常由该国际标准划定。 - 结尾
注意]

C ++标准不会在某些构造上强加特定的行为,而是说必须通过特定实现(库的版本)选择特定的,定义明确的行为(但不一定描述)。因此,在没有提供描述的情况下,用户很难确切知道程序的行为。

C++ standard n3337 § 1.3.10
implementation-defined behavior

behavior, for a well-formed program construct and correct data, that
depends on the implementation and that each implementation documents

Sometimes C++ Standard doesn't impose particular behavior on some constructs but says instead that a particular, well defined behavior has to be chosen and described by particular implementation (version of library). So user can still know exactly how will program behave even though Standard doesn't describe this.


C++ standard n3337 § 1.3.24
undefined behavior

behavior for which this International Standard imposes no requirements
[ Note: Undefined behavior may be expected when this International
Standard omits any explicit definition of behavior or when a program
uses an erroneous construct or erroneous data. Permissible 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). Many erroneous program constructs do not engender undefined
behavior; they are required to be diagnosed. — end note ]

When the program encounters construct that is not defined according to C++ Standard it is allowed to do whatever it wants to do ( maybe send an email to me or maybe send an email to you or maybe ignore the code completely).


C++ standard n3337 § 1.3.25
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. The range of possible
behaviors is usually delineated by this International Standard. — end
note ]

C++ Standard doesn't impose particular behavior on some constructs but says instead that a particular, well-defined behavior has to be chosen ( but not necessarily described) by particular implementation (version of library). So in the case when no description has been provided it can be difficult for the user to know exactly how the program will behave.

〗斷ホ乔殘χμё〖 2025-02-17 05:22:07

未定义的行为是丑陋的 - 如“好,坏和丑陋”。

良好:出于正确的理由,可以编译和工作的程序。

不好:一个具有错误的程序,该程序可以检测和投诉。

丑陋:一个有错误的程序,编译器无法检测和警告,这意味着该程序编译了,并且似乎在某些时候可以正常工作,但也使某些时间失败了。那就是不确定的行为。

某些程序语言和其他正式系统会努力限制“不确定的湾” - 也就是说,他们尝试安排事情,以便大多数或所有程序都是“好”或“坏”,而很少有人是“丑陋的” ”。然而,C的特征是它的“不确定鸿沟”非常广泛。

Undefined behavior is ugly -- as in, "The good, the bad, and the ugly".

Good: a program that compiles and works, for the right reasons.

Bad: a program that has an error, of a kind that the compiler can detect and complain about.

Ugly: a program that has an error, that the compiler cannot detect and warn about, meaning that the program compiles, and may seem to work correctly some of the time, but also fails bizarrely some of the time. That's what undefined behavior is.

Some program languages and other formal systems try hard to limit the "gulf of undefinedness" -- that is, they try to arrange things so that most or all programs are either "good" or "bad", and that very few are "ugly". It's a characteristic feature of C, however, that its "gulf of undefinedness" is quite wide.

简单爱 2025-02-17 05:22:07

更新2024:

对于C ++,由于C ++ 26,我们还具有错误的行为,它与不确定的行为不同。

这篇文章的讨论篇幅:什么是错误的”>行为?它与未定义的行为有何不同?

但是,由于当前的帖子是各种“有问题”的律师的主要文章,
我认为在此处发布摘要将很有帮助:

  • 主要动机是减少对安全攻击的志愿性,因为UB允许编译器几乎可以做任何事情。
  • 此功能是C ++委员会努力将语言引导到更安全的地方的一部分。
  • 错误的行为始于错误的值,例如,由于一个非初始化的变量而导致的。
  • 错误的行为带来了失去一些优化机会的成本。
  • 非初始化的变量可以通过使用 [[不确定]] 属性来保持Pre-C ++ 26行为。

描述来自 cppreference

错误的行为 - (不正确的)行为,建议实现进行诊断。

  • 错误的行为始终是程序代码不正确的结果。
  • 对常数表达的评估永远不会导致错误的行为。
  • 如果执行包含指定为错误行为的操作,则允许并建议执行诊断,并允许在该操作后未指定的时间终止执行。
  • 实现可以确定在针对程序行为的一组假设之下可以达到错误的行为,这可以发出诊断。

最后,这篇文章可能是Intereset的:
据此,编译器确实应确保某种初始化。
由于具有性能效果,因此您可以使用 [[不确定]] 属性如上所述避免。

Update 2024:

For C++ Since C++26 we also have erroneous behavior which is different than undefined-behavior.

This post discusses it in length: What is erroneous behavior? How is it different from undefined behavior?.

However since the current post is the main one regarding all sorts of "problematic" behviors,
I thought it would be helpful to post a summary here for completeness:

  • The main motivation is to reduce the volunarablity to security attacks due to fact that UB allows the compiler to do almost anything.
  • This feature is a part of the effort of the C++ committee to direct the language to a safer place.
  • Erroneous behavior starts with an erroneous value, e.g. as a result of an uninitialized variable.
  • Erroneous behavior comes with the cost of loosing some optimization opportunities.
  • An uninitialized variable can keep the pre-C++26 behavior by using the [[indeterminate]] attribute.

The description from cppreference:

erroneous behavior - (incorrect) behavior that the implementation is recommended to diagnose.

  • Erroneous behavior is always the consequence of incorrect program code.
  • The evaluation of a constant expression never results in an erroneous behavior.
  • If the execution contains an operation specified as having erroneous behavior, the implementation is permitted and recommended to issue a diagnostic, and is permitted to terminate the execution at an unspecified time after that operation.
  • An implementation can issue a diagnostic if it can determine that erroneous behavior is reachable under an implementation-specific set of assumptions about the program behavior, which can result in false positives.

Finally this post might be of intereset: In C++26, are implementations required to "initialize" uninitialized variables to some fixed byte pattern?.
According to it the compiler should indeed ensure some kind of initialization.
Since this has a performance effect, you can use the [[indeterminate]] attribute as mentioned above to avoid it.

隐诗 2025-02-17 05:22:06

未定义的行为 是C和C ++语言的这些方面之一,这对于来自其他语言的程序员来说可能会令人惊讶(其他语言试图更好地隐藏它)。基本上,即使许多C ++编译器不会报告程序中的任何错误,也可以编写无法以可预测方式行为的C ++程序!

让我们看一个经典示例:

#include <iostream>
    
int main()
{
    char* p = "hello!\n";   // yes I know, deprecated conversion
    p[0] = 'y';
    p[5] = 'w';
    std::cout << p;
}

变量 p 指向字符串文字“ hello!\ n” ,下面的两个作业尝试修改该字符串字面的字体。这个程序做什么?根据 c ++标准,[lex.string]注释4 ,它调用未定义的行为

尝试修改字符串字面的效果是不确定的。

我可以听到人们尖叫的声音。尝试导致核心转储”。这正是不确定行为的问题。基本上,该标准允许一旦调用未定义的行为(甚至鼻恶魔),任何事情都会发生。如果根据您的语言心理模型存在“正确”的行为,则该模型简直是错误的; C ++标准是唯一的投票期。

未定义行为的其他示例包括

[Into.defs] 也定义了未定义的两个较不危险的兄弟,未指定的兄弟行为实施定义的行为

实施定义的行为&nbsp;&nbsp;&nbsp;&nbsp;

行为,用于良好的程序构建和正确的数据,取决于实现,每个实施文档

未指定的行为&nbsp;&nbsp;&nbsp;&nbsp; /H5>

行为,对于良好的程序构建和正确的数据,取决于实现

[注意:不需要实现来记录发生哪些行为。
可能的行为范围通常由本文档描述。
- end Note ]

未定义的行为&nbsp;&nbsp;&nbsp;&nbsp; /H5>

本文档不强调要求的行为

[注释:当本文档省略对行为的任何明确定义或程序使用错误的构造或错误的数据时,可能会预期不确定的行为。
允许的不确定的行为范围从完全忽略情况以不可预测的结果忽略情况,到在翻译或计划执行过程中以有记录的环境特征(有或没有发出诊断消息的发行)到终止翻译或执行(通过发行发行)诊断消息)。 [...]
- end Note ]

您该怎么做才能避免遇到未定义的行为?基本上,您必须阅读良好的C ++书籍由知道他们在说什么的作者。避免互联网教程。避免公牛。

Undefined behavior is one of those aspects of the C and C++ language that can be surprising to programmers coming from other languages (other languages try to hide it better). Basically, it is possible to write C++ programs that do not behave in a predictable way, even though many C++ compilers will not report any errors in the program!

Let's look at a classic example:

#include <iostream>
    
int main()
{
    char* p = "hello!\n";   // yes I know, deprecated conversion
    p[0] = 'y';
    p[5] = 'w';
    std::cout << p;
}

The variable p points to the string literal "hello!\n", and the two assignments below try to modify that string literal. What does this program do? According to the C++ standard, [lex.string] note 4, it invokes undefined behavior:

The effect of attempting to modify a string literal is undefined.

I can hear people screaming "But wait, I can compile this no problem and get the output yellow" or "What do you mean undefined, string literals are stored in read-only memory, so the first assignment attempt results in a core dump". This is exactly the problem with undefined behavior. Basically, the standard allows anything to happen once you invoke undefined behavior (even nasal demons). If there is a "correct" behavior according to your mental model of the language, that model is simply wrong; The C++ standard has the only vote, period.

Other examples of undefined behavior include

[intro.defs] also defines undefined behavior's two less dangerous brothers, unspecified behavior and implementation-defined behavior:

implementation-defined behavior    [defns.impl.defined]

behavior, for a well-formed program construct and correct data, that depends on the implementation and that each implementation documents

unspecified behavior    [defns.unspecified]

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.
The range of possible behaviors is usually delineated by this document.
end note]

undefined behavior    [defns.undefined]

behavior for which this document imposes no requirements

[Note: Undefined behavior may be expected when this document omits any explicit definition of behavior or when a program uses an erroneous construct or erroneous data.
Permissible 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). [...]
end note]

What can you do to avoid running into undefined behavior? Basically, you have to read good C++ books by authors who know what they're talking about. Avoid internet tutorials. Avoid bullschildt.

时光与爱终年不遇 2025-02-17 05:22:06

好吧,这基本上是 c Standard ::

3.4.1 1 实施定义的行为未指定的行为
每个实现都记录了如何
选择是

2示例一个例子
实施定义的行为是
当高阶位传播
签名的整数向右移动。

3.4.3 1 未定义的行为行为,使用了不可商业或错误的行为
程序构造或错误
数据,该国际
标准不征收任何要求

2
注意可能的未定义行为
范围从忽略情况
完全有不可预测的结果,
在翻译过程中表现或
程序执行在有记录的
方式的方式
环境(有或没有
发出诊断信息),
终止翻译或执行
(随着诊断的发行
消息)。

3示例
不确定的行为是行为
整数溢出。

3.4.4 1 未指定的行为使用未指定的价值或其他行为
这个国际标准
提供两个或更多可能性,

在任何情况下都选择

2
示例未指定的示例
行为是
评估了函数的论点。

Well, this is basically a straight copy-paste from the C standard:

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.

凡间太子 2025-02-17 05:22:06

也许比标准的严格定义更容易理解简单的措辞。

实施定义的行为:
该语言说我们有数据类型。编译器供应商指定了他们使用的尺寸,并提供了有关他们所做工作的文档。

未定义的行为:
你做错了什么。例如,您在 int 中具有很大的价值,该价值不适合 char 。您如何将该值放入 char 中?实际上,没有办法!任何事情都可能发生,但是最明智的事情就是将其第一个字节放入 char 中。分配第一个字节是错误的,但这就是引擎盖下发生的情况。

未指定的行为:
首先执行这两个功能中的哪个?

void fun(int n, int m);

int fun1() {
    std::cout << "fun1";
    return 1;
}
int fun2() {
    std::cout << "fun2";
    return 2;
}

//...

fun(fun1(), fun2()); // which one is executed first?

该语言未指定评估,左至右或向左!因此,未指定的行为可能会或可能不会导致不确定的行为,但是当然,您的程序不应产生未指定的行为。


@eskay我认为您的问题值得编辑答案以澄清更多:)

对于 fun(fun1(),fun2()); 行为是否“实现”吗?编译器必须选择一个或另一个课程,毕竟?

实施定义和未指定之间的区别在于,编译器应该在第一种情况下选择行为,但在第二种情况下不必这样做。例如,实现必须具有 sizeof(int)的一个定义。因此,它不能说该程序的某些部分 为4,而其他部分为8。与未指定的行为不同,编译器可以说:“好吧,我将从左到右评估这些论点,并评估了下一个函数的论点。”它可以在同一程序中发生,这就是为什么它被称为 未指定的 。实际上,如果指定了一些未指定的行为,则可以使C ++更容易。在此处查看 dr。 Stroustrup的答案

据称,可以为编译器提供这种自由与需要“普通左右评估”的差异可能很重要。我不敢相信,但是有无数的编译器“那里”利用自由,有些人热情地捍卫自由,改变将是困难的,可能需要数十年的时间才能渗透到C和C ++世界的遥远角落。我感到失望的是,并非所有编译器都警告 ++ i+i ++ 等代码。同样,未指定参数的评估顺序。

imo太多的“事物”是未定义的,未指定的,很容易说,甚至举例说明,但很难解决。还应注意的是,避免大多数问题并产生便携式代码并不是那么困难。

Maybe simpler wording could be easier to understand than the rigorous definition of the standards.

implementation-defined behavior:
The language says that we have data-types. The compiler vendors specify what sizes shall they use, and provide a documentation of what they did.

undefined behavior:
You are doing something wrong. For example, you have a very large value in an int that doesn't fit in char. How do you put that value in char? actually there is no way! Anything could happen, but the most sensible thing would be to take the first byte of that int and put it in char. It is just wrong to do that to assign the first byte, but thats what happens under the hood.

unspecified behavior:
Which of these two functions is executed first?

void fun(int n, int m);

int fun1() {
    std::cout << "fun1";
    return 1;
}
int fun2() {
    std::cout << "fun2";
    return 2;
}

//...

fun(fun1(), fun2()); // which one is executed first?

The language doesn't specify the evaluation, left to right or right to left! So an unspecified behavior may or mayn't result in an undefined behavior, but certainly your program should not produce an unspecified behavior.


@eSKay I think your question is worth editing the answer to clarify more :)

for fun(fun1(), fun2()); isn't the behaviour "implementation defined"? The compiler has to choose one or the other course, after all?

The difference between implementation-defined and unspecified, is that the compiler is supposed to pick a behavior in the first case but it doesn't have to in the second case. For example, an implementation must have one and only one definition of sizeof(int). So, it can't say that sizeof(int) is 4 for some portion of the program and 8 for others. Unlike unspecified behavior, where the compiler can say: "OK I am gonna evaluate these arguments left-to-right and the next function's arguments are evaluated right-to-left." It can happen in the same program, that's why it is called unspecified. In fact, C++ could have been made easier if some of the unspecified behaviors were specified. Take a look here at Dr. Stroustrup's answer for that:

It is claimed that the difference between what can be produced giving the compiler this freedom and requiring "ordinary left-to-right evaluation" can be significant. I'm unconvinced, but with innumerable compilers "out there" taking advantage of the freedom and some people passionately defending that freedom, a change would be difficult and could take decades to penetrate to the distant corners of the C and C++ worlds. I am disappointed that not all compilers warn against code such as ++i+i++. Similarly, the order of evaluation of arguments is unspecified.

IMO far too many "things" are left undefined, unspecified, that's easy to say and even to give examples of, but hard to fix. It should also be noted that it is not all that difficult to avoid most of the problems and produce portable code.

泅人 2025-02-17 05:22:06

从官方的基本原理文件中

术语未指定的行为, 不确定的行为和 实施定义行为用于对编写属性标准属性的编写程序的结果进行分类没有或不能完全描述。采用这种分类的目的是允许实施中有一定的变化,这使实施质量成为市场上的积极力量,并允许某些流行的扩展,而无需消除与标准的统一性。标准目录的附录F将这些行为属于这三个类别之一。

未指定的行为在翻译程序时为实施者提供了一些纬度。这个纬度并不能扩展到未能翻译程序。

未定义的行为允许实施者许可不要捕获难以诊断的某些程序错误。它还确定了可能符合语言扩展的领域:实施者可以通过提供官方不确定的行为来增强语言。

实施定义的行为使实施者可以自由选择适当的方法,但要求向用户解释此选择。指定为实施定义的行为通常是用户可以根据实现定义做出有意义的编码决策的行为。在确定实施定义的广泛定义时,实施者应牢记该标准。与未指定的行为一样,简单地不翻译包含实现定义行为的源并不是足够的响应。

From the official C Rationale Document

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.

伊面 2025-02-17 05:22:06

不确定的行为与未指定的行为有一个简短的描述。

他们的最后摘要:

总结一下,未指定的行为通常是您不应该不应该的
担心,除非您的软件需要便携。
相反,不确定的行为总是不受欢迎的,永远不应该
发生。

Undefined Behavior vs. Unspecified Behavior has a short description of it.

Their final summary:

To sum up, unspecified behavior is usually something you shouldn't
worry about, unless your software is required to be portable.
Conversely, undefined behavior is always undesirable and should never
occur.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文