链接列表,结构的创建
当未完全定义结构时,我们如何能够在结构节点内创建类型节点的指针。
struct node
{
int data;
node* next;
}
How are we able to create a pointer of type node inside the struct node when the struct is not fully defined.
struct node
{
int data;
node* next;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要了解这一点,您需要知道计算机不了解诸如结构之类的高级软件概念化。
实际上,当将代码编译为组装时, 都不会产生诸如结构之类的东西。取而代之的是,从C编译器的解析/语义分析中,生成了一个明确的结构化代码,您的CPU将通过通过路由表中的不同晶体管中的位置来实现其目的。
汇编代码:简而言之,在主函数的堆栈上发生了一个具有4位权重的静态内存位置的价值分配。
这些路由表具有2个规则分类类别,可以通过这些类别是: RAM/CPU缓存的静态/动态位置的内存地址和存储在该内存地址中的值(位)。
由此,丹尼斯·里奇(Dennis Ritchie)提出了子例程作为抽象层的概念,以使我们 管理汇编指针中记忆的所有复杂性。
现在,像递归一样复杂的概念在计算中不可能实现,因为该定义告诉我们,即使未完全定义了递归函数,也必须自称为自身。
面对这个问题,C处理指针的递归,因为指针是具有内存地址的实体,与要应用递归的类型或功能的地址完全不同。因此,对于像链接列表这样的递归数据结构,只能通过指针具有指向同一类型的另一个内存位置的链接。
简而言之,这类型的数据结构可以通过指针进行递归,因为指针本身与包含类型完全不同。。
To understand this, you need to know that the computer doesn't understand high-level software conceptualizations like structs.
In fact, when compiling your code to assembly, no such thing as a structure is ever generated. Instead, from the parsing/semantic analysis of the C compiler, a clearly structured code is generated, where your CPU will fulfill its purpose through the addressing of bits in the different transistors through routing tables.
Assembly Code: Simply, a value assignment to a static memory location with a 4-bit weight occurs on the stack of the main function.
These routing tables have 2 rule classification categories to have a data transmission format through the buses, these categories are: The memory address of a static/dynamic location of the RAM/CPU Cache and the value stored in that memory address (bits).
From this, Dennis Ritchie proposed the concept of subroutines as abstraction layers to spare us all the complexity involved in managing memory from assembler pointers.
Now, concepts as complex as recursion, in the first instance would be impossible to implement in computation because the definition tells us that a recursive function must call itself even if it is not completely defined.
Faced with this problem, C handles recursion from pointers, since pointers are entities with a memory address totally different from the address of the type or function to which you want to apply recursion. For this reason, for a recursive data structure like a linked list, it is only possible to have a link to another memory location of the same type through pointers.
In short, recursion in this type of data structure is possible through pointers because the pointer itself is a completely different memory address than the containing type.
远期声明
forward declarations
简短的答案是因为标准是这样说的。
较长的答案是,指针的大小(无论如何是指向数据的指针)总是相同的,因此编译器知道它是什么,即使
node
尚未完全定义。因此,它可以确定node
的布局,而又不知道接下来会发生什么,这足以使其快乐。将您的代码段与此对比:
现在编译器陷入困境,因为每个
next
将包含另一个node
,它将包含另一个next
等等,ad-infinitum。因此,该代码不会编译。但是有了指针,很好。根据 @goswinvonbrederlow的评论,更正式地
struct node
将node
作为不完整的类型,即使您立即通过{...};
以及在node> node
的声明中,节点
被视为声明但不完整。从那以后,潜入标准那:
这就是使您的示例起作用的原因。正如我所说,这是语言的设计方式。 C大致相同。
The short answer is because the standard says so.
The longer answer is that the size of a pointer (a pointer to data, anyway) is always the same and so the compiler knows what it is even though
node
is not yet fully defined. It is therefore able to determine the layout ofnode
without knowing what is coming next, and that's enough to keep it happy.Contrast your code snippet with this:
Now the compiler is in trouble, because each
next
will contain anothernode
which will contain anothernext
and so on, ad-infinitum. This code, therefore, will not compile. But with a pointer, it's fine.As per @GoswinvonBrederlow's comments, more formally
struct node
introducesnode
as an incomplete type, even if you immediately follow it by{ ... };
, and within the declaration ofnode
,node
is considered declared but incomplete.Following on from that, diving into the standard tells us that:
which is what makes your example work. As I say, it's how the language is designed. C is much the same.
当您要求编译器分配节点*接下来时,您实际上要求分配指针的大小的内存,这是所有指针类型的固定大小。
因此,当您声明它时,编译器不需要知道结构节点的大小。
When you ask the compiler to allocate node* next you actually ask to allocate memory of size of a pointer, which is fixed size for all pointers types.
So the compiler don't need to know the size of struct node when you declare it.