在一个union类型结构中,所有的成员公用同样的存储空间,其占用的大小为其成员中需要空间最大者;

union本身只保留一块地址空间,因为只有一个成员真正存储于该地址, 但这块地址也要满足内存对齐原则。

 #include <stdio.h>

 union U1 {
int a;
char b;
}; union U2 {
char a[];
double b;
}; union U3 {
U1 a;
U2 b;
char c[];
};
int main() {
printf("size of U1: %ld\n", sizeof(U1));
printf("size of U2: %ld\n", sizeof(U2));
printf("size of U3: %ld\n", sizeof(U3));
return ;
}

注意U3对齐还是以前面的double进行对齐,所以应该是对齐到下一个8的位数,也就是24,而不是20.

 root@xxj-VirtualBox:~/interview# ./union
size of U1:
size of U2:
size of U3:

Union大小的更多相关文章

  1. union 和struct大小计算

    一.字节对齐 现代计算机的内存空间是按照字节(byte)来划分的,字节对齐的意思是在给特定变量类型分配内存空间的时候,变量的内存地址是它本身变量类型大小的整数倍.比如,给int类型的变量a分配地址空间 ...

  2. C语言深度剖析-笔记

    关键字: C语言关键字32个: 关键字                                         意 义 auto                           声明自动变 ...

  3. C语言:结构体,共用体

    结构体: 一个变量,存储不同类型的数据项共用体:一个变量,存储不同类型的数据项,相同的内存位置,存储不同的数据类型 #include <stdio.h> #include <stri ...

  4. 联合体union和大小端(big-endian、little-endian)

    1.联合体union的基本特性——和struct的同与不同 union,中文名“联合体.共用体”,在某种程度上类似结构体struct的一种数据结构,共用体(union)和结构体(struct)同样可以 ...

  5. struct与union字节大小的终极解释

    1.字节对齐的细节和编译器实现相关,但一般而言,如在windows下,就VC而言,满足一下三个准则:1) 结构体变量的首地址能够被其最宽基本类型成员的大小所整除:2) 结构体每个成员相对于结构体首地址 ...

  6. 大小端; union

    #include<stdio.h> #include <stdlib.h> typedef union { int m; char a[4]; }Node; int main ...

  7. union关键字 与大小端模式

    union 关键字(主要用来压缩空间,如果一些数据不可能同一时间同时用到,可是考虑使用union) union关键字声明的变量称之为联合体变量: (1)联合体变量只配置一个足够大的空间来容纳最大长度的 ...

  8. c语言:union,大小端

    union: 不允许只用联合变量名作赋值或其它操作. 也不允许对联合变量作初始化赋值,赋值只能在程序中进行. 小端存储: 以字节为单位,低存低,高存高. 任何数据在内存中都是以二进制(1或着0)顺序存 ...

  9. C++基础之---union联合体大小分析

    #include <iostream> using namespace std; union un { int a[7]; double b; char c[10]; int d[3]; ...

随机推荐

  1. libavcodec/dxva2.h:40:5: error: unknown type name 'IDirectXVideoDecoder'

    gcc 4.9.2 编译 ffmpeg-git-1aeb88b 是出现如下错误 > FFmpeg fails to make with: > > CC libavcodec/dxva ...

  2. 【JAVA、C++】LeetCode 001 Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  3. Java for LeetCode 076 Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  4. POJ1704 Georgia and Bob (阶梯博弈)

    Georgia and Bob Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u Subm ...

  5. HDU2067卡特兰数

    小兔的棋盘 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  6. intellij 提交代码到git

    .配置git .create git repository .git-->add commit Directory .提交代码 git remote add origin https://git ...

  7. 多源最短路(codevs 1077)

    题目描述 Description 已知n个点(n<=100),给你n*n的方阵,a[i,j]表示从第i个点到第j个点的直接距离. 现在有Q个询问,每个询问两个正整数,a和b,让你求a到b之间的最 ...

  8. 为什么C++类定义中,数据成员不能被指定为自身类型,但可以是指向自身类型的指针或引用?为什么在类体内可以定义将静态成员声明为其所属类的类型呢 ?

    static的成员变量,不是存储在Bar实例之中的,因而不会有递归定义的问题. 类声明: class Screen: //Screen类的声明 1 类定义: class Screen{ //Scree ...

  9. Linux 内核中断内幕

    转自:http://www.ibm.com/developerworks/cn/linux/l-cn-linuxkernelint/index.html#resources Linux 内核中断内幕 ...

  10. jquery easy ui 1.3.4 事件与方法的使用(3)

    3.1.easyui事件 easyui事件可以在构建的时候就通过属性方式添加上去,比如在panel收缩的时候添加一个事件,在构建的时候代码如下 onCollapse: function () { al ...