相关疑难解决方法(0)

linux/list.h中container_of宏背后的原理

在linux内核列表的实现中/include/linux/list.h,container_of宏的第一行(下面粘贴)背后的基本原理是什么?

const typeof( ((type *)0)->member ) *__mptr = (ptr);
Run Code Online (Sandbox Code Playgroud)

在我的示例代码中,我删除了这一行并将定义更改为

#define container_of(ptr, type, member) ({                      \
     (type *)( (char *)ptr - offsetof(type,member) );})
Run Code Online (Sandbox Code Playgroud)

我的代码仍显示预期的结果.那么第一行是多余的吗?或者它有一些我不知道的隐藏陷阱?

我在Faq/LinkedLists找到的代码

/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:        the pointer to the member.
 * @type:       the type of the container struct this is embedded in.
 * @member:     the name of the member within the struct.
 *
 */
#define container_of(ptr, type, …
Run Code Online (Sandbox Code Playgroud)

c struct pointers linked-list linux-kernel

31
推荐指数
1
解决办法
4724
查看次数

标签 统计

c ×1

linked-list ×1

linux-kernel ×1

pointers ×1

struct ×1