这是未定义的,但它是合法的吗? [英] This is undefined, but is it legal?

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

问题描述






我听说如果你宣布了一个变量(例如双倍或
$) b $ b一个int)而不是初始化它,然后打印出它的

值的结果是未定义的。


我也听说过 ;未定义的行为可能意味着任何事情,比如飞出的猴子从鼻子里射出来的东西。当然,

这是夸张的,但通常我认为这意味着

程序可能会崩溃(或停止运行)不规律,甚至腐败
数据。


所以我的问题是:虽然我无法安全地预测未初始化的int或double的打印

输出,是吗仍然安全(或合法)

这样做?换句话说,如果我运行这个程序:


#include< iostream>


int main(int argc,char ** argv )

{

int i;

double d;


std :: cout< < i = << i<<的std :: ENDL; // safe?

std :: cout<< d = << d<<的std :: ENDL; //安全?


返回0;

}


我可能无法预测会打印什么out,但是有没有机会因为那些线路程序会崩溃?


如果你很好奇我为什么这么说,那就是'这是因为在某些代码中,我是

通过一个带有整数的结构 - 其中一些是从未使用或初始化的
。但是,这个结构(将其所有的全部内容)都写入磁盘(稍后再读回)。但是

没有时间这些未初始化的int用于逻辑代码

代码。


因为代码是写出未初始化的值(以及稍后阅读它们的
),是否有可能程序可以自行解决(或损坏任何东西)只是因为那些整数不是' 'b

初始化了?


提前致谢。


- Jean-Luc

解决方案

On Fri,2008年7月25日09:04:07 -0700,jl_post写道:


Hi ,


我听说如果你已经声明了一个变量(比如一个双或

一个int)而没有初始化它,那么打印出来的结果

未定义。


我也听说过未定义的行为可能意味着任何事情,比如飞出的猴子从鼻子里射出来的东西。



事实上,公认的后果是你的冰箱除霜。


当然,

这是夸张的,但通常我认为这意味着

程序会不稳定地崩溃(或停止运行),甚至会损坏数据。

所以我的问题是:虽然我永远无法安全地预测未初始化的int或double的打印

输出,但它是否仍然安全(或合法)

这样做?



这肯定是合法的从某种意义上说它不会阻止你的程序

编译。


换句话说,如果我运行这个程序:


#include< iostream>


int main(int argc,char ** argv)

{

int i;

double d;


std :: cout<< i = << i<<的std :: ENDL; // safe?

std :: cout<< d = << d<<的std :: ENDL; //安全?


返回0;

}


我可能无法预测会打印什么out,但是由于这些线路,程序可能崩溃是否有任何

的机会?



你没事。变量i的*值*可能未定义,但是

i仍然是int;并输出一个int - 任何int,无论它的价值是多少 - 应该永远不会崩溃你的程序。同上双倍等等。


-

Lionel B


jl ***** @ hotmail.com 写道:


[..]所以我的问题是:虽然我永远无法安全地预测未初始化的int或double的打印输出,但它是否仍然安全(或合法)

这样做?换句话说,如果我运行这个程序:


#include< iostream>



添加


#include< ostream>


来制作它完全符合标准。


>

int main(int argc,char ** argv)

{

int i;

double d;


std :: cout<< i = << i<<的std :: ENDL; // safe?

std :: cout<< d = << d<<的std :: ENDL; //安全?


返回0;

}


我可能无法预测会打印什么out,但是有没有机会因为这些线路程序崩溃?

[..]



我从未见过''int''表现那样,但我可以证明

未初始化''double''会导致系统抛出硬件

例外,我已经看到它发生了。


在档案中搜索未初始化的值陷阱异常或者

类似的东西,你可以希望找到其他人与

指针相遇甚至可能是积分值。


一般来说,任何* use *的未初始化对象都有未定义的

行为。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


7月25日下午6:04,jl_p ... @ hotmail.com写道:


