c struct 对齐_C中的struct大小| 填充,结构对齐
c struct 對齊
What we know is that size of a struct is the sum of all the data members. Like for the following struct,
我們知道的是, 結構的大小是所有數據成員的總和 。 對于以下結構,
struct A{int a;int* b;char c;char *d; };Size of the struct should be sum of all the data member, which is: Size of int a+ size of int* b +size of char c+ size of char* d
結構的大小應為所有數據成員的總和,即:int a的大小+ int * b的大小+ char c的大小+ char * d的大小
Now considering the 64-bit system, Size of int is 4 Bytes Size of character is 1 Byte Size of any pointer type is 8 Bytes (Pointer size doesn't depend on what kind of data type they are pointing too)So the size of the struct should be: (4+8+1+8)=21 BytesLet's see what compiler is giving using the sizeof() operator.
讓我們看看使用sizeof()運算符給出的編譯器內容。
#include <stdio.h>struct A {int a;int* b;char c;char* d; };int main() {struct A a;printf("Size of struct A: %lu\n", sizeof(struct A));printf("Size of object a: %lu\n", sizeof(a));return 0; }Output:
輸出:
Size of struct A: 32 Size of object a: 32Oops!! The output is 32 Bytes. How is that possible?
糟糕! 輸出為32個字節。 那怎么可能?
It seems like the compiler took maximum size out of the data type and assigned the same memory to all data types. Is it so?
似乎編譯器從數據類型中取出了最大大小,并為所有數據類型分配了相同的內存。 是這樣嗎?
Okay, it's quite like that, but not the same. Of course, the compiler adds padding and tries to align the data members. So for the above structure, the data alignment looks like below,
好的,就像那樣,但是不一樣。 當然,編譯器會添加填充并嘗試對齊數據成員。 因此,對于上述結構,數據對齊如下所示,
Above is the alignment of the structure A, and that's why the size of the struct is 32 Bytes. Also, the object a of type struct A is 32 Bytes.
上面是結構A的對齊方式,這就是為什么該結構的大小為32 Bytes的原因 。 同樣,類型為struct A的對象a是32個字節。
編譯器如何添加填充? (How compiler adds padding?)
Now the question is how compiler adds padding and align? The method is compiler dependent and kind of greedy. It aligns till the boundary of maximum memory allocated. Here we find that max memory allocated is 8 Bytes, thus all the data members acquire 8 Bytes and the total size is 32 Bytes. Now the question is will it happen every time similarly?
現在的問題是編譯器如何添加填充和對齊? 該方法取決于編譯器并且有點貪婪。 對齊直到分配的最大內存邊界。 在這里,我們發現分配的最大內存為8字節,因此所有數據成員都獲取8字節,總大小為32字節。 現在的問題是,是否每次都會同樣發生?
Is it like the number of data members * max datatype size?
就像數據成員數*最大數據類型大小一樣嗎?
The answer is no. Check the following structure which has the same members but the ordering is different.
答案是不。 檢查以下具有相同成員但順序不同的結構。
struct B{int* b;char c;int a;char *d; }; #include <stdio.h>struct B {int* b;char c;int a;char* d; };int main() {struct B b;printf("Size of struct B: %lu\n", sizeof(struct B));printf("Size of object b: %lu\n", sizeof(b));return 0; }Output:
輸出:
Size of struct B: 24 Size of object b: 24In the above structure, we find that the size is 24 Bytes though the same data members have been used. This is due to the change in the order of the member declaration. In this case, the alignment and padding would be like below:
在上面的結構中,我們發現盡管使用了相同的數據成員,但大小為24字節。 這是由于成員聲明順序的更改。 在這種情況下,對齊方式和填充將如下所示:
Above is the alignment for structure B and that's why size is 24 Bytes, instead of 32. We saw that compiler keeps aligning greedily and that's why it aligned char c & int a in the same row. When it tried to align char* d, it could not as only 3 bytes were left. But instead of char*, if it was char only then it would have aligned in the same line.
上面是結構B的對齊方式,這就是為什么大小為24字節而不是32字節的原因。我們看到編譯器不斷貪婪地對齊,這就是為什么它在同一行中對齊char c和int a的原因。 當它嘗試對齊char * d時 ,它不能,因為只剩下3個字節。 但是,如果不是char * ,則如果只是char,則它將在同一行中對齊。
So, I hope it's clear how compiler aligns a structure. A point to be noted is that compiler can't reorder the data members though it may have reduced size. Thus, struct A will have size 32 Bytes, not 24 Bytes.
因此,我希望很清楚編譯器如何對齊結構。 需要注意的一點是,盡管編譯器的大小可能減小,但它不能對其重新排序。 因此, 結構A的大小為32字節,而不是24字節。
翻譯自: https://www.includehelp.com/c/size-of-struct-in-c-padding-alignment-in-struct.aspx
c struct 對齊
總結
以上是生活随笔為你收集整理的c struct 对齐_C中的struct大小| 填充,结构对齐的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: codejam题目_嵌套深度-Googl
- 下一篇: 面试官:int和Integer有什么区别