结构和联合的区别

1,union共享内存单元所以按占用内存最大的那个成员变量分配内存,
  class、struct要考虑对齐(即将内存小的那个成员补齐到和内存单元最大的那个)
  占用内存计算方式要视情况而定
 

C/C++ code
union u { char c; int i; } sizeof(u) = 4 struct s0 { int i; char c; } sizeof(s0) = 8; struct s1 { int i; char c1; char c2; } sizeof(s1) = 8 struct s2 { int i; char c1; char c2; char c3; char c4; char c5; } sizeof(s2) = 12 struct s3 { double d; char c; } sizeof(s3) = 16


2,对联合中一个的更新会影响到其他成员而结构数据成员拥有独立内存所以不会有影响

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值