我听说过,如果你已经声明了一个变量(比如

double或int)而没有初始化它,那么

打印出它的值的结果是未定义的。



使用* * *执行的任何操作都是未定义的行为。 (你

仍然可以获取其地址,或分配给它。)使用

unsigned char和char的例外。


我也听说过未定义的行为可能意味着任何事情,比如飞出的猴子从鼻子里射出来。

当然,这是夸张的,但通常我会解释为

意味着该程序可能崩溃(或停止运行)

不正常,甚至损坏数据。



在非特权模式下,在现代通用操作系统下,

这就是它通常意味着什么。在没有特权的系统(或内核代码)中的系统......我已经看到它需要重新格式化磁盘



所以我的问题是:虽然我永远无法安全地预测未初始化的int或double的

打印输出,但是它仍然是

safe(或合法)这样做?



否。这是未定义的行为。


换句话说,如果我运行此程序:


#include< iostream>


int main(int argc,char ** argv)

{

int i ;

加倍;


std :: cout<< i = << i<<的std :: ENDL; // safe?

std :: cout<< d = << d<<的std :: ENDL; //安全吗?


返回0;

}


我可能无法预测将打印出来的内容,但是,由于这些行,程序可能崩溃的可能性是多少?b
$ b



当然。在大多数现代机器上都不太可能使用int

(但至少有一个是明显的可能性)。

加倍,它''可以在每台Windows或Unix机器上运行我知道。


如果你很好奇我为什么这么问,那就是因为在某些代码中

我正在进行中有一个带有整数的结构 - 一些

从未使用也没有初始化。但是,这个结构

(将其所有的整数)写入磁盘(后来又读回了
)。但是这些

未初始化的int的值在任何时候都没有用于代码中的逻辑。


因为代码正在写出未初始化的值(并且稍后读取它们后面是
),是否有可能

程序可以自毁(或腐败任何东西)只是因为这些整数没有被初始化?



正式,是的,任何好的调试系统都会抱怨。 (我知道Purify会这么做,因为我必须处理相同的问题。)为什么不用初始化结构。


你是怎么写出来的?如果您只是将结构的

位复制到磁盘,那么您无法保证将来能够读取数据。
>

-

James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

Conseils更多信息,请联系我们b $ b beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''cocole,France,+ 33(0)1 30 23 00 34



Hi,

I''ve heard that if you''ve declared a variable (such as a double or
an int) and not initialize it, then the result of printing out its
value is undefined.

I''ve also heard that "undefined behavior" can mean just about
anything, such as "flying monkeys shooting out of your nose." Sure,
that''s an exaggeration, but normally I interpret that to mean that the
program can crash (or cease running) erratically, or even corrupt
data.

So my question is: Although I can never safely predict the printed
output of an uninitialized int or double, is it still safe (or legal)
to do so? In other words, if I run this program:

#include <iostream>

int main(int argc, char ** argv)
{
int i;
double d;

std::cout << "i = " << i << std::endl; // safe?
std::cout << "d = " << d << std::endl; // safe?

return 0;
}

I may not be able to predict what will print out, but is there any
chance that the program can crash because of those lines?

If you''re curious why I ask this, it''s because in some code I''m
working through there is a structure with ints -- some of which are
never used nor initialized. However, this structure (will all its
ints) is getting written out to disk (and later read back in). But at
no time are the values of these uninitialized ints used for logic in
the code.

Because the code is writing out uninitialized values (and later
reading them in), is there a possibility that the program can self-
destruct (or corrupt anything) just because those ints weren''t
initialized?

Thanks in advance.

-- Jean-Luc

解决方案

On Fri, 25 Jul 2008 09:04:07 -0700, jl_post wrote:

Hi,

I''ve heard that if you''ve declared a variable (such as a double or
an int) and not initialize it, then the result of printing out its value
is undefined.

I''ve also heard that "undefined behavior" can mean just about
anything, such as "flying monkeys shooting out of your nose."

