`
ChuanSu
  • 浏览: 52800 次
  • 性别: Icon_minigender_1
  • 来自: 石家庄
社区版块
存档分类
最新评论

C/C++ union size

阅读更多
C/C++,对于union的 size,普遍说法是,union的大小和其所包含的成员中size最大的members一致。
int main(void){
union u_tag {
	char a[13];
	int i;
}u;
printf("%d",sizeof(u));
 return 0;
}

很快可以看出,此union中,其size最大的member是 char a[13],为 13 bytes。
但是其union的size 却是 16。

union在本质上是 variable。
The C programming language 写道
A union is a variable that may hold (at different times) objects of different types and sizes,
with compiler keeping track of size and alignment requirements

分析一个variable,需要不仅要分析其在内存中所占的大小,即 size,还要分析其在内存中的对齐方式,即 alignment。

union u最少需要分配需13 bytes的内存空间,这样才既可以存储char a[13],又有足够空间来存储 int i。
对任何一种 varaible都有 默认的alignment 要求,比如int型 的alignment是4 bytes,char是1 bytes. union也不例外,其alignment要和其成员中alignment值最大的成员保持一致。在上面的例子,union u的alignment需要和int i的alignment一致。即4 bytes.
所以union在分配13 bytes的 内存空间的同时,还需要在最后padding(填补) 3个无意义的bytes来满足 4-bytes的alignment的要求(size为4的整数倍)所以其size为16 bytes。

在内存中分析union时,只需要分析union中,所占size最大的成员和alignment值最大的成员,如定义所说的既要满足size requirement 又要满足 alignment requirement.

http://chuansu.iteye.com/blog/1487350这里对 data alignment以及struct size有很好的诠释。
int main(void){

union u_tag {
	char a[13];
	int i;
};
struct tt{
		short d;
		union u_tag u;
	}t;
printf("%d",sizeof(t));
 return 0;
}


struct t 的成员中包含 union u。 由于union u的alignment为4,在内存中,u与short d之间需要填补两个bytes来满足 union u的offset是 其alignment的整数倍。
最后需要看看struct内所有member的总size是不是为 此struct alignment的整数倍,在这里struct t的alignment就是union u的alignment(与其alignment值最大的成员保持一致).
所以最后结果为 sizeof(short d)+2 padding bytes +sizeof(union u)=20。

union与struct的区别

首先 union仅仅为一个variable 而struct则是 a collection of variables,是集合。
union在内存中,每一个成员或者variable都是起始于同一位置,或者说其所有的member的offset是一样的。这也是为什么union一次只能存储一个variable的原因。而struct中的成员显然不是。
引用
In effect, a union is a structure in which all memebers have offset zero from the base, the structure is big enough to hold the "widest" memeber and the alignment is appropriate for all of the types in the union.
1
0
分享到:
评论

相关推荐

    LeetCode最全代码

    201 | [Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [C++](./C++/bitwise-and-of-numbers-range.cpp) [Python](./Python/bitwise-and-of-numbers-range.py) | _...

    c++ 面试题 总结

    6.下面是C语言中两种if语句判断方式。请问哪种写法更好?为什么? int n; if (n == 10) // 第一种判断方式 if (10 == n) // 第二种判断方式 如果少了个=号,编译时就会报错,减少了出错的可能行,可以检测出是否少...

    c++并查集优化(基于size和rank)

    基于size的优化是指:当我们在指定由谁连接谁的时候,...基于size的代码: UnionFind3.h #ifndef UNION_FIND3_H_ #define UNION_FIND3_H_ #include #include namespace UF3 { class UnionFind { private: int* p

    C语言扫雷

    摘要:VC/C++源码,游戏编程,扫雷 C语言扫雷源码,其实是模拟扫雷的游戏:union REGS regs;  int size=15;/*用于表示每个方块的大小(正方形的边长)*/  int pix,piy=50;/*pix,piy是矩阵的偏移量*/  char b[2]="1...

    stdafx.h代码

    stdafx.h的代码// This is a part of the Microsoft Foundation Classes C++ library. // Copyright (C) 1992-1998 Microsoft Corporation // All rights reserved. // // This source code is only intended as a ...

    c++基础教程,简洁扼要

    因为在C++中类和数据结构的概念太相似了,所以这两个关键字struct和class的作用几乎是一样的(也就是说在C++中struct定义的 类也可以有成员函数,而不仅仅有数据成员)。两者定义的类的唯一区别在于由class定义的类...

    determine_c_or_cpp:从C ++以及各种版本中以编程方式确定C

    // returns 8 in C and 100 in C++ } 我在使用了此示例 C使用struct,union和enum标签作为名称空间的原始形式, 。 因此,以下代码将为C提供sizeof int,为C ++提供sizeof struct T # include extern int T; int...

    C语言FAQ 常见问题列表

    难道在C语言中一个结构不能包含指向自己的指针吗? o 2.7 怎样建立和理解非常复杂的声明?例如定义一个包含 N 个指向返回指向字符的指针的函数的指针的数组? o 2.8 函数只定义了一次, 调用了一次, 但编译器提示...

    你必须知道的495个C语言问题(PDF)

    难道在C语言中一个结构不能包含指向自己的指针吗? . . . . 3 1.7 怎样建立和理解非常复杂的声明?例如定义一个包含N 个指向返 回指向字符的指针的函数的指针的数组? . . . . . . . . . . . . . . 3 1.8 函数只定义...

    语言程序设计课后习题答案

    在C++中,有两种给出注释的方法:一种是延用C语言方法,使用"/*"和"*/"括起注释文字。另一种方法是使用"//",从"//"开始,直到它所在行的行尾,所有字符都被作为注释处理。 2-8 什么叫做表达式?x = 5 + 7是一个...

Global site tag (gtag.js) - Google Analytics