简体   繁体   English

如何计算C结构中的填充

[英]How to calculate padding in C struct

While I understand how padding generally works in a C struct, I struggle to understand how inner struct are padded.虽然我了解填充通常如何在 C 结构中工作,但我很难理解内部结构是如何填充的。 Example:例子:

struct inner
{
    int a;
}

struct outer
{
   char a;
   struct inner b;
}

How is the padding between member outer.a and member outer.b calculated? member outer.a和 member outer.b之间的 padding 是如何计算的?

How is the padding of the end of struct outer calculate when the data type of member inner.a changes from int to short or double ?当成员inner.a的数据类型从int变为shortdouble时,struct outer末尾的填充如何计算?

What are the implications of calculating these paddings in a 32 bit architecture versus a 64 bit one?在 32 位架构和 64 位架构中计算这些填充有什么含义?

I always find it better to hex-dump such things and play around in order to understand better.为了更好地理解,我总是发现最好对这些东西进行十六进制转储并四处玩耍。 Here is an example for your case:这是您的案例的示例:

example例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM