联合的默认值是否为0? [英] Does a union always have default value of zero?

查看:188
本文介绍了联合的默认值是否为0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请让我们考虑以下代码:

Please let us consider following code:

#include <iostream>
using namespace std;

union{
 int i;
}u;

int main(){

     int k=5;
     cout<<k+u.i<<endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}

这段代码显示我输出5,对我来说意味着变量i在union结构中有默认值= 0,但在ideone.com上的相同代码显示类似这样的警告

This code shows me output 5,what means to me is that,variable i in union structure has default value=0, but the same code on ideone.com shows warning like this

prog.cpp:6: warning: non-local variable ‘<anonymous union> u’ uses anonymous type and then prints  5 as well, and last one  core of this problem comes  from algorithm calculate  

平方根的倒数,这里是代码

Reciprocal of the square root and here is code

#include<iostream>
#include<math.h>
using namespace std;

float invsqrt(float x){

    float xhalf=0.5f*x;

    union{
         float x;
         int i;
    }u;

   u.x=x;
   u.i=0x5f3759df-(u.i>>1);
   x=u.x*(1.5f-xhalf*u.x*u.x);

   return x;
}

int main(){

    float  x=234;
    cout<<invsqrt(x)<<endl;

    return 0;
}

它也显示我的输出,但我的问题是,好,我的意思是因为int i不是initailized,任何编译器认为它的价值为零?我很好奇,请告诉我一些关于这一点,也是如果一些不清楚我的问题说我,我不是英语母语。

It shows me output also,but my question is that is it a this code good?i meant that because int i is not initailized ,can any compiler consider it's value as zero?i am curious and please tell me something about this,also if something is not clear from my question say me,i am not English native speaker.

推荐答案


联盟的默认值是否为零?

Does a union always have default value of zero?

语言标准说:


或线程存储持续时间没有初始化
,则:

If an object that has static or thread storage duration is not initialized explicitly, then:


  • 如果它具有指针类型,则初始化为空指针;

  • 如果它具有算术类型,则初始化为(正或无符号)零;

  • 如果它是聚合, (递归地)根据这些规则,
    和任何填充被初始化为零比特;

  • 如果它是联合,则根据这些规则初始化
    规则,并且任何填充都初始化为零位;

  • if it has pointer type, it is initialized to a null pointer;
  • if it has arithmetic type, it is initialized to (positive or unsigned) zero;
  • if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
  • if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

第一个代码示例, ui 将初始化为零。

So, in your first code sample, u.i will be initialised to zero.

我不确定第二个代码示例所有。我看不到 union 的地方。我相当怀疑你打算使用 struct 而不是 union 。但是请注意,两个代码示例是非常不同的,因为 union 在这第一个有静态存储持续时间,第二个联合具有自动存储持续时间。这导致未初始化变量的完全不同的语义。

I'm not sure about the second code sample at all. I cannot see the point of the union there. I rather suspect that you meant to use a struct rather than a union. But note that the two code examples are very different because the union in this first has static storage duration and in the second the union has automatic storage duration. This results in completely different semantics for uninitialized variables.

这篇关于联合的默认值是否为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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