找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 295|回复: 1

Question2.13:Why does sizeof report a larger size than I expect for a structure type。。。。

[复制链接]

210

主题

371

回帖

0

积分

管理员

积分
0
发表于 2013-7-1 13:09:29 | 显示全部楼层 |阅读模式

Q: Why does sizeof report a larger size than I expect for a structure type, as if there were padding at the end?
A: Padding at the end of a structure may be necessary to preserve alignment when an array of contiguous structures is allocated. Even when the structure is not part of an array, the padding remains, so that sizeof can always return a consistent size.
See also question 2.12.

210

主题

371

回帖

0

积分

管理员

积分
0
 楼主| 发表于 2013-7-1 13:22:17 | 显示全部楼层
问题:为什么我们用sizeof时,得到的空间大小会比我所期望的结构体类型的还要大,似乎在结构体末尾补齐了?
回答:当大量连续的结构体被分配空间的时候,为了维持队列对齐,在结构体的末尾字节补齐是很有必要的。即使当结构体不是连续结构体中的一部分时,仍然会保持字节补齐,以至于我们用sizeof得到的是一致的空间大小。参考问题2.12。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-1 13:58 , Processed in 0.076001 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表