关于警告的问题 [英] Question about warning

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

问题描述

当我编译它时(使用GCC和优化):


void foo(void)

{

int a;

浮动f = *((浮动*)& a);

}


我收到警告: 解除引用类型 - 惩罚指针将破坏

严格别名规则。这是否意味着f和a可以存储在

相同的内存位置?

-

Daniel Sj?blom

删除_NOSPAM以通过邮件回复

解决方案



" Daniel Sj?blom" < DS ****** @ mbnet.fi_NOSPAM>写了


当我编译它时(使用GCC和优化):

void foo(void)
{
int a;
float f = *((float *)& a);
}
我收到警告:取消引用类型 - 惩罚指针会破坏
严格 - 通货规则这是否意味着f和a可以存储在相同的内存位置?



编号允许编译器对其喜欢的任何内容发出警告(所以

复杂的编译器可以警告函数中的英文拼写

,例如setcolour())。

你的函数在精心设计的代码中相当无用,因为按顺序

重新解释整数作为浮点数是不可移植的,而不是人类有意义的,而不是你应该做的事情(尽管在实践中你

可能需要这样做,例如通过直接对比特来加速浮点运算

)。所以编译器就此发出警告。


On Sun,2004年6月27日19:23:46 +0300,Daniel Sj?blom

< ds******@mbnet.fi_NOSPAM>在comp.lang.c中写道:

当我编译它时(使用GCC和优化):

void foo(void)
{
int a;
float f = *((float *)& a);
}
我收到警告:dereferencing type-punned pointer将打破
严格别名规则。这是否意味着f和a可以存储在相同的内存位置?




这意味着此代码会产生未定义的行为。你违反了规则,并且C标准不再对

结果提出任何要求。执行此代码时发生的任何事情,甚至是编译的
都超出了C语言的范围。


-

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


你好!

我不知道你想用代码实现什么,但我的猜测是

u想要投出任何值''a''必须

' F。 (将int转换为float)



---------------

void foo(void )

{

int a;

float f =(float)a;

}

---------------

然而编译器会警告使用'a''
$ b的uninitzialized使用$ b // J?rgen Tapani

" Daniel Sj?blom" < DS ****** @ mbnet.fi_NOSPAM> skrev i meddelandet

新闻:40 ********************** @ news.mbnet.fi ...

当我编译它时(使用GCC和优化):

void foo(void)
{
int a;
float f = *((浮动*)& a);
}
我收到警告:取消引用类型惩罚指针将破坏
严格别名规则。这是否意味着f和a可以存储在相同的内存位置?
-
Daniel Sj?blom
删除_NOSPAM以通过邮件回复



When I compile this (with GCC and optimizations on):

void foo(void)
{
int a;
float f = *((float *) &a);
}

I get the warning : "dereferencing type-punned pointer will break
strict-aliasing rules". Does this mean that f and a can be stored in the
same memory location?
--
Daniel Sj?blom
Remove _NOSPAM to reply by mail

解决方案


"Daniel Sj?blom" <ds******@mbnet.fi_NOSPAM> wrote


When I compile this (with GCC and optimizations on):

void foo(void)
{
int a;
float f = *((float *) &a);
}

I get the warning : "dereferencing type-punned pointer will break
strict-aliasing rules". Does this mean that f and a can be stored in the
same memory location?


No. A compiler is allowed to warn about anything it likes (so a
sophisticated compiler could warn about British spelling in a function
called setcolour(), for example).
Your function is fairly useless in well-designed code, because a bitwise
reinterpretation of an integer as a float is non-portable, not human
meaningful, and not something you should need to do (though in practise you
might need to do it, for instance to speed up the floating point operations
by twiddling bits directly). So the compiler warns about it.


On Sun, 27 Jun 2004 19:23:46 +0300, Daniel Sj?blom
<ds******@mbnet.fi_NOSPAM> wrote in comp.lang.c:

When I compile this (with GCC and optimizations on):

void foo(void)
{
int a;
float f = *((float *) &a);
}

I get the warning : "dereferencing type-punned pointer will break
strict-aliasing rules". Does this mean that f and a can be stored in the
same memory location?



It means that this code produces undefined behavior. You have broken
the rules, and the C standard no longer places any requirements on the
result. Anything that happens when this code is executed, or even
compiled, is beyond the scope of the C language.

--
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


Hello!
I dont know what u want to achive with the code but my guess would be that
u want to cast whatever value ''a'' has to
''f''. (converting int to float)
Like
---------------
void foo(void)
{
int a;
float f = (float)a;
}
---------------
However the compiler will warn for use of an uninitzialized use of ''a''
//J?rgen Tapani
"Daniel Sj?blom" <ds******@mbnet.fi_NOSPAM> skrev i meddelandet
news:40**********************@news.mbnet.fi...

When I compile this (with GCC and optimizations on):

void foo(void)
{
int a;
float f = *((float *) &a);
}

I get the warning : "dereferencing type-punned pointer will break
strict-aliasing rules". Does this mean that f and a can be stored in the
same memory location?
--
Daniel Sj?blom
Remove _NOSPAM to reply by mail



这篇关于关于警告的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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