简体   繁体   English

链表节点的大小

[英]Size of a linked list node

I find this thing a little tricky and recursive as to how sizeof operator calculates the size of one node in a linked list. 我发现这个事情有点棘手和递归,因为sizeof运算符如何计算链表中一个节点的大小。 I've the below structure as one node in list for example: 我将以下结构作为列表中的一个节点,例如:

struct ll_core
{
    char c_ll;
    struct ll_core * next;
};

printf("size of struct ll_core = %d\n\n",sizeof(struct ll_core));

it gives me an answer 8. Now how does it decides the size 8, because to add sizes of individual struct element it agains encounter the same struct ll_core. 它给出了答案8.现在它如何决定大小8,因为要添加单个struct元素的大小,它会再次遇到相同的struct ll_core。 So it's a kind of loop or recursion while calculating size. 因此,在计算大小时,它是一种循环或递归。 Please excuse me and let me know if I'm missing any basic thing in my head while thinking like this. 请原谅我告诉我,如果我在想这样的话,我会遗漏任何基本的东西。

It doesn't need the size of the struct again, since the struct only contains a pointer to the struct, not the struct itself. 它不再需要struct的大小,因为struct只包含指向 struct的指针 ,而不是struct本身。

The size of "pointer to struct ll_core " has nothing to do with size of struct ll_core , they are two distinct types. “指向struct ll_core指针”的大小与struct ll_core大小struct ll_core ,它们是两种不同的类型。 One is pointer, the other one isn't. 一个是指针,另一个不是。

You can't declare a "truly" recursive data structure, since it would be infinite. 你不能声明一个“真正的”递归数据结构,因为它是无限的。

because to add sizes of individual struct element it agains encounter the same struct ll_core . 因为要添加单个struct元素的大小,它会再次遇到相同的struct ll_core

No, it encounters a pointer to a struct ll_core . 不,它遇到了一个指向 struct ll_core指针 If it encountered the struct itself, that would lead to infinite sizes. 如果遇到结构本身,那将导致无限大小。 The compiler knows the size and alignment requirements of a struct ll_core * even without knowing anything about struct ll_core , so it can add the sizes of the members plus the padding required for alignment (of the next member in this case) to find the size of the struct ll_core . 编译器知道struct ll_core *的大小和对齐要求,即使不知道任何关于struct ll_core ,因此它可以添加成员的大小加上对齐所需的填充(在本例中为next成员)以查找大小struct ll_core

Assuming you are running your code on 32 bit machine, size of pointer will be 4 bytes. 假设您在32位机器上运行代码,指针大小将为4个字节。 As structure will be aligned on word boundary, 3 bytes will be padded and so the size will be 8 bytes. 由于结构将在字边界上对齐,因此将填充3个字节,因此大小将为8个字节。

this structure will actually be like, 这种结构实际上就像,

struct ll_core{
    char       c_11:
    char const byte[3];  //padded bytes for alignment reasons
    struct ll_core *next;
};
struct ll_core
{
    char c_ll;
    struct ll_core * next;
};

sizeof operator adds the size of the member of the structure ll_core . sizeof运算符添加结构ll_core成员的大小。 Here it is a character ( c_ll ) and a pointer( next ). 这是一个字符( c_ll )和一个指针( next )。

A little more information about how the size is calculated. 有关如何计算大小的更多信息。

Structure Padding: 结构填充:

In a 64 bit system, data will read and written as 8 byte chunks. 在64位系统中,数据将以8字节块的形式读写。 So when the size of the structure is calculated, padding happens. 因此,当计算结构的大小时,就会发生填充。 Means compiler will insert some gaps between the members of the structure to "align" to the architecture address boundaries. 意味着编译器将在结构的成员之间插入一些间隙以“对齐”到体系结构地址边界。 Something as follows: 如下:

struct ll_core
{
    char c_ll;
    /* 7 bytes of padding */
    struct ll_core * next;
 };

So the size of this structure in a 64-bit system will be 16 bytes. 因此,64位系统中此结构的大小将为16个字节。

Structure Packing: 结构包装:

You can prevent compiler from doing structure padding by doing structure packing. 您可以通过执行结构打包来阻止编译器执行结构填充。 In GCC, it is done like this: 在GCC中,它是这样完成的:

struct __attribute__((__packed__)) ll_core
{
    char c_ll;
    struct ll_core * next;
};

Now the size would be 9 bytes on a 64 bit machine. 现在64位机器上的大小将是9个字节。

sizeof(char) + sizeof(pointer)

Edit - From your question it seems you are running on a 32-bit machine. 编辑 - 从您的问题看来,您似乎在32位计算机上运行。 In a 32-bit machine, data will be read and written as 4 byte chunks. 在32位机器中,数据将作为4字节块读取和写入。

Many processors work better if certain types are aligned on certain addresses. 如果某些类型在某些地址上对齐,则许多处理器工作得更好 This means that the compiler pads your structure after the single char element so that the pointer is on a native word boundary. 这意味着编译器在单个char元素之后填充结构,以使指针位于本机字边界上。

As for the "recursion" in the structure, there is none because the next field is a pointer to the structure and not the structure itself. 至于结构中的“递归”,没有,因为next字段是指向结构的指针而不是结构本身。

you are missing the point, look carefully... 'next' is just a pointer. 你错过了这一点,仔细看......“下一个”只是一个指针。 you cannot do just 'struct ll_core next'..that is without *. 你不能只做'struct ll_core next'..那就是没有*。 So, as long as it is a pointer. 所以,只要它是一个指针。 The size will be calculated for the size of the pointer only. 将仅针对指针的大小计算大小。 In general, the pointer size is 4. 通常,指针大小为4。

If you have any confusion, remove the * from next and try compiling the code. 如果您有任何疑惑,请从下一步中删除*并尝试编译代码。

There 2 elements are a char (size 1) and a pointer (size 4), so you'd think the size would be 5 but structures are padded to 4 bytes (word size). 有两个元素是char(大小为1)和一个指针(大小为4),因此您认为大小为5,但结构填充为4个字节(字大小)。 The char is padded to 4 bytes giving a total of 8. char填充为4个字节,总共8个字节。

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

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