定义了未定义的行为 [英] undefined behavior defined

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

问题描述

在comp.lang.c新闻组的上下文中,

术语未定义的行为实际上指的是

行为,不是由ANSI / ISO C [89] 9标准定义的。

具体来说,不是*真的任何事情都可能发生<如果您的C代码调用未定义的行为,则为
.


行为未由ANSI / ISO C [89] 9标准定义

可能由其他标准(即POSIX)或

定义,它可能由您的编译器的[实现]定义,

您的操作系统或您的机器架构。

存在*没有*上述

的组合,其中没有定义行为。


行为可能是无证"或者文档可能是未发布的。

有时,这是因为各个开发人员对

的监督,但更常见的是文档

的非标准功能根本没有向公众提供

因为开发人员不能保证支持功能。


从来没有一个可信的报告

恶魔飞扬任何程序员的鼻子

,因为他[或她]调用了未定义的行为。

解决方案

2004年1月10日星期六18:14:04 -0800,E。 Robert Tisdale

< E. ************** @ jpl.nasa.gov>在comp.lang.c中写道:

在comp.lang.c新闻组的上下文中,
术语未定义的行为。实际上指的是没有由ANSI / ISO C [89] 9标准定义的行为。
具体来说,如果您的C代码调用,那么任何事情都可能发生并不是真的。 未定义的行为。


再次错误,量油尺。


术语未定义的行为在C标准中确切地定义了

本身,因此它在C语言中具有精确含义并且在comp.lang.c新闻组中具有确定的含义。


第3.4.3节第3段规定:


未定义行为

行为,使用不可移植或错误的程序构造或

的错误数据,本国际标准没有规定

要求


注意关键短语强加没有要求简而言之,

意味着如果你的程序产生未定义的行为,那么所有发生的事情就像其他任何东西一样有效或无效,至今

作为标准C语言。

未由ANSI / ISO C [89] 9标准定义的行为可能由其他一些标准(即POSIX)定义或者它可能由你的编译器,你的操作系统或你的机器架构的[实现]定义。
上面存在* no *组合
在哪种行为没有定义。


当然这很荒谬。在某些情况下,

的行为是未定义的,因为它只是简单的不可知和

不可预测,直到实际的执行瞬间,每一个

代码执行的时间。


当然,就这个群体而言,没有像POSIX标准这样的东西。

。 />
行为可能是未记录的或者文档可能是未发布的。
有时,这是因为各个开发人员的疏忽,但更常见的是非标准特征的文档。根本没有向公众提供
因为开发人员不能保证支持功能。

从来没有一个可信的报告说恶魔飞扬任何程序员的鼻子
因为他[或她]调用了未定义的行为。




否,但有硬盘驱动器重新格式化的情况。

跳转到IBM XT上的不确定指针很可能导致

调用ROM硬盘格式例程。


告诉我这个程序的行为是如何定义的:


#include< time.h>

#include< stdio.h>


int main(无效)

{

double * dp =(double *)time(0);

printf(值为%f \ n,* dp);

返回0;

}


现在正是什么文件告诉我这个程序是否会被关闭

内存访问违规,内存对齐错误,浮点数b $ b点硬件异常,或者会输出一些乱码?

如果我运行相同的结果将保证结果是相同的
明天,下周,下个月,明年
计划?




未定义行为可能发生的非标准定义被调用只是简单无关 - 和

偏离主题 - 这里。


-

Jack Klein

主页: http://JK-Technology.Com
常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


",E。 Robert Tisdale <,E ************** @ jpl.nasa.gov>。写道:

从来没有一个可信的报告说恶魔飞扬任何程序员的鼻子,因为他(或她)调用了未定义的行为 ;。




无关紧要,因为没有人另有说法。


OTOH,经常听到声称如果C程序调用未定义

的行为,一个符合C的实现将被允许使得b / b $ b恶魔从程序员的鼻子中飞出,绝对是真的。这个

直接来自3.4.3#1。


Martin


在文章< 40**************@jpl.nasa.gov> ;,

E。 Robert Tisdale <,E ************** @ jpl.nasa.gov>。写道:

从来没有一个可靠的报告说恶魔飞扬任何程序员的鼻子
因为他(或她)调用了未定义的行为 ;




