包含两个指针的结构体的大小 [英] Size of struct that contains two pointers

查看:42
本文介绍了包含两个指针的结构体的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个结构体的大小是多少?(32 位系统计算.不是 64 位.)

struct list_element{短数据;struct list_element* 下一个;struct list_element* 上一个;};

我试过用这些公式计算尺寸:

<块引用>

  1. (sizeof(list_element*) + sizeof(short)) + ((sizeof(list_element*) * 2)

    • (4 + 2) + (4 * 2) = 6 + 8 = 14
  2. (sizeof(short)) + (sizeof(list_element*) * 2)

    • 2 + (4 * 2) = 2 + 8 = 10
  3. (sizeof(list_element*) + sizeof(list_element*) + sizeof(short)) + (sizeof(list_element*) * 2)

    • (4 + 4 + 2) + (4 * 2) = 10 + 8 = 18
  4. (sizeof(list_element*) + sizeof(list_element*) + sizeof(short))

    • (4 + 4 + 2) = 10

然而,他们没有返回正确的答案.你用什么公式来计算这个结构体的大小?

更新:

我的老师说我们重新忽略了数据对齐......希望这不会让任何人失望,因为你习惯于用你的代码和结构来处理数据对齐......

更新 2感谢您的帮助和数据对齐的介绍.

答案是 10 没有数据对齐......不知道为什么我这么急于在 C 中使用数据对齐......好玩吗?

此外,数据对齐的答案是 12.正如你们所解释的,您必须对短数据进行数据对齐以匹配整数.因此,您有 (2 + (2 个额外字节)) + 4 + 4 = 12.

解决方案

结构体的大小由下式给出:

size_t size = sizeof(struct list_element);

事实上,你有两个指向结构的成员只是意味着您要两次添加指针的大小.在 32 位构建中,sizeof 将解析为每个指针额外 4 个字节,在 64 位构建中,它会导致每个指针额外 8 个字节.

另一件需要注意的事情是,结构的大小可能不仅仅是各个成员的 sizeof 的总和,因为结构中的存储通常是为了对齐而填充的.因此,在您的 short 和下一个成员之间,填充将导致额外的大小.

我使用可能这个词的原因是,如果pragma pack 指令在您的源代码中使用,可以更改打包对齐方式,从而导致不同的值大小.

关于结构对齐填充的两个很好的讨论:此处的一般性讨论这里如何减少内存占用.第二个链接特别有趣,因为它涉及结构对齐、填充和位字段,以及它们如何影响内存使用.

What is the size of this struct? (32 bit system calculation. Not 64 bit.)

struct list_element
{
  short data;
  struct list_element* next;
  struct list_element* prev;
};

I have tried calculating the size with these formulas:

  1. (sizeof(list_element*) + sizeof(short)) + ((sizeof(list_element*) * 2)

    • (4 + 2) + (4 * 2) = 6 + 8 = 14
  2. (sizeof(short)) + (sizeof(list_element*) * 2)

    • 2 + (4 * 2) = 2 + 8 = 10
  3. (sizeof(list_element*) + sizeof(list_element*) + sizeof(short)) + (sizeof(list_element*) * 2)

    • (4 + 4 + 2) + (4 * 2) = 10 + 8 = 18
  4. (sizeof(list_element*) + sizeof(list_element*) + sizeof(short))

    • (4 + 4 + 2) = 10

However, they do not return the correct answer. What formula do you use to calculate the size of this struct?

Update:

My teacher says we a re ignoring data alignment... Hopefully that does not throw anyone off too much since you are used handling data alignment with your code and structs...

Update 2 Thank you for the help and the introduction to data alignment.

The answer was 10 without data alignment... Not sure why I am in such a rush to work with data alignment in C... Is it fun?

Also, the answer with data alignment is 12. As you guys explained, you have to data align the short to match the integers. Therefore, you have (2 + (2 additional bytes)) + 4 + 4 = 12.

解决方案

The size of the struct is given by:

size_t size = sizeof(struct list_element);

The fact that you have two members that are pointers to the struct just means you are adding the size of a pointer, twice. On a 32 bit build, sizeof would resolve to an additional 4 bytes per pointer, on a 64 bit build, it would result in an additional 8 bytes per pointer.

Another thing to be aware of is that the size of your struct is likely not simply the sum of the sizeof's of the individual members, as storage in a struct is often padded for alignment purposes. So, between your short, and the next member, the padding will result in additional size.

The reason I used the word likely is that if a pragma pack directive was used in your source, the packing alignment could be changed, resulting in a different value for sizeof.

Two good discussions on struct alignment padding: A general discussion here, and How to reduce memory footprint here. The second link is particularly interesting as it deals with structure alignment, padding and bit fields, and how each can affect memory usage.

这篇关于包含两个指针的结构体的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