很早前之前看的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 例子说明的更多相关文章

  1. linux中offsetof与container_of宏定义

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

  2. 刨一刨内核container_of()的设计精髓

    新年第一帖,总得拿出点干货才行,虽然这篇水分还是有点大,大家可以晒干了温水冲服.这段时间一直在整理内核学习的基础知识点,期间又碰到了container_of()这个宏,当然还包括一个叫做offseto ...

  3. (十)Linux内核中的常用宏container_of

    Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址. Containe ...

  4. Linux内核中的常用宏container_of

    Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址. Containe ...

  5. 内核中container_of宏的详细分析【转】

    转自:http://blog.chinaunix.net/uid-30254565-id-5637597.html 内核中container_of宏的详细分析 16年2月28日09:00:37 内核中 ...

  6. linux内核中宏container_of是干什么的?

    Linux Kernel Version 4.14 1. container_of是干什么的? 已知一个结构体中某个成员的首指针,那么就可以通过宏container_of来获得此结构体的首指针 2 先 ...

  7. linux设备驱动归纳总结(三):3面向对象思想和lseek、container_of、write、read 【转】

    linux设备驱动归纳总结(三):3.设备驱动面向对象思想和lseek的实现 转自:http://blog.chinaunix.net/uid-25014876-id-59418.html 一.结构体 ...

  8. 嵌入式C语言自我修养 04:Linux 内核第一宏:container_of

    4.1 typeof 关键字 ANSI C 定义了 sizeof 关键字,用来获取一个变量或数据类型在内存中所占的存储字节数.GNU C 扩展了一个关键字 typeof,用来获取一个变量或表达式的类型 ...

  9. Linux内核中的常用宏container_of其实很简单【转】

    转自:http://blog.csdn.net/npy_lp/article/details/7010752 开发平台:Ubuntu11.04 编 译器:gcc version 4.5.2 (Ubun ...

随机推荐

  1. .NET Core 2.1 IIS 部署 出现500.19 错误

    HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 最后发现是由于项目从 .NET Core 1.0 升级到 .NET Co ...

  2. 【转】WPF自定义控件与样式(12)-缩略图ThumbnailImage /gif动画图/图片列表

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要针对WPF项目 ...

  3. 嵌入式开发之网络心跳包---阻塞和非阻塞以及是否有必要心跳包heartbeat

    1.1 TCP和UDP的心跳包是用来维持长连接的 心跳包只是用来检测socket的链接状态 2.1 非阻塞情况下TCP 心跳包是否有必要建立心跳包 需要, a.如果说 严格 检测掉线的话 那么不管是不 ...

  4. Linux下Qt Creator编辑器无法输入中文解决

    Ubuntu安装了搜狗输入法,在浏览器中可以使用,但是在Qt Creator中却无法输入中文. 解决办法: 执行sudo apt-get install fcitx-libs-qt5 该命令将库文件l ...

  5. Git命令行大全

    git branch 查看本地所有分支 git status 查看当前状态 git commit 提交 git branch -a 查看所有的分支 git branch -r 查看远程所有分支 git ...

  6. js返回上一页并刷新、返回上一页、自动刷新页面

    一.返回上一页并刷新 <a href="javascript:" onclick="self.location=document.referrer;"&g ...

  7. 必问的Java集合框架面试题

    Arraylist 与 LinkedList 异同 是否保证线程安全: ArrayList 和 LinkedList 都是不同步的,也就是不保证线程安全: 底层数据结构: Arraylist 底层使用 ...

  8. js call 理解

    首先直接放定义: 总结 1.前提:fun是函数 2.thisArg是在fun函数运行时 指定的this值 1.使用call来继承,新函数使用已经定义好的函数里的方法 下面直接上实例  函数b直接使用函 ...

  9. 浅析 <路印协议--Loopring> 及整体分析 Relay 源码

    作者:林冠宏 / 指尖下的幽灵 前序: 路印协议功能非常之多及强大,本文只做入门级别的分析. 理论部分请细看其白皮书,https://github.com/Loopring/whitepaper 实际 ...

  10. grafana安装和配置(centos7上配置)

    author:  headsen  chen date:  2019-03-29  10:33:19 1,安装: yum install https://s3-us-west-2.amazonaws. ...