在sizeof的C / C联合++ [英] sizeof a union in C/C++

查看:137
本文介绍了在sizeof的C / C联合++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是C / C ++ sizeof运算工会?它是sizeof运算里面的最大的数据类型?如果是这样,编译器如何计算如何移动堆栈指针如果工会的小数据类型之一,是积极的?

What is the sizeof the union in C/C++? Is it the sizeof the largest datatype inside it? If so, how does the compiler calculate how to move the stack pointer if one of the smaller datatype of the union is active?

推荐答案

标准答案在第9.5节的所有问题:

The Standard answers all questions in section 9.5:

在联合中,数据成员的至多一个可以在任何时间被激活,即,数据成员的至多一个的值可以在任何时间被存储在一个联盟。 [注:一种特殊的担保是为了简化使用工会提出:如果POD联合包含几个POD-结构共享一个通用初始序列(9.2),如果这POD联合类型的对象包含的一个在POD-结构,它允许检查任何POD结构成员的共同初始序列;见9.2。 ]工会的大小足以容纳最大的数据成员。就好像它是一个结构的鞋底构件的每个数据成员被分配。

In a union, at most one of the data members can be active at any time, that is, the value of at most one of the data members can be stored in a union at any time. [Note: one special guarantee is made in order to simplify the use of unions: If a POD-union contains several POD-structs that share a common initial sequence (9.2), and if an object of this POD-union type contains one of the POD-structs, it is permitted to inspect the common initial sequence of any of POD-struct members; see 9.2. ] The size of a union is sufficient to contain the largest of its data members. Each data member is allocated as if it were the sole member of a struct.

这意味着每个成员共享相同的内存区域。有的最多一个成员活跃,但你不能找出哪一个。你将不得不存储有关当前活动成员自己在别处的信息。存储除了这样的标志工会(例如有一个整数作为类型标志和工会作为数据存储的结构体)会给你一个所谓的歧视联盟:它知道什么类型的工会它是目前活动之一。

That means each member share the same memory region. There is at most one member active, but you can't find out which one. You will have to store that information about the currently active member yourself somewhere else. Storing such a flag in addition to the union (for example having a struct with an integer as the type-flag and an union as the data-store) will give you a so called "discriminated union": An union which knows what type in it is currently the "active one".

一个常见的​​用途是在词法分析器,在那里你可以有不同的标记,但根据令牌,你有不同的信息存储(将到每个结构显示什么是公共初始序列):

One common use is in lexers, where you can have different tokens, but depending on the token, you have different informations to store (putting line into each struct to show what a common initial sequence is):

struct tokeni {
    int token; /* type tag */
    union {
        struct { int line; } noVal;
        struct { int line; int val; } intVal;
        struct { int line; struct string val; } stringVal;
    } data;
};

标准允许您访问每个成员的,因为这是每个人的公共初始序列。

The Standard allows you to access line of each member, because that's the common initial sequence of each one.

有存在的编译器扩展,允许访问当前已存储其中一个它的价值所有成员直索。允许与每个部件之间不同类型的存储的位有效reinter pretation。例如,下面的可以用于一个浮点变量解剖成2无符号短裤

There exist compiler extensions that allow accessing all members disregarding which one currently has its value stored. That allows efficient reinterpretation of stored bits with different types among each of the members. For example, the following may be used to dissect a float variable into 2 unsigned shorts:

union float_cast { unsigned short s[2]; float f; };

这可以编写低级code来的时候非常方便。如果编译器不支持该扩展,但你也无妨,你写code,其结果没有定义。所以可以肯定你的编译器支持它,如果你使用的伎俩。

That can come quite handy when writing low-level code. If the compiler does not support that extension, but you do it anyway, you write code whose results are not defined. So be certain your compiler has support for it if you use that trick.

这篇关于在sizeof的C / C联合++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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