“可能未定义”行为 [英] "Might be undefined" Behaviour

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

问题描述



我对

C标准所描述的不同行为有一个大致的了解,例如:


(1)明确定义的行为:


int a = 2,b = 3;


int c = a + b;


(Jist:代码将完美运行。)

(2)实现定义的行为


unsigned i = -1;


if(i 65535)DoSomething();

else DoSomethingElse();


(Jist:在不同的平台上可能会发生不同的事情,但

程序不应该崩溃。)

(3)未指明的行为


int i = Func1()+ Func2();


(Jist:我们不知道首先调用哪个函数。)

(4)未定义的行为


int i = INT_MAX;


++ i;


(实现可以做任何它喜欢的事情,程序可能会非常好地崩溃。)


我正在寻找一个名词thoug h描述一个代码片段_might_

调用未定义的行为。这是一个例子:


int i = 32767;


++ i;


给定int的最小范围,此代码可能在某些系统上失败并且

在其他系统上成功。我只是简单地将其标记为未定义

行为。我如何描述可能会根据具体实施细节调用未定义的

行为的代码片段?


-


Frederick Gotham


I have a general idea of the different kinds of behaviour described by the
C Standard, such as:

(1) Well-defined behaviour:

int a = 2, b = 3;

int c = a + b;

(Jist: The code will work perfectly.)
(2) Implementation-defined behaviour

unsigned i = -1;

if (i 65535) DoSomething();
else DoSomethingElse();

(Jist: Different things can happen on different platforms, but the
program shouldn''t crash.)
(3) Unspecified Behaviour

int i = Func1() + Func2();

(Jist: We don''t know which function is called first.)
(4) Undefined Behaviour

int i = INT_MAX;

++i;

(The implementation can do whatever it likes, and the program may very
well crash.)

I''m looking for a term though to describe a code snippet which _might_
invoke undefined behaviour. Here''s an example:

int i = 32767;

++i;

Given the minimum range of "int", this code may fail on some systems and
succeed on others. I hestitate though to simply label it as "undefined
behaviour". How would I describe a code snippet which may invoke undefined
behaviour depending on implementation-specific details?

--

Frederick Gotham

推荐答案




Frederick Gotham写道On 11/21/06 16:43 ,:


Frederick Gotham wrote On 11/21/06 16:43,:

我对

C标准所描述的不同行为有一般概念,例如:


(1)明确定义的行为:


int a = 2,b = 3;


int c = a + b;


(Jist:代码将完美运行。)
I have a general idea of the different kinds of behaviour described by the
C Standard, such as:

(1) Well-defined behaviour:

int a = 2, b = 3;

int c = a + b;

(Jist: The code will work perfectly.)



Gist。

"Gist."


>

(2)实现定义的行为


unsigned i = -1 ;


if(i 65535)DoSomething();

else DoSomethingElse();


(Jist:不同的事情可能发生在不同的平台上,但是

程序不应该崩溃。)


(3)未指明的行为


int i = Func1 ()+ Func2();


(Jist:我们不知道首先调用哪个函数。)


(4)未定义的行为


int i = INT_MAX;


++ i;


(实施可以做任何它喜欢的事情,程序可能会非常好地崩溃。)


我正在寻找一个术语来描述一个代码片段_might_

调用未定义的行为。这是一个例子:


int i = 32767;


++ i;


给定int的最小范围,此代码可能在某些系统上失败并且

在其他系统上成功。我只是简单地将其标记为未定义

行为。我如何描述可能会根据特定于实现的详细信息调用未定义的

行为的代码片段?
>
(2) Implementation-defined behaviour

unsigned i = -1;

if (i 65535) DoSomething();
else DoSomethingElse();

(Jist: Different things can happen on different platforms, but the
program shouldn''t crash.)
(3) Unspecified Behaviour

int i = Func1() + Func2();

(Jist: We don''t know which function is called first.)
(4) Undefined Behaviour

int i = INT_MAX;

++i;

(The implementation can do whatever it likes, and the program may very
well crash.)

I''m looking for a term though to describe a code snippet which _might_
invoke undefined behaviour. Here''s an example:

int i = 32767;

++i;

Given the minimum range of "int", this code may fail on some systems and
succeed on others. I hestitate though to simply label it as "undefined
behaviour". How would I describe a code snippet which may invoke undefined
behaviour depending on implementation-specific details?



这是实现定义的行为是定义还是未定义的
。请注意,仅仅是实现定义的

并不能完全覆盖这种情况,因为实现

其中INT_MAX == 32767不需要定义行为

这段代码。


也许我们可以用

implementation-undefined。这句话来缩写这个概念。 />

-
Er ********* @ sun.com

"It is implementation-defined whether the behavior is
defined or undefined." Note that mere "implementation-defined"
doesn''t quite cover the situation, because an implementation
where INT_MAX==32767 is not required to define the behavior of
this code.

Perhaps we could abbreviate this notion with the phrase
"implementation-undefined."

--
Er*********@sun.com


Frederick Gotham写道:
Frederick Gotham wrote:

>
>



.... snip ...

.... snip ...


>

