链表的节点应存储谁的地址:其他节点,或将一个节点作为字段的数据结构? [英] Whose address shall the node of a linked list store: other node, or data structure having a node as a field?

查看:151
本文介绍了链表的节点应存储谁的地址:其他节点,或将一个节点作为字段的数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

了解Linux内核:

Linux内核定义了list_head数据结构,仅 next和prev字段表示a的前向指针和后向指针 通用双链表元素.重要的是要 但是请注意,list_head字段中的指针存储了 其他list_head字段的地址,而不是 整个数据结构,其中list_head结构是 包含;参见图3-3(a).

the Linux kernel defines the list_head data structure, whose only fields next and prev represent the forward and back pointers of a generic doubly linked list element, respectively. It is important to note, however, that the pointers in a list_head field store the addresses of other list_head fields rather than the addresses of the whole data structures in which the list_head structure is included; see Figure 3-3 (a).

为什么list_head字段中的指针存储其他list_head字段的地址,而不是其中包括list_head结构的整个数据结构的地址?

Why do the pointers in a list_head field store the addresses of other list_head fields rather than the addresses of the whole data structures in which the list_head structure is included?

给出一个指向list_head对象的指针,如何获取包含list_head对象的数据结构(例如数据结构1")的对象?例如,我该如何从Linux内核中的PID获取进程描述符?

Given a pointer to a list_head object, how can I get the object of a data structure (such as "data structure 1") which contains the list_head object? For example, How can I get the process descriptor from a PID in Linux kernel?

如果使用OO语言,则list_head字段在包含list_head结构的数据结构中是否可能是私有的?那么,与其他list_head字段的地址相比,list_head字段中的指针存储包含list_head结构的整个数据结构的地址是否更合理?

If it were in a OO language, is it that the list_head fields would likely be private in the data structures in which the list_head structure is included? So is it more reasonable that the pointers in a list_head field store the addresses of the whole data structures in which the list_head structure is included than the addresses of other list_head fields?

推荐答案

为什么list_head字段中的指针存储其他地址 list_head字段而不是整个数据的地址 包含list_head结构的结构?

Why do the pointers in a list_head field store the addresses of other list_head fields rather than the addresses of the whole data structures in which the list_head structure is included?

因为从逻辑上讲它是正确的,并且易于实现.您不需要知道用于迭代列表的类型.同样,列表头未嵌入任何结构中.列表为空时应该指向什么?

Because it is logically right and easier to implement. You don't need to know the type to iterate the list. Also list head is not embedded into any structure. What should it point to when list is empty?

给出一个指向list_head对象的指针,我该如何获取 数据结构(例如数据结构1")包含 list_head对象?

Given a pointer to a list_head object, how can I get the object of a data structure (such as "data structure 1") which contains the list_head object?

使用 container_of 宏.

这篇关于链表的节点应存储谁的地址:其他节点,或将一个节点作为字段的数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