#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. 第十四章:样式(Style)和主题(Theme)

    简介 Android的样式(Style)和主题(Theme)文件就好比WEB开发中的CSS一样,可以实现UI界面的风格统一管理,这和Windows平台的XAML格式(Silverlight.WPF)类 ...

  2. jenkins2 pipeline实例

    比较完整的实例,使用了maven打包,git tag,发通知邮件,等待用户deploy,deploy到nexus. 文章来自:http://www.ciandcd.com文中的代码来自可以从githu ...

  3. centos网卡错误Device eth0 does not seem to be present

    在使用vmware及VirtualBox迁移linux系统过程中,发现部署后的linux系统无法启动网卡 报错为 Bringing up interface eth0: Device eth0 doe ...

  4. duilib进阶教程 -- 各种控件的响应 (10)

    到上一个教程为止,界面显示的代码就都介绍完啦,现在开始介绍控件的响应,其实在<2013 duilib入门简明教程 -- 事件处理和消息响应 (17)>里已经列出了duilib自己定义的所有 ...

  5. 电影成生活O2O必争之地,破局之战就此拉开

    这一次的两会过后,互联网最流行的一个词恐怕当属“互联网+”.尤其是总理关于“以互联网为载体.把线上线下互动的新兴消费搞得红红火火”的一席话,更是让国内的O2O从业者兴奋不已.百度李彦宏在两会接受记者采 ...

  6. Android模拟器配置

    // 检查连接设备,为了保证运行正常,只能留一个连接设备 adb devices

  7. ASP 中 Cookies 的 Expires 属性的设置(JS版本)

    直接上代码,代码中有注释 <%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%> <% var numVisi ...

  8. Moccakids-Tangram Puzzle 限免啦!

    圣诞前上线的App,Tangram Puzzle 限免,大家去下载玩玩. 介绍网站:http://www.mokamisu.com/ App Store:https://itunes.apple.co ...

  9. cocos2d-x.0创建工程

    $ python create-multi-platform-projects.py -p PompaDroid -k cn.philon.pompadroid -l cpp

  10. Objective C for Windows

    You can use Objective C inside the Windows environment. If you follow these steps, it should be work ...