您的位置:首页 > 其它

与struct相关的宏定义 --- 根据成员变量偏移,获取struct起始地址

2016-06-28 23:11 316 查看
这里主要介绍在struct中如何获取成员变量的偏移以及通过成员变量的地址获取struct的起始地址。

获取成员变量的偏移,直接上代码:

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

要理解这个宏,首先要理解宏定义到底是怎么一回事,还有struct是怎么一回事,还有指针的用法。

简单解释一下这个宏:

如果我们定义一个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不就是偏移了吗?

好了,当我们有了获取成员变量相对偏移的宏。如果已知成员变量的地址,那我们也可以轻松得获取该成员所在结构体的首地址。

代码如下:

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

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

好了,不多说,结出一个完整的实例:

    // testConsole.cpp : Defines the entry point for the console application.  

    //  

    #include "stdafx.h"  

    #include <time.h>  

    #include <stdio.h>  

    #include <stdlib.h>  

      

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

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

      

    typedef struct{  

        int     a;  

        int     b;  

        char    c;  

        int     d;  

    }package_t;  

      

    int main()  

    {  

        package_t pkg;  

        int *p;  

      

        p = (int *)&pkg.d;  

        printf("offset of 'd' is %d.\n", offset_of(package_t, d));  

        printf("The addr of pkg = 0x%x.\n", &pkg);  

        printf("The addr of d's container = 0x%x.\n", container_of(p, package_t, d));  

        return 0;  
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息