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 ...
随机推荐
- 物联网全景动态图谱2.0|PaaS物联网平台汇总(上篇)
物联网智库 原创 物联网智库 整理发布 转载请注明来源和出处 ------ [导读] ------ 毫无疑问,2018年物联网对行业的深度变革才刚刚开启. 物联网产业链企业的质与量将进入全面爆 ...
- 利用cwRsync客户端将Windows下文件同步到Linux
这里不描述Linux服务端安装配置rsync服务的过程,有需要可以在网络上查找相关教程. 1.安装cwRsync客户端下载地址:http://itefix.no/cwrsync/下载文件cwRsync ...
- android studio: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>: java.lang.NoClassDefFoundError: Failed resolution o
今天在运行部署项目时logcat弹出下列错误: -- ::-/? E/Zygote: v2 -- ::-/? I/libpersona: KNOX_SDCARD checking this -- :: ...
- Error: Could not find gradle wrapper within Android SDK. Might need to update your Android SDK - Android
在Windows7上运行 “cordova build android” 报错,如下: C:\test\hello> cordova build android ANDROID_HOME=C:\ ...
- 通过动态SQL语句创建游标
DECLARE @sql varchar(100); DECLARE @TableName varchar(32); DECLARE @FieldName varchar(32); DECLARE @ ...
- JavaScript鼠标拖动div且可调整div大小
http://www.softwhy.com/article-5502-1.html <!DOCTYPE html> <html> <head> <meta ...
- Faster-RCNN 算法解读(转)
论文:<Faster R-CNN: Towards Real-Time ObjectDetection with Region Proposal Networks> 摘要:算法主要解决两个 ...
- plsql 通过修改配置文件的方式实现数据库的连接
查看oracle的安装位置: XP系统: 开始>>所有程序>>>Oracle-OraDb10g_home1>>>配置和移植工具>>>右 ...
- 新内容转入github
所有新内容已经转入 https://github.com/honggzb/Study-General https://github.com/honggzb/Study2016
- Zend Optimizer,Zend Guard Loader 和 Zend Opcache 三者之间的区别
PHP的加速插件有三个:Zend Optimizer.Zend Guard Loader 和 Zend Opcache.但其实都是一个,针对不通的php版本.名字叫法不一样而已. Zend Optim ...