我正在寻找一个术语虽然描述了一个代码片段,但是
_might_调用未定义的行为。这是一个例子:


int i = 32767;


++ i;


给定int的最小范围,此代码可能在某些

系统上失败并在其他系统上成功。我犹豫不决只是简单地将它标记为未定义的行为。我如何描述代码片段

可能会根据实现调用未定义的行为 -

具体细节?
>
I''m looking for a term though to describe a code snippet which
_might_ invoke undefined behaviour. Here''s an example:

int i = 32767;

++i;

Given the minimum range of "int", this code may fail on some
systems and succeed on others. I hestitate though to simply label
it as "undefined behaviour". How would I describe a code snippet
which may invoke undefined behaviour depending on implementation-
specific details?



#include< limits.h>


....


if(INT_MAX == i)overflowerror();

else ++ i;


-

Chuck F (cinefalconer at maineline dot net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>

#include <limits.h>

....

if (INT_MAX == i) overflowerror();
else ++i;

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


Frederick Gotham写道:
Frederick Gotham wrote:

我对<描述的不同类型的行为有一个大概的了解br />
C标准,例如:


(1)明确定义的行为:


int a = 2, b = 3;


int c = a + b;


(Jist:代码将完美运行。)
I have a general idea of the different kinds of behaviour described by the
C Standard, such as:

(1) Well-defined behaviour:

int a = 2, b = 3;

int c = a + b;

(Jist: The code will work perfectly.)



你的下一个例子也完全定义为IMO。


标准定义了严格符合_program_的术语。虽然它是
将其限定为_output_行为。例如,以下是一个

严格符合
的程序,即使某些操作使用

实现定义或未指定的值和结果.. 。


#include< stdio.h>

#include< limits.h>


int main(无效)

{

无符号x = UINT_MAX;

无符号y = -1u / 2;

unsigned z = x / y;

printf(生命的答案,宇宙,一切都是......);

printf("%u \ n",z * 21);

返回0;

}

Your next example is perfectly well defined too IMO.

The standard defines the term strictly conforming _program_. Though it
qualifies it as _output_ behaviour. For example, the following is a
strictly
conforming program even though some of the operations use
implementation defined or unspecified values and results...

#include <stdio.h>
#include <limits.h>

int main(void)
{
unsigned x = UINT_MAX;
unsigned y = -1u / 2;
unsigned z = x / y;
printf("The answer to life, the universe and everything is... ");
printf("%u\n", z * 21);
return 0;
}


(2)实施 - 定义的行为


unsigned i = -1;

if(i 65535)DoSomething();

else DoSomethingElse() ;
(2) Implementation-defined behaviour

unsigned i = -1;
if (i 65535) DoSomething();
else DoSomethingElse();



不需要实现来记录那段

代码的行为。请注意,该标准已经记录了第一个声明的行为

以及if条件和

语句的整体行为。

An implementation is not required to document the behaviour of that
piece of code. Note that the standard already documents the behaviour
of the first declaration and the behaviour of the if conditional and
statement as a whole.


(Jist:在不同平台上可能会发生不同的事情,但

程序不应该崩溃。)
(Jist: Different things can happen on different platforms, but the
program shouldn''t crash.)



为我定义的实现意味着:(-5> 1)

Implementation defined for me means something like: (-5 >1)


(3)未指定的行为


int i = Func1()+ Func2();


(Jist:我们不知道首先调用哪个函数。)


(4)未定义的行为


int i = INT_MAX;

++ i;
(3) Unspecified Behaviour

int i = Func1() + Func2();

(Jist: We don''t know which function is called first.)

(4) Undefined Behaviour

int i = INT_MAX;
++i;



请注意,有一类更强的未定义行为,即

约束

违规。 [在一段时间的clc线程中,委员会成员表示

即使标准实际上规定



行为也未定义在约束之外的规范性文本中的行为。]

Note that there is a stronger class of undefined behaviour, namely
constraint
violations. [In a clc thread a while back, committee members stated
that the
behaviour was undefined even if the standard does in fact specfiy the
behaviour in normative text outside of the constraint.]


(实现可以做任何它喜欢的事情,程序可能非常好b / b
很好的崩溃。)


我正在寻找一个术语来描述一个代码片段_might_

调用未定义的行为。这是一个例子:


int i = 32767;

++ i;


鉴于最小范围为int,此代码可能在某些系统上失败并且

在其他系统上成功。我只是简单地将其标记为未定义

行为。


我如何描述可能调用未定义的代码片段

行为取决于特定于实现的细节?
(The implementation can do whatever it likes, and the program may very
well crash.)

I''m looking for a term though to describe a code snippet which _might_
invoke undefined behaviour. Here''s an example:

int i = 32767;
++i;

Given the minimum range of "int", this code may fail on some systems and
succeed on others. I hestitate though to simply label it as "undefined
behaviour".

How would I describe a code snippet which may invoke undefined
behaviour depending on implementation-specific details?



Schr?dinger C?


可能未定义的行为?


" ;不便携似乎很常见。


-

彼得

Schr?dinger C?

Potentially undefined behaviour?

"Not portable" seems to be quite common.

--
Peter


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

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