【问题标题】:Why structs took more space than it needs? [duplicate]为什么结构占用的空间比它需要的多? [复制]
【发布时间】:2015-01-28 17:11:56
【问题描述】:

我看到,当我检查结构的大小时,它占用了 36 个字节,但相同的变量(没有结构)占用了 33 个字节。 为什么?

【问题讨论】:

  • 可能发生了一些填充。发个例子
  • 你能举出两个例子吗?

标签: c memory-management struct


【解决方案1】:
sizeof(struct) >= sum of all the structure members.

因为对齐要求可能会有额外的填充。这就是您看到结构的大小比您想象的要大的原因。

【讨论】:

  • 结构中间也可以有填充。
猜你喜欢
  • 2021-07-08
  • 1970-01-01
  • 2020-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-05
  • 1970-01-01
  • 2016-05-08
相关资源
最近更新 更多