未定义的行为 - 2个查询 [英] Undefined behavior - 2 queries

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

问题描述

嗨 -


很棒的小组!


我有2个关于未定义行为的疑问:


1)以下代码是否未定义?


浮动myfunction(浮动f)

{

if(sizeof(float) )& sizeof(int)){

/ *使用慢速普通FP操作* /

}否则{

int i = *( int *)& f;

/ *做巧妙的位 - 浮动操作* /

}

}


它只会尝试以整数形式访问浮点数,这会使得

感觉良好。


2)我见过代码如下:


struct s {

int a;

char * b;

float c;

} s;

int * i =(int *)s;


这不是这个未定义?如果编译器在结构的第一个元素之前插入填充

,这不会爆炸吗?


干杯!

解决方案

Tommy Vercetti写道:


嗨 -


很棒的小组!


我有2个关于未定义行为的查询:


1)以下代码是否未定义?


浮动myfunction(浮点数f)

{

if(sizeof(float)& sizeof(int)){


我已经尝试弄清楚为什么要使用&算子在这里。我不是看见它的b $ b。您的意思是使用!=?


/ *使用慢速普通FP操作* /

}否则{

int i = *(int *)& f;

/ *做巧妙的位 - 浮动操作* /

}

}


它只会尝试以整数形式访问浮点数,这使得

有意义。



行为超出了C标准的范围,很可能也没有将b $ b定义为特定实现的扩展。如果您希望

您的代码可以移植,那就不要这样做了。如果您不需要您的代码便携式,请尝试使用您的编译器提供的扩展程序来确定如何使用
提供的扩展程序。你可能确实有一个正式的编译器

允许你当前的代码作为扩展,但我对此表示怀疑。


2)我见过如下代码:


struct s {

int a;

char * b;

浮动c;

} s;

int * i =(int *)s;


这不是未定义的吗?



这是不允许的。你不能将结构转换为指针。如果你这意味着(int *)& s,那么它是有效的,虽然我个人更喜欢写

& sa


如果编译器在结构的第一个元素之前插入填充

,那么这不会爆炸吗?



对。这不是问题,因为编译器不允许在结构的第一个元素之前插入

填充。




" Tommy Vercetti" < no@spam.com写信息

news:sl ******************** @ nospam.invalid ...


Hi -


很棒的小组!


我有2个关于未定义行为的查询:


1)以下代码是否未定义?


浮动myfunction(浮动f)

{

if(sizeof(float)& sizeof(int)){

/ *使用慢速普通FP操作* /

}其他{

int i = *(int *)& f;

/ *做巧妙的钻头浮动操作* /

}

}


它只会尝试以整数形式访问浮点数,这会让

感觉到。


2)我见过如下代码:


struct s {

int a;

char * b;

浮动c;

} s;

int * i =(int *)s;


这不是未定义的吗?如果编译器在结构的第一个元素之前插入填充

,那么这不会爆炸吗?



第一个例子应该是(sizeof(float)!= sizeof(int))。如果这是

false,则代码几乎是正确的。但是浮点数可能包含一个合法的
浮点数,它是一个整数的陷阱值。你必须在一个漂亮的

病理平台上才能解决这个问题,但是符合要求的b $ b实施可能会让你失望。


第二个例子没问题。结构的第一个元素具有相同的地址

作为整体。不允许预先填写,但填充可以插入

结尾或元素之间。


-

免费游戏和编程好东西。
http://www.personal.leeds。 ac.uk/~bgy1mm


2007年9月8日22:45,Harald van D?3k写道:
< blockquote class =post_quotes>
Tommy Vercetti写道:


>嗨 -

伟大的团队!

我有2个关于未定义行为的查询:

1)以下代码是否未定义?

float myfunction(float f)
{
if(sizeof(float)& sizeof(int)){



我已经尝试弄清楚为什么要使用&算子在这里。我不是看见它的b $ b。你的意思是使用!=?



这是一个优化 - 在机器代码级别,大多数处理器将会b / b $ b $只有一条指令用于&。 />


> / *使用慢速普通FP操作* /
}否则{
int i = *(int *)& f;
/ *做聪明的bit-twiddling浮点运算* /
}


它只会尝试以整数形式访问浮点数。



行为超出了C标准的范围,很可能也没有将b $ b定义为特定实现的扩展。如果您希望

您的代码可以移植,那就不要这样做了。如果您不需要您的代码便携式,请尝试使用您的编译器提供的扩展程序来确定如何使用
提供的扩展程序。有可能你真的有一个正式的编译器

允许你当前的代码作为扩展,但我对此表示怀疑。



即使未定义的行为线在没有被定义的情况下也不会被调用

?例如。在以下代码中:


if(1 == 2){

char * p = NULL;

* p; / * KABOOM! * /

}


当你调用UB的行被保证时,你真的可以调用这个UB

永远不会被调用吗? br />


>


> 2)我见过如下代码:

struct s {
int a;
char * b;
浮动c;
} s;
int * i =(int *)s;

这不是未定义的吗?



这是不允许的。你不能将结构转换为指针。如果你的意思是(b * b)b,那么它是有效的,不过我个人更喜欢写

& s.a。



是的,这是一个错字。


