转自:http://blog.csdn.net/tigerjibo/article/details/8299584

1.offset宏讲解

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE*)0)->MEMBER)

对这个宏的讲解我们大致可以分为以下4步进行讲解:

1>( (TYPE *)0 )  0地址强制 "转换" 为 TYPE结构类型的指针;

2>((TYPE *)0)->MEMBER   访问TYPE结构中的MEMBER数据成员;

3>&( ( (TYPE *)0 )->MEMBER)取出TYPE结构中的数据成员MEMBER的地址;

4>(size_t)(&(((TYPE*)0)->MEMBER))结果转换为size_t类型。

宏offsetof的巧妙之处在于将0地址强制转换为 TYPE结构类型的指针,TYPE结构以内存空间首地址0作为起始地址,则成员地址自然为偏移地址。可能有的读者会想是不是非要用0呢?当然不是,我们仅仅是为了计算的简便。也可以使用是他的值,只是算出来的结果还要再减去该数值才是偏移地址。来看看如下的代码:

#include<stdio.h>

#defineoffsetof(TYPE, MEMBER) ((size_t) &((TYPE *)4)->MEMBER)

struct test_struct {

int num;

char ch;

float f1;

};

int main(void)

{

printf("offsetof (struct test_struct,num)=%d\n",offsetof(struct test_struct,num)-4);

printf("offsetof (structtest_struct,ch) =%d\n",offsetof(struct test_struct,ch)-4);

printf("offsetof (struct test_struct,f1)=%d\n",offsetof(struct test_struct,f1)-4);

return 0;

}

运行结果为:

jibo@jibo-VirtualBox:~/cv_work/work/list/offset $ ./main

offsetof (struct test_struct,num) =0

offsetof (struct test_struct,ch) =4

offsetof (struct test_struct,f1) =8

为了让大家加深印象,我们在代码中没有使用0,而是使用的4,所以在最终计算出的结果部分减去了一个4才是偏移地址,当然实际使用中我们都是用的是0。

二.举例体会offsetof宏的使用:

#include<stdio.h>

#defineoffsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

struct test_struct {

int num;

char ch;

float f1;

};

int main(void)

{

printf("offsetof(struct test_struct,num) =%d\n",offsetof(struct test_struct,num));

printf("offsetof (structtest_struct,ch) =%d\n",offsetof(struct test_struct,ch));

printf("offsetof (struct test_struct,f1)=%d\n",offsetof(struct test_struct,f1));

return 0;

}

执行结果为:

jibo@jibo-VirtualBox:~/cv_work/work/list/offset $ ./main

offsetof (struct test_struct,num) =0

offsetof (struct test_struct,ch) =4

offsetof (struct test_struct,f1) =8

offset宏的讲解【转】的更多相关文章

  1. 转:container_of分析 研究内核的博客

    源地址:http://blog.csdn.net/tigerjibo/article/details/8299589 2012-12-15 19:23 1636人阅读 评论(2) 收藏 举报   目录 ...

  2. linux内核宏container_of

    首先来个简单版本 /* given a pointer @ptr to the field @member embedded into type (usually * struct) @type, r ...

  3. 嵌入式C语言自我修养 04:Linux 内核第一宏:container_of

    4.1 typeof 关键字 ANSI C 定义了 sizeof 关键字,用来获取一个变量或数据类型在内存中所占的存储字节数.GNU C 扩展了一个关键字 typeof,用来获取一个变量或表达式的类型 ...

  4. offsetof宏---个人笔记

    标准库里面提供的offsetof(t,m)宏,用来计算两个变量在内存中的地址偏移量 #include <stdio.h>//原型: #define offsetof(TYPE, MEMBE ...

  5. c语言对齐问题

    引言 考虑下面的结构体定义: typedef struct{ char c1; short s; char c2; int i; }T_FOO; 假设这个结构体的成员在内存中是紧凑排列的,且c1的起始 ...

  6. linux内核的双链表list_head、散列表hlist_head

    一.双链表list_head 1.基本概念 linux内核提供的标准链表可用于将任何类型的数据结构彼此链接起来. 不是数据内嵌到链表中,而是把链表内嵌到数据对象中. 即:加入链表的数据结构必须包含一个 ...

  7. C语言字节对齐问题详解

    引言 考虑下面的结构体定义: typedef struct{ char c1; short s; char c2; int i; }T_FOO; 假设这个结构体的成员在内存中是紧凑排列的,且c1的起始 ...

  8. C语言字节对齐问题详解(对齐、字节序、网络序等)

    首先说明一下,本文是转载自: http://www.cnblogs.com/clover-toeic/p/3853132.html 博客园用的少,不知道怎么发布转载文章,只能暂时这样了. 引言 考虑下 ...

  9. C语言字节对齐问题详解【转】

    引言 转自:http://www.cnblogs.com/clover-toeic/p/3853132.html 考虑下面的结构体定义: 1 typedef struct{ 2 char c1; 3 ...

随机推荐

  1. bootstrap 中的静态模式的控制按钮上的一个坑

    在使用modal时发现,代码:<button class="btn btn-danger" data-toggle="modal" data-target ...

  2. BZOJ 2152 聪聪可可(树形DP)

    给出一颗n个点带边权的树(n<=20000),求随机选择两个点,使得它们之间的路径边权是3的倍数的概率是多少. 首先总的对数是n*n,那么只需要统计路径边权是3的倍数的点对数量就行了. 考虑将无 ...

  3. 【比赛】HNOI2018 毒瘤

    虚树+dp 直接看zlttttt的强大题解 zlttttt的题解看这里 #include<bits/stdc++.h> #define ui unsigned int #define ll ...

  4. 【WPF】GridControl的使用

    一.前言 在WPF中我们常常使用DataGrid来显示数据,在Dev中也有一个类似的控件—GridControl. 二.范例 <dxg:GridControl Grid.Row=" I ...

  5. 【BZOJ2306】幸福路径(动态规划,倍增)

    [BZOJ2306]幸福路径(动态规划,倍增) 题面 BZOJ 题解 不要求确切的值,只需要逼近 显然可以通过移动\(\infty\)步来达到逼近的效果 考虑每次的一步怎么移动 设\(f[i][j]\ ...

  6. mysql允许远程特定ip访问

    1.登录 mysql -u root -p 之后输入密码进行登陆 2.权限设置及说明 2.1添加远程ip访问权限 GRANT ALL PRIVILEGES ON *.* TO 'root'@'10.1 ...

  7. Hadoop1重新格式化HDFS

    注意:原来的数据全部被清空了.产生了一个新的hdfs 1. 删除已有目录 1.1 查看hdfs-site.xml 将 dfs.name.dir和dfs.data.dir所指定的目录删除 vim hdf ...

  8. spark(三)spark sql

    一.DataFrame 1.DataFrame是组织成命名列的数据的分布式集合,类似于关系型数据库的一张表,如果没有列名就等于RDD,如果有列名,就是DataFrames DataFrames可以从各 ...

  9. Linux应用编程之串口操作20170901

    主要介绍在Linux应用程序下对串口的操作: 1.串口初始化 int InitCom() { int Ret; Ret = SerailComm.OpenCom( ComPortDevPath, 0 ...

  10. Codeforces Round #384 (Div. 2) A B C D dfs序+求两个不相交区间 最大权值和

    A. Vladik and flights time limit per test 2 seconds memory limit per test 256 megabytes input standa ...