简体   繁体   English

C / C ++:结构如何包含指向自身的指针?

[英]C/C++: How can a structure contain a pointer pointing to itself?

The question is more of theory-based than practical implementation. 问题更多是基于理论而非实际实施。

What I would like to know is, in the code: 我想知道的是,在代码中:

struct temp {
    int num;
    struct temp *next;
};

In the structure made above, how does the compiler decide how much of space should be allocated in memory when we instantiate it? 在上述结构中,实例化实例时,编译器如何确定应在内存中分配多少空间?

If I try to dry-run the code, the size of an integer would be 2 or 4 bytes, after that, if I try to find the size of the pointer to the structure itself, I enter a structure containing another integer and a pointer to itself. 如果尝试空运行代码,则整数大小将为2或4个字节,此后,如果尝试查找指向结构本身的指针的大小,则输入包含另一个整数和指针的结构本身。

By this logic, the compiler would enter an infinite loop when deciding to allocate space to an element of this structure, how then is the space decided? 通过这种逻辑,编译器在决定为该结构的元素分配空间时将进入无限循环,那么如何确定空间?

PS I did try to find out the size of this structure myself (using the sizeof() operator, it turned out to be 16 bytes (the size of the integer being 4 bytes)... I'm interested in knowing how did it determine this figure of 16 bytes as the space required! PS我确实尝试自己找出此结构的大小(使用sizeof()运算符,结果是16个字节(整数的大小为4个字节)...我很想知道它是怎么做的确定此16字节的数字是否为所需的空间!

A structure containing a pointer to itself is not a problem. 包含指向自身的指针的结构不是问题。

A pointer has a fixed size, so it doesn't matter how big the size of the structure it points to is. 指针的大小是固定的,因此指向的结构大小没有关系。 On most systems you're likely to come across, a pointer will be either 4 bytes or 8 bytes in size. 在大多数系统上,您可能会碰到一个指针,大小为4个字节或8个字节。

For the struct you've given, the int field will probably use 4 bytes and the struct temp * field will probably use 8 bytes. 对于您给出的structint字段可能使用4个字节,而struct temp *字段可能使用8个字节。 This means the struct will be at least 12 bytes in size, probably 16 due to padding for proper alignment. 这意味着该结构的大小将至少为12个字节,由于填充的正确对齐,可能为16个字节。

If on the other hand you had something like this: 另一方面,如果您有以下内容:

struct temp {
    int num;
    struct temp next;
};

This would not be legal because the struct contains a copy of itself, which would in turn contain a copy of itself, and so on. 这是不合法的,因为该结构包含其自身的副本 ,而该结构又将包含其自身的副本,依此类推。

In the structure made above, how does the compiler decide how much of space should be allocated in memory when we instantiate it? 在上述结构中,实例化实例时,编译器如何确定应在内存中分配多少空间?

Pointer types take up a fixed size, which is not dependent on the pointed-to type (IOW, the size and representation of struct temp * is not in any way dependent on the size or representation of struct temp ). 指针类型占据固定大小,该大小不依赖于指向类型(IOW, struct temp *的大小和表示形式不依赖于struct temp的大小或表示形式)。 Furthermore, all pointer to struct types have the same size: 此外,所有指向struct类型的指针都具有相同的大小:

6.2.5 Types 6.2.5类型
... ...
28 A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. 28指向void的指针应具有与字符类型的指针相同的表示和对齐要求。 48) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. 48)同样,指向兼容类型的合格或不合格版本的指针应具有相同的表示和对齐要求。 All pointers to structure types shall have the same representation and alignment requirements as each other. 所有指向结构类型的指针应具有相同的表示和对齐要求。 All pointers to union types shall have the same representation and alignment requirements as each other. 指向联合类型的所有指针应具有相同的表示和对齐要求。 Pointers to other types need not have the same representation or alignment requirements. 指向其他类型的指针不必具有相同的表示或对齐要求。

C 2011 online draft C 2011在线草案

On platforms like x86, all pointer types have the same size and representation, but the standard has to take oddball architectures into account. 在x86之类的平台上, 所有指针类型都具有相同的大小和表示形式,但是该标准必须考虑奇数球体系结构。

It's no different than if you had a pointer to int or pointer to void as a member. 这与您有一个指向int的指针或一个指向void的成员没有什么不同。 The only thing that makes it look weird is that you're creating a self-referential pointer, which is allowed: 唯一使它看起来很奇怪的是,您正在创建一个自引用指针,该指针是允许的:

6.7.2.1 Structure and union specifiers 6.7.2.1结构和联合说明符
... ...
3 A structure or union shall not contain a member with incomplete or function type ( hence, a structure shall not contain an instance of itself, but may contain a pointer to an instance of itself ), except that the last member of a structure with more than one named member may have incomplete array type; 3结构或联合不得包含具有不完整或函数类型的成员( 因此,结构不应包含其自身的实例,但可以包含指向其自身的实例的指针 ),除非结构的最后一个成员具有更多的功能或类型。一个命名成员可能具有不完整的数组类型; such a structure (and any union containing, possibly recursively, a member that is such a structure) shall not be a member of a structure or an element of an array. 这样的结构(以及任何可能包含递归的此类结构的并集的联合)不应是结构的成员或数组的元素。

ibid. 同上

Even though the struct type itself is not complete until the closing } of the definition, any pointer to it is complete (that is, its size is known). 即使结构类型本身在定义的}之前不完整,但指向它的任何指针都是完整的(即,其大小已知)。

The size of the pointed-to type matters for pointer arithmetic (ie, the address value resulting from p + 1 will depend on the size of the thing p points to). 指向类型的大小对于指针算术很重要(即,从p + 1得到的地址值将取决于p指向的对象的大小)。 However, it doesn't matter for storing the pointer itself. 但是,存储指针本身并不重要。

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

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