#include <stddef.h>
#define offsetof ( TYPE, m)   (size_t )&reinterpret_cast< const volatile char&>(((( TYPE *)0)->m))
宏功能:获得一个结构体变量成员在此结构体中的偏移量。通过获取偏移量取得结构体的地址
 
 
/*  Takes a pointer to a member variable and computes pointer to the structure
    that contains it. 'type' is type of the structure, not the member. */
#define nn_cont (ptr, type, member ) \
    (ptr ? (( type*) (((char *) ptr) - offsetof(type , member))) : NULL)
 
这个宏是通过知道ptr的地址,这个地址也是member的地址,使用offsetof计算menber的偏移量,ptr-member的偏移量获得
type结构体变量的地址。
 
 
在nanomsg代码中,
  for ( it = nn_list_begin (&self. socktypes);
          it != nn_list_end (&self. socktypes);
          it = nn_list_next (&self. socktypes, it )) {
        socktype = nn_cont (it, struct nn_socktype , item);
        ......
        }
 
    socktypes 是个链表,保存着结构体 nn_socktype 的 item 成员,所以it指向的是成员item的地址,通过item的地址获得
    结构体nn_socktype 的变量 socktype 。 
 
 
 
 
 
 
 
 
 

offsetof的使用的更多相关文章

  1. linux中offsetof与container_of宏定义

    linux内核中offsetof与container_of的宏定义 #define offsetof(TYPE, MEMBER)    ((size_t) &((TYPE *)0)->M ...

  2. 理解#define offsetof(struct_t,member) ((int)&((struct_t *)0)->member)

    #define offsetof(struct_t,member) ((int)&((struct_t *)0)->member) 这个东西很多人应该知道: offsetof是用来判断结 ...

  3. 编程中的offsetof

    linux和windows平台都已经定义了offsetof函数,用于取struct类型中某个变量的偏移量 在stddef.h头文件中,该宏的完整说明如下: #ifdef __cplusplus #if ...

  4. (转)offsetof与container_of宏[总结]

    1.前言 今天在看代码时,遇到offsetof和container_of两个宏,觉得很有意思,功能很强大.offsetof是用来判断结构体中成员的偏移位置,container_of宏用来根据成员的地址 ...

  5. C语言typeof详解 offsetof

    http://blog.chinaunix.net/uid-28458801-id-4200573.html 前言:    typeof关键字是C语言中的一个新扩展,这个特性在linux内核中应用非常 ...

  6. C++ offsetof

    这是一个宏,用于计算类中某个成员的地址相对于类实例的偏移量 在C++11中,要求这个类standard_layout 基本用法是这样子的: #include <stdio.h> /* pr ...

  7. typeof、offsetof、container_of的解释

    链表是内核最经典的数据结构之一,说到链表就不得不提及内核最经典(没有之一)的宏container_of. container_of似乎就是为链表而生的,它的主要作用是根据一个结构体变量中的一个域成员变 ...

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

    #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)宏的运行机理:1. ( (TYPE *)0 ) 将零转型为TY ...

  9. C语言笔记(结构体与offsetof、container_of之前的关系)

    关于结构体学习,需要了解:结构体的定义和使用.内存对齐.结构体指针.得到结构体元素的偏移量(offsetof宏实现) 一.复习结构体的基本定义和使用 typedef struct mystruct { ...

随机推荐

  1. iOS UITableView中点击状态栏无法回滚到顶部

    // When the user taps the status bar, the scroll view beneath the touch which is closest to the stat ...

  2. js手风琴图片切换实现原理及函数分析

    关键词: js手风琴 js百叶窗 js百页窗 实现原理解读 使用两层for循环实现, 第一层有三个功能,分别给第个li: 添加索引 预设位置 添加事件 第二层有两个功能,整理图片位置: 鼠标的li,以 ...

  3. 使用socket.io开发简单群聊功能

    1.新建package.json文件: { "name": "socket-chat-example", "version": " ...

  4. ps、grep和kill联合使用杀掉进程

    例如要杀掉hello这个进程,使用下面这个命令就能直接实现.   ps -ef |grep hello |awk '{print $2}'|xargs kill -9 这里是输出ps -ef |gre ...

  5. 前端与Server端(路由)

    路由(前端):原理:只有几个XHR请求,就能实现页面无刷新闪烁更改URL 前端:1. ajax承担数据通信以及相对应的逻辑(交互逻辑) ajax进行数据交互,跟URL打交道,让数据交互的变化反映到UR ...

  6. Could not find a storyboard named 'Main' in bundle NSBundle

    转自:http://www.cnblogs.com/ygm900/p/3836580.html 1.删掉工程中main.storyboard 后要删除plist文件中对应的键值,否则会报如下错误: C ...

  7. 几种你不知道的获取浙A牌照的方法

    http://www.19lou.com/forum-464848-thread-18191429174490953-1-1.html 杭州限牌政策执行已有一年多,因为限牌政策,很多人买车之前,都不得 ...

  8. asp.net“服务器应用程序不可用” 解决方法

    服务器应用程序不可用 您试图在此 Web 服务器上访问的 Web 应用程序当前不可用.请点击 Web 浏览器中的“刷新”按钮重试您的请求. 管理员注意事项: 详述此特定请求失败原因的错误消息可在 We ...

  9. Zabbix监控Windows事件日志

    1.zabbix_agentd.win文件修改: LogFile=c:\zabbix\zabbix_agentd.log Server=1.16.2.4 ServerActive=1.16.2.4 H ...

  10. WHY数学图形可视化工具(开源)

    WHY数学图形可视化工具 软件下载地址:http://files.cnblogs.com/WhyEngine/WhyMathGraph.zip 源码下载地址: http://pan.baidu.com ...