当前位置:网站首页>C语言中变量在内存中的保存与访问
C语言中变量在内存中的保存与访问
2022-08-02 20:08:00 【Code Writers】
CPU通过内存地址访问元素
int i=0x12345678
假如i的地址是0x100,那么在小端存储体系中
地址 数值
0x100 0x78
0x101 0x56
0x102 0x34
0x103 0x12
CPU访问内存需要同时具备两个因素:
内存基址:从哪里访问内存——就是地址
内存布局:访问几个字节(怎样解析,解释方法)
定义变量时指明的数据类型就是指明内存布局,i是int型变量,所以CPU知道连同0x100后面四个地址一起组成i的值
强制类型转换改变的就是解析方法,也就是内存布局
char p= (char)i
这样CPU会只将地址0x100中的0x78解析为一个char类型的字符然后赋给p
同理 short p=(short)i
得到的short型变量p的值是0x5678
这样看来,只要给定了内存基址和内存布局就可以不用实际变量名得到变量值
比如 *(short *)0x102实际上就是值0x1234
对于结构体变量来说也是如此解析的(虽然其中有内存对齐问题)
struct foo{
int first;
short second;
char third;
}*pfoo;
对于语句 pfoo->first中的->操作符,它首先算出右侧的变量在左侧结构体中的偏移量,然后让左侧变量的地址(指针所指地址)加上偏移量就实际上得到了右侧变量的内存基址,然后根据右侧变量类型,也就是得到了它的内存布局,这样两个因素都具备了也就得到了它的值
在一些实际应用中,会看到这样的链表应用方法:将链表嵌入到数据结构中(学校里用的链表大概都是将数据结构嵌入链表)
struct data_in_list{ //数据嵌入双向链表
int price;
… //一些可能要用的数据
struct data_in_list *next;
struct data_in_list *pre;
}
//下面是链表嵌入数据结构中
struct list{
struct list *next;
struct list *pre;
}
struct list_in_data{
…//一些会用到的数据
struct list lnode;
}
比如在2.6版本的Linux内核中,这种数据结构是很常见的,进程描述符task_struct结构体中就包含链表节点,所有进程描述符是用一个双向链表连起来的,而根据链表节点访问结构体就是用到了上面说的CPU访问数据的两个要素
type 是结构体类型
address 是结构体中链表节点
field 是链表节点类型
(type*)(address - &(((type*)0)->field)) //(type*)0表示内存基址是0的type型变量,&符获得了field在type中的偏移量
//这样可以根据链表节点得到其所在的type结构体的地址,实际上可以根据结构体中任一变量得到整个结
边栏推荐
- The so-called fighting skill again gao also afraid of the chopper - partition, depots, table, and the merits of the distributed
- 【SLAM】DM-VIO(ros版)安装和论文解读
- 传感器工作原理
- "A daily practice, happy water problem" 1374. Generate a string with an odd number of each character
- OP-5,输入/输出信号范围-一信号处理能力
- 【 LeetCode 】 1374. Generate each character string is an odd number
- iframe------------frame-
- 【软件工程导论】软件工程导论笔记
- C# Barrier类
- 线程安全(上)
猜你喜欢

4 kmiles join YiSheng group, with more strong ability of digital business, accelerate China's cross-border electricity full domain full growth

即时通讯开发移动端网络短连接的优化手段

封装和包、访问修饰权限

【手撕AHB-APB Bridge】~ AMBA总线 之 APB

浅议.NET遗留应用改造
The time series database has been developed for 5 years. What problem does it need to solve?

用了TCP协议,就一定不会丢包吗?

Introduction of uncommon interfaces of openlayers

第七章 噪声

Digital twins help visualize the construction of smart cities
随机推荐
Leetcode刷题——23. 合并K个升序链表
Linphone 被叫方如何解析来电SIP消息中的自定义头消息
LeetCode 622 设计循环队列[数组 队列] HERODING的LeetCode之路
【StoneDB性能相关工具】内存监控
六石管理学:入门机会只有一次,先把产品做好
Silver circ: letter with material life insurance products should be by the insurance company is responsible for the management
ShardingSphere-proxy +PostgreSQL implements read-write separation (static strategy)
golang源码分析之geoip2-golang
五大维度解读软件测试分类
ALV report learning summary
第一次进入前20名
Informatics Olympiad All-in-One (1260: [Example 9.4] Intercepting Missiles (Noip1999))
【LeetCode】622. 设计循环队列
信息学奥赛一本通(1258:【例9.2】数字金字塔)
SQL Server实现group_concat功能
接口测试常用工具及测试方法(入门篇)
Tencent YunMeng every jie: I experienced by cloud native authors efficiency best practices case
J9 Digital Currency Theory: Identifying Web3's New Scarcity: Open Source Developers
[21 Days Learning Challenge] Bubble Sort and Insertion Sort
SQL 入门之第一讲——MySQL 8.0.29安装教程(windows 64位)