c语言typeof size_t offsetof container_of

一、typeof

作用: 获取变量类型 typeof(a)

struct test
{
	int a;
};

struct test st1;
typeof(st1) st2;
st2.a = 33;
printf("a = %d\n", st2.a);

二、size_t

size_t是一些C/C++标准在stddef.h中定义的。
size_t的全称应该是size type,就是说“一种用来记录大小的数据类型”。这个类型足以用来表示对象的大小。通常我们用sizeof(XXX)操作,这个操作所得到的结果就是size_t类型。

int i;							//定义一个int类型的变量i
size_t size=sizeof(i);			//用sizeof操作得到变量i的类型的大小

因为size_t类型的数据其实是保存了一个整数,所以它也可以做加减乘除,也可以转化为int并赋值给int类型的变量。
size_t的真实类型与操作系统有关。

在32位架构中被普遍定义为:
	typedef   unsigned int size_t;
而在64位架构中被定义为:
 typedef  unsigned long size_t;
 size_t在32位架构上是4字节,在64位架构上是8字节,在不同架构上进行编译时需要注意这个问题。而int在不同架构下都是4字节,与size_t不同;且int为带符号数,size_t为无符号数。

三、offset_of

作用: 获取结构体中成员的偏移位置
头文件: stddef.h

 #define offsetof(type, member)	(size_t)&(((type*)0)->member)
    
    struct test
    {
    	int a;
    	char b;
    	int c;
    };
        
   printf("offsetof(a) = %d\n", (int)offsetof(struct test, a));		//0
   printf("offsetof(b) = %d\n", (int)offsetof(struct test, b));		//4
   printf("offsetof(c) = %d\n", (int)offsetof(struct test, c));    	//8

三、container_of

作用: 根据结构体中成员member的地址ptr反推得到结构体type的地址
头文件:需要包含linux/kernel.h

#define container_of(ptr, type, member) ({\
	const typeof( ((type*)0)->member  ) *_mptr = (ptr);\
	(type*)( (char*)_mptr - offsetof(type, member)); })
container_of宏分为两部分:
第一部分: const typeof(  ((type*)0)->member  ) *_mptr = ptr
	通过typeof定义一个member类型指针变量_mptr,并赋值为ptr
第二部分: (type*)( (char *)_mptr - offsetof(type, member))
	通过offsetof得到member在type中的偏移量,然后用memeber的地址减去偏移量,得到type的起始地址,即指向tpye类型的指针

测试代码:

#ifndef container_of
#define container_of(ptr, type, member) ({\
	const typeof(((type*)NULL)->member)*_mptr = ptr;\
	(type*)((char *)_mptr - offsetof(type, member));})
#endif

struct test
{
	int a;
	char *pname;
};

struct test st1, *pst;	
pst = container_of(&(st1.pname), struct test, pname);
printf("address of st1 = 0x%x\n", (unsigned int)&st1);	//0xc5197118
printf("address of pst = 0x%x\n", (unsigned int)pst);	//0xc5197118

猜你喜欢

转载自blog.csdn.net/weixin_42639771/article/details/89072052