>赢了如果编译器在结构的第一个元素之前插入填充
,这会爆炸吗?



对。这不是问题,因为不允许编译器在结构的第一个元素之前插入

填充。



我想如果你检查一下,编译器可以在它认为合适的情况下将填充插入到

结构中。特别是,如果s的地址不能正确对齐int,那么就必须插入填充!


Hi -

Great group!

I have 2 queries about undefined behavior:

1) Is the following code undefined?

float myfunction(float f)
{
if(sizeof(float) & sizeof(int)) {
/* use slow ordinary FP operations */
} else {
int i = *(int *)&f;
/* do clever bit-twiddling floating-point operation */
}
}

It only attempts to access the float as an integer when this makes
sense.

2) I''ve seen code like the following:

struct s {
int a;
char *b;
float c;
} s;
int *i = (int *) s;

Isn''t this undefined? Won''t this blow up if the compiler inserts padding
before the first element of the struct?

Cheers!

解决方案

Tommy Vercetti wrote:

Hi -

Great group!

I have 2 queries about undefined behavior:

1) Is the following code undefined?

float myfunction(float f)
{
if(sizeof(float) & sizeof(int)) {

I''ve tried figuring out why you would use the & operator here. I''m not
seeing it. Did you mean to use != ?

/* use slow ordinary FP operations */
} else {
int i = *(int *)&f;
/* do clever bit-twiddling floating-point operation */
}
}

It only attempts to access the float as an integer when this makes
sense.

The behaviour is outside the scope of the C standard, and very likely also
not defined as an extension by your particular implementation. If you want
your code to be portable, don''t do it. If you don''t need your code to be
portable, try to figure out how to do it using the extensions your compiler
provides. It''s possible you actually do have a compiler that officially
allows your current code as an extension, but I doubt it.

2) I''ve seen code like the following:

struct s {
int a;
char *b;
float c;
} s;
int *i = (int *) s;

Isn''t this undefined?

This is not allowed. You can''t convert a structure to a pointer. If you
meant (int *) &s, then it''s valid, though I personally prefer to write
&s.a.

Won''t this blow up if the compiler inserts padding
before the first element of the struct?

Right. Which is not a problem, since the compiler is not allowed to insert
padding before the first element of the struct.



"Tommy Vercetti" <no@spam.comwrote in message
news:sl********************@nospam.invalid...

Hi -

Great group!

I have 2 queries about undefined behavior:

1) Is the following code undefined?

float myfunction(float f)
{
if(sizeof(float) & sizeof(int)) {
/* use slow ordinary FP operations */
} else {
int i = *(int *)&f;
/* do clever bit-twiddling floating-point operation */
}
}

It only attempts to access the float as an integer when this makes
sense.

2) I''ve seen code like the following:

struct s {
int a;
char *b;
float c;
} s;
int *i = (int *) s;

Isn''t this undefined? Won''t this blow up if the compiler inserts padding
before the first element of the struct?

The first example should read if(sizeof(float) != sizeof(int)). If this is
false, the code is almost correct. However the float could contain a legal
float which is a trap value for an integer. You''d have to be on a pretty
pathological platform for this to be a problem, but a conforming
implementation could crash you out.

The second example is OK. The first element of a struct has the same address
as the whole. No prepadding is allowed, though padding may be inserted at
the end or between elements.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


On 8 Sep 2007 at 22:45, Harald van D?3k wrote:

Tommy Vercetti wrote:

>Hi -

Great group!

I have 2 queries about undefined behavior:

1) Is the following code undefined?

float myfunction(float f)
{
if(sizeof(float) & sizeof(int)) {


I''ve tried figuring out why you would use the & operator here. I''m not
seeing it. Did you mean to use != ?

It''s an optimization - at the machine code level, most processors will
have a single instruction for &.

> /* use slow ordinary FP operations */
} else {
int i = *(int *)&f;
/* do clever bit-twiddling floating-point operation */
}
}

It only attempts to access the float as an integer when this makes
sense.


The behaviour is outside the scope of the C standard, and very likely also
not defined as an extension by your particular implementation. If you want
your code to be portable, don''t do it. If you don''t need your code to be
portable, try to figure out how to do it using the extensions your compiler
provides. It''s possible you actually do have a compiler that officially
allows your current code as an extension, but I doubt it.

Even though the undefined-behavior lines aren''t called in situations
where they''d be undefined? E.g. in the following code:

if(1==2) {
char *p=NULL;
*p; /* KABOOM! */
}

can you really call this UB when the line invoking UB is guaranteed
never to be called?

>

>2) I''ve seen code like the following:

struct s {
int a;
char *b;
float c;
} s;
int *i = (int *) s;

Isn''t this undefined?


This is not allowed. You can''t convert a structure to a pointer. If you
meant (int *) &s, then it''s valid, though I personally prefer to write
&s.a.

Yes, that was a typo.

>Won''t this blow up if the compiler inserts padding
before the first element of the struct?


Right. Which is not a problem, since the compiler is not allowed to insert
padding before the first element of the struct.

I think if you check, the compiler is allowed to insert padding into
structs as it sees fit. In particular, if the address of s isn''t
properly aligned for an int then it will have to insert padding!


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

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