1)union是几个不同类型的变量共占一段内存(相互覆盖);struct是把不同类型的数据组合成一个整体
2)对齐方式略有区别;union不需要+,只需要拿出对齐后的最长

structure union

Keyword struct defines a structure.

Keyword union defines a union.

Example structure declaration:

struct s_tag
{
int ival;
float fval;
char *cptr;
}s;

Example union declaration:

union u_tag
{
int ival;
float fval;
char *cptr;
}u;

Within a structure all members gets memory allocated and members have addresses that increase as the declarators are read left-to-right. That is, the members of a structure all begin at different offsets from the base of the structure. The offset of a particular member corresponds to the order of its declaration; the first member is at offset 0. The total size of a structure is the sum of the size of all the members or more because of appropriate alignment.

For a union compiler allocates the memory for the largest of all members and in a union all members have offset zero from the base, the container is big enough to hold the WIDEST member, and the alignment is appropriate for all of the types in the union.

When the storage space allocated to the union contains a smaller member, the extra space between the end of the smaller member and the end of the allocated memory remains unaltered.

Within a structure all members gets memory allocated; therefore any member can be retrieved at any time.

While retrieving data from a union the type that is being retrieved must be the type most recently stored. It is the programmer's responsibility to keep track of which type is currently stored in a union; the results are implementation-dependent if something is stored as one type and extracted as another.

One or more members of a structure can be initialized at once.

A union may only be initialized with a value of the type of its first member; thus union u described above (during example declaration) can only be initialized with an integer value.

 
 

struct和union的区别的更多相关文章

  1. struct 和union的区别

    union ( 共用体):构造数据类型,也叫联合体  用途:使几个不同类型的变量共占一段内存(相互覆盖) struct ( 结构体 ):是一种构造类型 用途: 把不同的数据组合成一个整体——自定义数据 ...

  2. struct与 union的基本用法

    结构体与联合体是C语言的常见数据类型,可对C的基本数据类型进行组合使之能表示复杂的数据结构,意义深远,是优异代码的必备工具.一.        struct与 union的基本用法,在语法上union ...

  3. 重新学struct,边界对齐,声明……与Union的区别

    在内存中,编译器按照成员列表顺序分别为每个结构体变量成员分配内存,当存储过程中需要满足边界对齐的要求时,编译器会在成员之间留下额外的内存空间. 如果想确认结构体占多少存储空间,则使用关键字sizeof ...

  4. <转> Struct 和 Union区别 以及 对内存对齐方式的说明

    转载地址:http://blog.csdn.net/firefly_2002/article/details/7954458 一.Struct 和 Union有下列区别: 1.在存储多个成员信息时,编 ...

  5. <struct、union、enum>差异

    关于C++和C的区别 区别最大的是struct,C++中的struct几乎和class一样了,可以有成员函数,而C中的struct只能包含成员变量. enum,union没区别. struct的定义 ...

  6. 关于C中struct和union长度的详解

    这几天看<代码大全>中的第十三章---不常见的数据类型,里面讲解到了C语言中的struct以及对指针的解释,联想到以前看过相关的关于C语言中stuct长度的文章,只是现在有些淡忘了,因此今 ...

  7. 【转】C/C++ struct/class/union内存对齐

    原文链接:http://www.cnblogs.com/Miranda-lym/p/5197805.html struct/class/union内存对齐原则有四个: 1).数据成员对齐规则:结构(s ...

  8. struct与class的区别

    C++中的struct是对C中struct的扩充,它已经不再只是一个包含不同数据类型的数据结构,因为其扩充了太多功能.总的来说,C++中struct和class极其相似,比如,struct能包含成员函 ...

  9. C#中struct和class的区别详解

    本文详细分析了C#中struct和class的区别,对于C#初学者来说是有必要加以了解并掌握的. 简单来说,struct是值类型,创建一个struct类型的实例被分配在栈上.class是引用类型,创建 ...

随机推荐

  1. CentOS 7下编译安装Boost_1_57_0

    之前对库的理解太肤浅(现在也仍很肤浅),导致走了挺多的弯路,现记录以备后查. 现在可以从Boost官网下载到最新的Boost源代码boost_1_57_0.tar.gz. 现将步骤记录如下: 1. 解 ...

  2. 色彩转换——RGB & HSI

    RGB to HSI I=(R+G+B)/3; S=1-3*min(R,G,B)/(R+G+B); H = cos^(-1)((0.5*((R-G)+(R-B))) / ((R-G)^2 + (R-B ...

  3. Android开发技巧——自定义控件之增加状态

    Android开发技巧--自定义控件之增加状态 题外话 这篇本该是上周四或上周五写的,无奈太久没写博客,前几段把我的兴头都用完了,就一拖再拖,直到今天.不想把这篇拖到下个月,所以还是先硬着头皮写了. ...

  4. UNIX/Linux C 程序员需要掌握的七种武器

    我是一名普通的软件工程师,不是什么技术大牛.这篇文章所提到的"七种武器"只是我这些年工作经验的一点体会和感悟,如果有错误的地方,还请大家指正. (一)C语言 作为一名C程序员,熟练 ...

  5. rails中weill_paginate的paginate方法中不能使用额外参数的解决办法

    我们知道高版本中的rails中的分页功能已经放在will_paginate这个gem中,我们在控制器方法中往往需要调用其paginate方法来实现分页数据集控制,举个例子:正常的情况我们想要每页显示1 ...

  6. Mac OS X 简单的方法知道何时来电了

    最近本猫所在的小区时常停电,往往半夜或是凌晨才来电啊!早上起来本猫在想如何知道确切的来电时间,但又不费事的方法呢. 方法一是用手机录音器录音,因为来电后门禁会发出"滴"的一声,所以 ...

  7. python标准库Beautiful Soup与MongoDb爬喜马拉雅电台的总结

    Beautiful Soup标准库是一个可以从HTML/XML文件中提取数据的Python库,它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式,Beautiful Soup将会节省数小 ...

  8. solr研磨之游标分页

    普通分页 当需要深度分页的时候,比如查询第10000页数据,每页显示10条,意味着需要提取前10000 x 10 页的数据,并将这100000条数据缓存在内存中,然后在内存中进行排序.最后返回最后10 ...

  9. IOS常用第三方库《转》

    UI 动画 网络相关 Model 其他 数据库 缓存处理 PDF 图像浏览及处理 摄像照相视频音频处理 响应式框架 消息相关 版本新API的Demo 代码安全与密码 测试及调试 AppleWatch ...

  10. JVM(HotSpot) 7种垃圾收集器的特点及使用场景

    这里讨论的收集器基于JDK1.7Update 14之后的HotSpot虚拟机,这个虚拟机包含的所有收集器如下图3-5所示: 上图展示了7种作用于不同分代的收集器,如果两个收集器之间存在连线,就说明它们 ...