一、offsetof 概述
宏, 定义在头文件 #include <stddef.h>
offsetof :
Retrieves the offset of a member from the beginning of its parent structure.
size_t offsetof(structName, memberName);
Parameters:
structName : Name of the parent data structure.
memberName :Name of the member in the parent data structure for which to determine the offset.
Return Value :
offsetof returns the offset in bytes of the specified member from
the beginning of its parent data structure. It is undefined for bit fields.
Remarks :
The offsetof macro returns the offset in bytes of memberName from the beginning of the structure specified by structName. You can specify types with the struct keyword.
Note :
offsetof is not a function and cannot be described using a C prototype.
#define offsetof(s, m) (size_t)&(((s *)0)->m)
s是一个结构名,它有一个名为m的成员(s和m 是宏offsetof的形参,它实际是返回结构s的成员m的偏移地址.
(s *)0 是骗编译器说有一个指向类(或结构)s的指针,其地址值0
&((s *)0)->m 是要取得类s中成员变量m的地址. 因基址为0,这时m的地址当然就是m在s中的偏移
最后转换size_t 型,即unsigned int。
二、Demo-test
#include <stdio.h>
typedef struct _Node
{
char a;
int b;
}Node;
int main()
{
unsigned int res = 0;
res = offsetof(Node, b);
printf("res=%d\n", res);
}
结果 res = 4 由此可见 在内存中有了补齐 ,按照4给字节对齐
三、container_of
container_of 的定义在 linux 内核 include/linux/kernel.h 中
其中的offsetof 的定义在 include/linux/stddef.h
在使用过程中一般不会有什么 问题,如:
但是下边的编译的时候就会有warning
编译过程:
注意红色部分,这是怎么回事呢?下面我们就来分析一下
container_of的部分展开