offset of 和 container of 解析

2024-09-05 05:32
文章标签 解析 container offset

本文主要是介绍offset of 和 container of 解析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

   在Linux源码中,经常看到大名鼎鼎offsetof 和 container of 的宏定义,这里就此进行解析,并做了实验验证用途,仅用于自己参考记录。

  1. offsetof
  主要作用是获取类型的偏移量
  定义如下:

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

  这里我们一层一层分析下offsetof
  (1)首先是TYPE *0定义了一个TYPE类型的指针,指针的偏移地址为0
  (2)然后从0位置开始选择我们需要看到偏移量的成员MEMBER
  (3)对其取址,得到的即为偏移量

  2. 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.** It takes three arguments – a pointer, type of the container,* and the name of the member the pointer refers to. * The macro will then expand to a new address pointing * to the container which accommodates the respective member. */
#define container_of(ptr, type, member) ({                      \const typeof( ((type *)0)->member ) *__mptr = (ptr);    \(type *)( (char *)__mptr - offsetof(type,member) );})

  第一行看起来和offsetof类似,只是少了取址,多了类型转换和赋值,其实是用一个__mptr代替ptr进行操作,以免误操作。

  第二行这里用字符指针是因为字符类型为1个字节,所以可以方便的计算出__mptr,即ptr的地址位置,然后减去其偏移量,得到的即为TYPE类型的首地址对应的指针。

测试代码如下:

#include <stdio.h>
#include <stdlib.h>
#include "list.h"typedef struct data_list
{int  test_int_data;char test_char_data; struct list_head list;
}data_list;int main()
{	data_list data;data.test_int_data = 123;data.test_char_data = 'a';/* We test offsetof here*/printf("\n-------We test the offset marco-------\n");printf("offset of test_int_data = %lu\n", offsetof(data_list, test_int_data));printf("offset of test_char_data = %lu\n", offsetof(data_list, test_char_data));printf("offset of list_head = %lu\n", offsetof(data_list, list));/*We test container_of here*/printf("\n-------We test the container_of marco-------\n");data_list *ptr;ptr = container_of(&(data.test_int_data), data_list, test_int_data);printf("We define a new pointer ptr to struct data_list\n");printf("After container_of, the values are as follow\n");printf("ptr->test_int_data = %d\n", ptr->test_int_data);printf("ptr->test_char_data = %c\n", ptr->test_char_data); return 0;
}

欢迎关注本人公众号,公众号会更新一些不一样的内容,相信一定会有所收获。
在这里插入图片描述

这篇关于offset of 和 container of 解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1138081

相关文章

Mysql中设计数据表的过程解析

《Mysql中设计数据表的过程解析》数据库约束通过NOTNULL、UNIQUE、DEFAULT、主键和外键等规则保障数据完整性,自动校验数据,减少人工错误,提升数据一致性和业务逻辑严谨性,本文介绍My... 目录1.引言2.NOT NULL——制定某列不可以存储NULL值2.UNIQUE——保证某一列的每一

深度解析Nginx日志分析与499状态码问题解决

《深度解析Nginx日志分析与499状态码问题解决》在Web服务器运维和性能优化过程中,Nginx日志是排查问题的重要依据,本文将围绕Nginx日志分析、499状态码的成因、排查方法及解决方案展开讨论... 目录前言1. Nginx日志基础1.1 Nginx日志存放位置1.2 Nginx日志格式2. 499

MySQL CTE (Common Table Expressions)示例全解析

《MySQLCTE(CommonTableExpressions)示例全解析》MySQL8.0引入CTE,支持递归查询,可创建临时命名结果集,提升复杂查询的可读性与维护性,适用于层次结构数据处... 目录基本语法CTE 主要特点非递归 CTE简单 CTE 示例多 CTE 示例递归 CTE基本递归 CTE 结

Spring Boot 3.x 中 WebClient 示例详解析

《SpringBoot3.x中WebClient示例详解析》SpringBoot3.x中WebClient是响应式HTTP客户端,替代RestTemplate,支持异步非阻塞请求,涵盖GET... 目录Spring Boot 3.x 中 WebClient 全面详解及示例1. WebClient 简介2.

在MySQL中实现冷热数据分离的方法及使用场景底层原理解析

《在MySQL中实现冷热数据分离的方法及使用场景底层原理解析》MySQL冷热数据分离通过分表/分区策略、数据归档和索引优化,将频繁访问的热数据与冷数据分开存储,提升查询效率并降低存储成本,适用于高并发... 目录实现冷热数据分离1. 分表策略2. 使用分区表3. 数据归档与迁移在mysql中实现冷热数据分

C#解析JSON数据全攻略指南

《C#解析JSON数据全攻略指南》这篇文章主要为大家详细介绍了使用C#解析JSON数据全攻略指南,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、为什么jsON是C#开发必修课?二、四步搞定网络JSON数据1. 获取数据 - HttpClient最佳实践2. 动态解析 - 快速

Spring Boot3.0新特性全面解析与应用实战

《SpringBoot3.0新特性全面解析与应用实战》SpringBoot3.0作为Spring生态系统的一个重要里程碑,带来了众多令人兴奋的新特性和改进,本文将深入解析SpringBoot3.0的... 目录核心变化概览Java版本要求提升迁移至Jakarta EE重要新特性详解1. Native Ima

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

MyBatis中$与#的区别解析

《MyBatis中$与#的区别解析》文章浏览阅读314次,点赞4次,收藏6次。MyBatis使用#{}作为参数占位符时,会创建预处理语句(PreparedStatement),并将参数值作为预处理语句... 目录一、介绍二、sql注入风险实例一、介绍#(井号):MyBATis使用#{}作为参数占位符时,会