由于程序员调用了未定义的行为,因此有多个可信的报告称硬盘被重新格式化为
。实际上,经过更接近的b $ b b检查,每个单独的一个硬盘由一个C / / $
程序重新格式化,涉及未定义的行为。


In the context of the comp.lang.c newsgroup,
the term "undefined behavior" actually refers to
behavior not defined by the ANSI/ISO C [89]9 standard.
Specifically, it is *not* true that "anything can happen"
if your C code invokes "undefined behavior".

Behavior not defined by the ANSI/ISO C [89]9 standard
may be defined by some other standard (i.e. POSIX) or
it may be defined by [the implementation of] your compiler,
your operating system or your machine architecture.
There exists *no* combination of the above
in which behavior is not defined.

Behavior may be "undocumented" or documentation may be "unpublished".
Sometimes, this is because of an oversight on the part of
the respective developers but, more often, documentation
of non-standard "features" simply isn''t offered to the public
because the developer cannot promise to support the "features".

There has never been a credible report of
demons flying up any programmer''s nose
because he [or she] invoked "undefined behavior".

解决方案

On Sat, 10 Jan 2004 18:14:04 -0800, "E. Robert Tisdale"
<E.**************@jpl.nasa.gov> wrote in comp.lang.c:

In the context of the comp.lang.c newsgroup,
the term "undefined behavior" actually refers to
behavior not defined by the ANSI/ISO C [89]9 standard.
Specifically, it is *not* true that "anything can happen"
if your C code invokes "undefined behavior".
Wrong again, dipstick.

The term "undefined behavior" is precisely defined in the C standard
itself, so therefore it has a precise meaning in the C language and
the comp.lang.c newsgroup.

Paragraph 3 of section 3.4.3 states:

"undefined behavior
behavior, upon use of a nonportable or erroneous program construct or
of erroneous data, for which this International Standard imposes no
requirements"

Notice the key phrase "imposes no requirements". In small words, that
means that if your program produces undefined behavior, anything at
all that happens is just as valid or invalid as anything else, as far
as the standard C language is concerned.
Behavior not defined by the ANSI/ISO C [89]9 standard
may be defined by some other standard (i.e. POSIX) or
it may be defined by [the implementation of] your compiler,
your operating system or your machine architecture.
There exists *no* combination of the above
in which behavior is not defined.
Of course this as absurd. There are circumstances under which the
behavior is undefined because it is just plain unknowable and
unpredictable until the actual instant of execution, each and every
time the code is executed.

And of course, there is no such thing as a POSIX standard as far as
this group is concerned.
Behavior may be "undocumented" or documentation may be "unpublished".
Sometimes, this is because of an oversight on the part of
the respective developers but, more often, documentation
of non-standard "features" simply isn''t offered to the public
because the developer cannot promise to support the "features".

There has never been a credible report of
demons flying up any programmer''s nose
because he [or she] invoked "undefined behavior".



No, but there have been cases of hard disk drive reformatting.
Jumping to an indeterminate pointer on an IBM XT could well result in
invoking the ROM hard disk format routine.

Tell me how defined the behavior of this program is:

#include <time.h>
#include <stdio.h>

int main(void)
{
double *dp = (double *)time(0);
printf("The value is %f\n", *dp);
return 0;
}

Now just what document tells me whether this program will be shut down
for a memory access violation, a memory alignment fault, a floating
point hardware exception, or will happen to output some gibberish?
And what guarantees that the result will be the same if I run the same
program tomorrow, next week, next month, next year?

The possible non-standard definitions of what might happen when
undefined behavior is invoked is just plain irrelevant -- and
off-topic -- here.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:

There has never been a credible report of
demons flying up any programmer''s nose
because he [or she] invoked "undefined behavior".



Irrelevant, as nobody claims otherwise.

OTOH, the often heard claim that if a C program invoked undefined
behavior, a conforming C implementation would be allowed to make
demons fly out of the programmer''s nose, is definitely true. This
follows directly from 3.4.3#1.

Martin


In article <40**************@jpl.nasa.gov>,
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote:

There has never been a credible report of
demons flying up any programmer''s nose
because he [or she] invoked "undefined behavior".



There have been multiple credible reports of harddisks being reformatted
because a programmer invoked "undefined behavior". Actually, upon closer
examination, _every single case_ of a harddisk being reformatted by a C
program involved "undefined behavior".


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

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