zoukankan      html  css  js  c++  java
  • 与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. ", offset_of(package_t, d));  
    25.     printf("The addr of pkg = 0x%x. ", &pkg);  
    26.     printf("The addr of d's container = 0x%x. ", container_of(p, package_t, d));  
    27.     return 0;  
    28. }  

  • 相关阅读:
    【转】Android——设置颜色的三种方法
    Eclipse Android安装APP时覆盖安装问题
    自定义数组,实现输出改数组的长度、最大值和最小值
    用程序实现对数组a[45,96,78,6,18,66,50]中的元素进行排序
    PHP面试题2
    PHP面试题
    gulp
    移动端base.css
    笔记
    mouseover和mouseout事件在鼠标经过子元素时也会触发
  • 原文地址:https://www.cnblogs.com/zcwsmile/p/4027840.html
Copyright © 2011-2022 走看看