为什么__last__元素需要c结构填充

时间:2021-02-15 22:31:37

I am aware of padding, and its rules, why it requires etc.

我知道填充,它的规则,为什么它需要等等。

My question is given struct,

我的问题是,

struct my_struct {
   int a;
   char c;
};

In this case start address of c is word align, but still compiler added 3 bytes (assuming 4 as word size) padding. with no element after c and why we need these 3 bytes? I checked following,

在这种情况下,c的起始地址是word align,但是编译器仍然添加了3个字节(假设4是word大小)填充。c后面没有元素,为什么需要这3个字节?我检查后,

int g_int1;
struct my_struct st;
int g_int2;

by above what I mean is my rest of variable declarations are not dependent on word align-ness of previous variable size. compiler always try to align next variable irrespective of its global or local auto var.

在上面我的意思是,我的剩余变量声明不依赖于之前变量大小的单词alignness。编译器总是试图对齐下一个变量,不管它的全局或局部自动变量。

I cant see any reason with endian-ness since this is char and for one byte it don't matters. what reason I think is instead of checking last element condition compiler always add padding whenever required.

我看不出有什么意外的原因,因为这是char,对于一个字节来说不重要。我认为的原因是不检查最后一个元素条件编译器总是在需要的时候添加填充。

what can the valid reason?

合理的理由是什么?

3 个解决方案

#1


9  

Because if sizeof(my_struct) was 5 rather than 8, then if you did this:

因为如果sizeof(my_struct)是5而不是8,那么如果你这样做:

my_struct array[2];

then array[0] would be word-aligned, but array[1] would not be. (Recall that array lookup is done by adding multiples of sizeof(array[0]) to the address of the first element.)

那么数组[0]将是字对齐的,而数组[1]则不是。(回想一下,数组查找是通过将sizeof(数组[0])添加到第一个元素的地址来完成的。)

#2


6  

Imagine you have an array of struct my_struct. How would its elements be word-aligned if they aren't a multiple of words in size each?

假设有一个struct my_struct数组。如果不是每个词都是多重的,那么它的元素是如何排列的呢?

#3


1  

It's to have the object size also aligned. Imagine having an array of my_structs. In this case you need to align the start adress of every element. Therefore sizeof(struct my_struct) must be "aligned". Otherwise you are not able to tell how much memory you need to allocate.

它的目标尺寸也要对齐。假设有一个my_structs数组。在这种情况下,您需要对齐每个元素的开始地址。因此,sizeof(struct my_struct)必须“对齐”。否则,您无法判断需要分配多少内存。

#1


9  

Because if sizeof(my_struct) was 5 rather than 8, then if you did this:

因为如果sizeof(my_struct)是5而不是8,那么如果你这样做:

my_struct array[2];

then array[0] would be word-aligned, but array[1] would not be. (Recall that array lookup is done by adding multiples of sizeof(array[0]) to the address of the first element.)

那么数组[0]将是字对齐的,而数组[1]则不是。(回想一下,数组查找是通过将sizeof(数组[0])添加到第一个元素的地址来完成的。)

#2


6  

Imagine you have an array of struct my_struct. How would its elements be word-aligned if they aren't a multiple of words in size each?

假设有一个struct my_struct数组。如果不是每个词都是多重的,那么它的元素是如何排列的呢?

#3


1  

It's to have the object size also aligned. Imagine having an array of my_structs. In this case you need to align the start adress of every element. Therefore sizeof(struct my_struct) must be "aligned". Otherwise you are not able to tell how much memory you need to allocate.

它的目标尺寸也要对齐。假设有一个my_structs数组。在这种情况下,您需要对齐每个元素的开始地址。因此,sizeof(struct my_struct)必须“对齐”。否则,您无法判断需要分配多少内存。