未定义和未指定的行为 [英] undefined and unspecified behavior

查看:98
本文介绍了未定义和未指定的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我未定义的行为

和未指定的行为之间的区别是什么?

虽然我已经阅读了有关它们的内容,但在ISO标准中,我仍然没有能够得到差异。


例如:

请考虑以下代码:

a [i ++] = i;


我们说上面的表达式语句会产生未定义的

行为。为什么我们不能将其称为未指明的行为,因为

没有说明副作用的完成顺序,

会影响输出?


此外,我发现这个表达你的鼻子里有恶魔,

被许多人使用,同时谈论未定义的行为。请告诉我

这个表达在这种情况下意味着什么。


提前感谢您的回复。

Can anyone tell me what is the difference between undefined behavior
and unspecified behavior?
Though I''ve read what is given about them, in ISO standards, I''m still
not able to get the difference.

For example:
Consider the following code:
a[i++] = i;

We say that the above expression statement produces undefined
behavior. Why can''t we call it as unspecified behavior, because
nothing is said about the order of completion of side effects, which
affects the output?

Furthermore, I''ve found this expression "demons out of your nose",
used by many, while talking about undefined behavior. Please tell me
what that expression means, in this context.

Thanks in advance for the reply.

推荐答案

2007年3月17日07:38:29 -0700,Rajesh S R < SR ********** @ gmail.com>

写道:
On 17 Mar 2007 07:38:29 -0700, "Rajesh S R" <SR**********@gmail.com>
wrote:

>任何人都可以告诉我未定义的行为与未指明的行为有什么区别?
虽然我已经阅读了有关它们的内容,但在ISO标准中,我仍然无法区分它们。

例如:
考虑以下代码:
a [i ++] = i;

我们说上面的表达式语句产生未定义的<行为。为什么我们不能把它称为未指明的行为,因为
没有说完副作用的顺序,这会影响输出?

此外,我在谈论未定义的行为时,已经发现这个表达你的鼻子里有恶魔,许多人都在使用它。请告诉我
在这种情况下,表达意味着什么。

提前感谢您的回复。
>Can anyone tell me what is the difference between undefined behavior
and unspecified behavior?
Though I''ve read what is given about them, in ISO standards, I''m still
not able to get the difference.

For example:
Consider the following code:
a[i++] = i;

We say that the above expression statement produces undefined
behavior. Why can''t we call it as unspecified behavior, because
nothing is said about the order of completion of side effects, which
affects the output?

Furthermore, I''ve found this expression "demons out of your nose",
used by many, while talking about undefined behavior. Please tell me
what that expression means, in this context.

Thanks in advance for the reply.



未定义的行为需要使用不可移植或错误的

程序结构或错误数据。 (第3.4.3段)。你的

上面的例子符合这个条件,因为虽然语法正确

它违反了第6.5-2段的限制,因此

错误。


未指定的行为不是错误的。这是一个稍微更通用的实现定义行为的案例。在这两种情况下,标准

提供两种或更多种可能性。对于实现定义的

行为,所选择的可能性必须由每个

(符合)实现记录。对于未指定的行为,没有

进一步的要求,因此无需记录。

删除del电子邮件

Undefined behavior requires the use of a "nonportable or erroneous
program construct or of erroneous data" (paragraph 3.4.3). Your
example above meets this condition because while syntactically correct
it violates the restriction in paragraph 6.5-2 and is therefore
erroneous.

Unspecified behavior is not erroneous. It is a slightly more general
case of implementation-defined behavior. In both cases, the standard
provides two or more possibilities. For implementation-defined
behavior, the chosen possibility must be documented by every
(conforming) implementation. For unspecified behavior, there are no
further requirements so it need not be documented.
Remove del for email


Rajesh SR写道:


< snip>
Rajesh S R wrote:

<snip>

此外,我发现这个表达恶魔在谈论未定义的行为时,许多人使用了你的鼻子,

。在这种情况下,请告诉我

表达式的含义。
Furthermore, I''ve found this expression "demons out of your nose",
used by many, while talking about undefined behavior. Please tell me
what that expression means, in this context.



这只是一种幽默的方式,提到在程序中调用了
未定义的行为之后会发生任何事情。我听说过这个特殊的短语是由一位名叫

Kaz Kyzhelku的前常客在clc中创造出来的。显然,这是未定义的常见反应
在神秘的DS9000上运行的C程序中调用
行为。

It''s just a humorous way of mentioning that anything can happen after
undefined behaviour has been invoked in a program. I''ve heard that
that particular phrase was coined in clc by a former regular called
Kaz Kyzhelku. Apparently it''s a common response when undefined
behaviour is invoked in C programs running on the mythical DS9000.


3月17,2:38 pm,Rajesh S R < SRRajesh1 ... @ gmail.comwrote:


未指明的行为:C标准提供了多种可能性,

并且没有指定选择这些可能性,但是

你知道其中一个会被选中。


未定义的行为:任何事情都可能发生,我的意思是绝对的>
任何东西。你甚至不想考虑弄清楚什么可能会发生什么,什么不可能,因为_anything_可能发生。在你的

例子中,如果副作用发生的未指定顺序是

唯一的问题,那么它将是未指定的。行为。但它更糟糕的是:_Anything_可能发生,包括格式化硬盘,

您的计算机开始在整个地方发送垃圾邮件,或报告

你的信用卡号码给一些黑客。

On Mar 17, 2:38 pm, "Rajesh S R" <SRRajesh1...@gmail.comwrote:

Unspecified behavior: The C Standard gives more than one possibility,
and it is not specified which of these possibilities is chosen, but
you know that one of them will be picked.

Undefined behavior: Anything can happen, and I mean absolutely
anything. Don''t even think about trying to figure out what could
happen and what couldn''t, because _anything_ can happen. In your
example, if the unspecified order in which side effects happen where
the only problem, then it would be "unspecified" behavior. But it is
worse: _Anything_ can happen, including formatting your hard disk,
your computer starting to send spam all over the place, or reporting
your credit card numbers to some hackers.


这篇关于未定义和未指定的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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