#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. Nim教程【二】

    第一篇教程1秒内就被管理员从首页踢掉了 管理员嫌内容太少,没有含金量,这次多写一些. 这应该是国内第一个关于Nim入门的系列教程 好,闲话休提,言归正传 Nim介绍 Nim代码会编译成C语言的代码,再 ...

  2. HTTP权威指南阅读笔记一:HTTP概述

    HTTP协议版本: 1.HTTP/0.9:HTTP的1991原型版本称为HTTP/0.9.这个协议有很多严重的缺陷,只应该用与与老客户端的交互.HTTP/0.9只支持GET方法,不支持多媒体内容的MI ...

  3. [游戏模版13] Win32 透明贴图 主角移动

    >_<:just add previous two ways to achieve this new result // stdafx.h : include file for stand ...

  4. Why we need template on Django ?

    Let's create a simple website by django ... step01: django-admin startproject x01 step02: cd x01 ls ...

  5. 为什么说外卖O2O行业的未来在于尖端技术?

    7月13日,百度公司董事长兼CEO李彦宏在发布会上谈及百度外卖时表示,百度外卖里有非常多的人工智能技术的应用,比如同样的商家订单,先配送后配送,时间路线规划等等,都有人工智能的技术,涉及机器学习的问题 ...

  6. iOS开发-Alpha,Hidden与Opaque区别

    UIView中的这三个属性用的比较多,尤其是Alpha和Opaque之间有的时候不是很好分别,稍微整理下: Alpha(不透明度) alpha是不透明度,属性为浮点类型的值,取值范围从0到1.0,表示 ...

  7. Mybatis多参传递的四种解决方案

    Mybatis多参传递的四种解决方案 代码异常:org.apache.ibatis.binding.BindingException: Parameter 'param' not found. 长时间 ...

  8. cmdCreateViewTag

    start ).Y < grid.Curve.get_EndPoint().Y)                 {                     grid = gridTmp;    ...

  9. Delphi 2 Unleashed (一) 介绍

    原书作者是作者是 Charles Calvert,国内翻译为<Delphi 2 程序设计大全>,由横空翻译组翻译,机械工业出版社1997年12月出版,看网上评论和介绍,该书是系统学习 De ...

  10. spring scope="prototype", scope="session"

    转自: http://www.cnblogs.com/JemBai/archive/2010/11/10/1873954.html struts+spring action应配置为scope=&quo ...