In fact the commonly acknowledged consequence is that your fridge defrosts.

Sure,
that''s an exaggeration, but normally I interpret that to mean that the
program can crash (or cease running) erratically, or even corrupt data.

So my question is: Although I can never safely predict the printed
output of an uninitialized int or double, is it still safe (or legal)
to do so?

It''s certainly "legal" in the sense that it won''t prevent your program
compiling.

In other words, if I run this program:

#include <iostream>

int main(int argc, char ** argv)
{
int i;
double d;

std::cout << "i = " << i << std::endl; // safe?
std::cout << "d = " << d << std::endl; // safe?

return 0;
}

I may not be able to predict what will print out, but is there any
chance that the program can crash because of those lines?

You''re ok. The *value* of the variable i may well be undefined, but
i is nonetheless an int; and outputting an int - any int, whatever its
value - should never crash your program. Ditto double, etc.

--
Lionel B


jl*****@hotmail.com wrote:

[..] So my question is: Although I can never safely predict the printed
output of an uninitialized int or double, is it still safe (or legal)
to do so? In other words, if I run this program:

#include <iostream>

Add

#include <ostream>

to make it completely standard-compliant.

>
int main(int argc, char ** argv)
{
int i;
double d;

std::cout << "i = " << i << std::endl; // safe?
std::cout << "d = " << d << std::endl; // safe?

return 0;
}

I may not be able to predict what will print out, but is there any
chance that the program can crash because of those lines?
[..]

I have never seen ''int'' behave that way, but I can attest that an
uninitialised ''double'' can cause the system to throw a hardware
exception, and I''ve seen it happen.

Search the archives for "uninitialized value trap exception" or
something like that, and you can hopefully find others'' encounters with
pointers and perhaps even integral values.

Generally speaking, any *use* of an uninitialised object has undefined
behaviour.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On Jul 25, 6:04 pm, jl_p...@hotmail.com wrote:

I''ve heard that if you''ve declared a variable (such as a
double or an int) and not initialize it, then the result of
printing out its value is undefined.

Anything you do with its *value* is undefined behavior. (You
can still take its address, or assign to it.) With the
exception of unsigned char and char.

I''ve also heard that "undefined behavior" can mean just about
anything, such as "flying monkeys shooting out of your nose."
Sure, that''s an exaggeration, but normally I interpret that to
mean that the program can crash (or cease running)
erratically, or even corrupt data.

In non-priviledged mode, under a modern general purpose OS,
that''s what it normally means. On systems without a priviledged
mode (or in kernel code)... I have seen it require the disk to
be reformatted.

So my question is: Although I can never safely predict the
printed output of an uninitialized int or double, is it still
safe (or legal) to do so?

No. It''s undefined behavior.

In other words, if I run this program:

#include <iostream>

int main(int argc, char ** argv)
{
int i;
double d;

std::cout << "i = " << i << std::endl; // safe?
std::cout << "d = " << d << std::endl; // safe?

return 0;
}

I may not be able to predict what will print out, but is there
any chance that the program can crash because of those lines?

Of course. It''s not likely with int, on most modern machines
(but there is at least one where it is a distinct possibility).
With double, it''s possible on every Windows or Unix machine I
know.

If you''re curious why I ask this, it''s because in some code
I''m working through there is a structure with ints -- some of
which are never used nor initialized. However, this structure
(will all its ints) is getting written out to disk (and later
read back in). But at no time are the values of these
uninitialized ints used for logic in the code.

Because the code is writing out uninitialized values (and
later reading them in), is there a possibility that the
program can self- destruct (or corrupt anything) just because
those ints weren''t initialized?

Formally, yes, and any good debugging system will complain. (I
know Purify does, because I''ve had to deal with the same
problem.) Why don''t you just initialize the structs.

And how are you writing them out? If you''re just copying the
bits of a struct to disk, then you have no guarantee of being
able to read the data in the future.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


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

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