与struct相关的宏定义 ---今Tencent笔试用到的

获取成员变量的偏移以及通过成员变量的地址获取struct的起始地址


1. 获取成员变量的偏移。

                         #define offset_of(type, field) ( (unsigned int)&(((type *)(0))->field) ) 

简单解释一下这个宏:

如果我们定义一个struct:

typedef struct{

int a;

int b;

} A;

那么offset_of(A, b)就会输出b相对于起始地址的偏移。

& -- 这是取地址符号,表示获取当前变量的地址。

0 -- 这是这个宏中最核心的地方。我们知道,对于一个struct, 比如说上面的A,  &A->b代表了b的地址, 如果我们想得到b相对于A的偏移,那么&A->b - &A就可以得到。

       但如是&A就是0呢。那么&A->b不就是偏移了吗?

2、如果已知成员变量的地址,那我们也可以轻松得获取该成员所在结构体的首地址。

代码如下:

                    #define container_of(ptr, type, field) (type *)((char *)ptr - offset_of(type, field))  

我们只需要把该成员变量的地址减去你相对于首地址的偏移,不就是该结构体的首地址了么?

结出一个完整的实例:

  1. // testConsole.cpp : Defines the entry point for the console application.  
  2. //  
  3. #include "stdafx.h"  
  4. #include <time.h>  
  5. #include <stdio.h>  
  6. #include <stdlib.h>  
  7.   
  8. #define offset_of(type, field) ( (unsigned int)&(((type *)(0))->field) )  
  9. #define container_of(ptr, type, field) (type *)((char *)ptr - offset_of(type, field))  
  10.   
  11. typedef struct{  
  12.     int     a;  
  13.     int     b;  
  14.     char    c;  
  15.     int     d;  
  16. }package_t;  
  17.   
  18. int main()  
  19. {  
  20.     package_t pkg;  
  21.     int *p;  
  22.   
  23.     p = (int *)&pkg.d;  
  24.     printf("offset of 'd' is %d.\n", offset_of(package_t, d));  
  25.     printf("The addr of pkg = 0x%x.\n", &pkg);  
  26.     printf("The addr of d's container = 0x%x.\n", container_of(p, package_t, d));  
  27.     return 0;  
  28. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值