container_of 例子说明
很早前之前看的linux内核,一直想把container_of记录一下,趁今天想起就记录一下:
内核中的描述
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ \
const typeof( ((type *))->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
作用:返回member成员对应的所在的父结构体指针。下面的例子就通过child变量的一个成员地址获取到child变量的地址,以便访问child变量的其他成员
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) #define container_of(ptr, type, member) ({ \
const typeof( ((type *))->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );}) struct parentStruct{
int a;
int b;
}; struct childStruct{
struct parentStruct parent;
int a;
int b;
}; void main(void){ struct childStruct * child =(struct childStruct*) malloc(sizeof(struct childStruct));
child->a =;
child->b =;
child->parent.a =;
child->parent.b =; struct parentStruct *parent = &child->parent; struct childStruct * child1 = container_of(parent,struct childStruct,parent); printf("%d,%d\n",child->a,child1->a);
printf("%d,%d\n",child->b,child1->b);
printf("%d,%d\n",child->parent.a,child1->parent.a);
printf("%d,%d\n",child->parent.b,child1->parent.b);
printf("%#x,%#x\n",child,child1); free(child); }
container_of 例子说明的更多相关文章
- linux中offsetof与container_of宏定义
linux内核中offsetof与container_of的宏定义 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->M ...
- 刨一刨内核container_of()的设计精髓
新年第一帖,总得拿出点干货才行,虽然这篇水分还是有点大,大家可以晒干了温水冲服.这段时间一直在整理内核学习的基础知识点,期间又碰到了container_of()这个宏,当然还包括一个叫做offseto ...
- (十)Linux内核中的常用宏container_of
Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址. Containe ...
- Linux内核中的常用宏container_of
Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址. Containe ...
- 内核中container_of宏的详细分析【转】
转自:http://blog.chinaunix.net/uid-30254565-id-5637597.html 内核中container_of宏的详细分析 16年2月28日09:00:37 内核中 ...
- linux内核中宏container_of是干什么的?
Linux Kernel Version 4.14 1. container_of是干什么的? 已知一个结构体中某个成员的首指针,那么就可以通过宏container_of来获得此结构体的首指针 2 先 ...
- linux设备驱动归纳总结(三):3面向对象思想和lseek、container_of、write、read 【转】
linux设备驱动归纳总结(三):3.设备驱动面向对象思想和lseek的实现 转自:http://blog.chinaunix.net/uid-25014876-id-59418.html 一.结构体 ...
- 嵌入式C语言自我修养 04:Linux 内核第一宏:container_of
4.1 typeof 关键字 ANSI C 定义了 sizeof 关键字,用来获取一个变量或数据类型在内存中所占的存储字节数.GNU C 扩展了一个关键字 typeof,用来获取一个变量或表达式的类型 ...
- Linux内核中的常用宏container_of其实很简单【转】
转自:http://blog.csdn.net/npy_lp/article/details/7010752 开发平台:Ubuntu11.04 编 译器:gcc version 4.5.2 (Ubun ...
随机推荐
- apache中如何调用CGI脚本
参考: http://www.jdon.com/idea/cgi.htm http://www.jb51.net/article/49069.htm 在Apache在配置对CGI的支持 LoadMod ...
- 用.NET开发的磁力搜索引擎——btbook.net
UPDATE:目前项目已停止维护,本文仅留作纪念. 去年10月份开始研究相关的协议与资料,中途乱七八糟的事情差点没坚持下来,寒假里修修补补上礼拜把Btbook发布了,经过社交网络的推广之后,上线第三天 ...
- Jquery Post提交时Content-Type的不同取值详解
四种常见的 POST 提交数据方式 我们知道,HTTP 协议是以 ASCII 码传输,建立在 TCP/IP 协议之上的应用层规范.规范把 HTTP 请求分为三个部分:状态行.请求头.消息主体.类似于下 ...
- Java多线程:多线程基础知识
一.线程安全性 定义:多个线程之间的操作无论采用何种执行时序或交替方式,都要保证不变性条件不被破坏 “共享”:变量可以由多个线程同时访问: “可变”:变量的值在其生命周期内可以发生改变 ...
- 怎么让self.view的Y从navigationBar下面开始计算
原文地址 http://blog.sina.com.cn/s/blog_1410870560102wu9a.html 在iOS 7中,苹果引入了一个新的属性,叫做[UIViewController s ...
- Windows server 2008普通用户不能远程登录问题
1.查登录权限 如果文件服务器没有为用户授权,那么用户自然就不能远程登录服务器系统了,为此笔者决定先仔细检查一下文件服务器系统是否为自己使用的登录账号,授予了远程登录权限.在进行这种检查时,笔者先是在 ...
- C++中常用的std标准容器
从c++11标准以来,c++中std定义的几种容器的效率非常高,优化的非常好,完全没有必要自己去定义类似的数据结构.了解使用它们,可以满足90%的日常编程需要.该篇文章基于c++11标准,从用户角度来 ...
- 通用Mapper新特性:ExampleBuilder 2017年12月18日
package tk.mybatis.mapper.test.example; import org.apache.ibatis.session.SqlSession; import org.juni ...
- Logstash安装和使用
Logstash 是开源的服务器端数据处理管道,能够同时 从多个来源采集数据.转换数据,然后将数据发送到您最喜欢的 “存储库” 中.(我们的存储库当然是 Elasticsearch.) 作用:集中.转 ...
- windows下设置PHP环境变量
1.找到“高级系统设置”(二选一的方法找到环境变量) ① 我的电脑-属性-高级-环境变量 ②win8,10 直接在搜索框搜 “查看高级系统设置”-环境变量 2.找到变量"Path" ...