与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. }  

posted @ 2013-09-04 01:51  Kiveen  阅读(222)  评论(0编辑  收藏  举报