C++指针等于地址加偏移量

简介: C++指针等于地址加偏移量

本文通过c++示例代码演示指针的加减法运算及对 “指针 = 地址 + 偏移量” 的理解。
研究示例

  1. 首先来检查各种变量类型所占的内存大小

include

using namespace std;

int main(){
cout << sizeof(char) << endl; // 1 Byte
cout << sizeof(short) << endl; // 2 Byte
cout << sizeof(int) << endl; // 4 Byte
cout << sizeof(long long) << endl; // 8 Byte
return 0;
}

  1. 各类型的指针进行加减运算

各类型的指针进行加减运算是以元素为单位进行加减,而非地址的最小单位(1个Byte)。以下演示各类型指针指向的地址:

include

using namespace std;

int main(){
char p_char = new char('a');
cout << "
p_char = " << p_char << endl;
cout << "p_char = " << (void
)p_char << endl; // char指针在使用cout输出会直接打印变量而非地址,要加(void)
cout << "p_char + 1 = " << (void
)(p_char + 1) << endl;
cout << "p_char + 2 = " << (void)(p_char + 2) << endl;
cout << "p_char - 1 = " << (void
)(p_char - 1) << endl;

short *p_short = new short(456);
cout << "*p_short = " << *p_short << endl; 
cout << "p_short = " << p_short << endl; 
cout << "p_short + 1 = " << p_short + 1 << endl; 
cout << "p_short + 2 = " << p_short + 2 << endl; 
cout << "p_short - 1 = " << p_short - 1 << endl; 

int *p_int = new int(123);
cout << "*p_int = " << *p_int << endl; 
cout << "p_int = " << p_int << endl; 
cout << "p_int + 1 = " << p_int + 1 << endl; 
cout << "p_int + 2 = " << p_int + 2 << endl; 
cout << "p_int - 1 = " << p_int - 1 << endl; 

long long *p_long_long = new long long(456789);
cout << "*p_long_long = " << *p_long_long << endl; 
cout << "p_long_long = " << p_long_long << endl; 
cout << "p_long_long + 1 = " << p_long_long + 1 << endl; 
cout << "p_long_long + 2 = " << p_long_long + 2 << endl; 
cout << "p_long_long - 1 = " << p_long_long - 1 << endl; 

delete p_char, p_short, p_int, p_long_long;
return 0;

}

输出:(char, short, int, long long的指针的最小移动单位分别是1, 2, 4, 8 Byte,刚好是对应的单个元素的类型所占内存大小)

p_char = a
p_char = 0xe41600
p_char + 1 = 0xe41601
p_char + 2 = 0xe41602
p_char - 1 = 0xe415ff
p_short = 456
p_short = 0xe41620
p_short + 1 = 0xe41622
p_short + 2 = 0xe41624
p_short - 1 = 0xe4161e
p_int = 123
p_int = 0xe41640
p_int + 1 = 0xe41644
p_int + 2 = 0xe41648
p_int - 1 = 0xe4163c
p_long_long = 456789
p_long_long = 0xe41660
p_long_long + 1 = 0xe41668
p_long_long + 2 = 0xe41670
p_long_long - 1 = 0xe41658

说明:指针 = 地址 + 偏移量。即指针除了包含地址信息之外,还包含解析这个地址的方式(从该地址开始向后读取多少个Byte),因此“指针就是地址”的说法是不准确的,一个int p1和char p2 指向的地址可能是相同的,但是解析这个地址的方式是不同的。

  1. 强制转换指针类型对地址进行解析
    [box.bbq400.com)
    [box.iirrr.com)
    [box.liulen.com)
    [box.52jxl.com)
    [box.youranzide.com)
    [box.728ddd.com)
    [box.dflk.net)

可以强行指定指针的类型对同一块内存的数据进行不同方式的解析。例如:

include

using namespace std;

int main(){
char arr[10] = {0,1,2,3,4,5,6,7,8,9};
unsigned int l = sizeof(arr)/sizeof(arr[0]);

char* p_char = arr;
cout << "*p_char = " << (int)*p_char << endl; 
cout << "*(p_char + 1) = " << (int) *(p_char + 1) << endl; 
cout << "*(p_char + 2) = " << (int)*(p_char + 2) << endl; 
cout << "*(p_char + 3) = " << (int)*(p_char + 3) << endl; 
cout << "*(p_char + 9) = " << (int)*(p_char + 9) << endl; 

short* p_short = (short*)arr;
cout << hex << "*p_short = " << *p_short << endl; 
cout << hex << "*(p_short + 1) = " << *(p_short + 1) << endl; 
cout << hex << "*(p_short + 2) = " << *(p_short + 2) << endl; 

int *p_int = (int*)arr;
cout << hex << "*p_int = " << *p_int << endl; 
cout << hex << "*(p_int + 1) = " << *(p_int + 1) << endl; 
cout << hex << "*(p_int + 2) = " << *(p_int + 2) << endl; 

return 0;

}

输出:

p_char = 0 (p_char + 1) = 1
(p_char + 2) = 2 (p_char + 3) = 3
(p_char + 9) = 9 p_short = 100
(p_short + 1) = 302 (p_short + 2) = 504
p_int = 3020100 (p_int + 1) = 7060504
*(p_int + 2) = fdf60908

解释如下图:arr数组在内存中占10个Byte,分别定义了三种类型的指针char, short, int*并且都指向首地址arr[0],则三种指针的差异体现在两个方面:1、解引用(解析地址)时的偏移量分别为1,2,4个Byte;2、加减时分别以1,2,4个Byte为单位移动地址。

注意:

1、本编译器采用小端模式,因此*p_short = 0x0100 而非 0x0001。

2、*(p_int + 2)访问到了未知的内存,因此结果中打印出来了不受控制的数据"0xfdf60908",警示了我们在使用指针时,尤其是涉及到类型转换、加减运算、赋值等操作时,一定要避免指针越界,否则将会产生不可预知的危险后果。
总结

本文通过几个简单的c++程序验证了 “指针 = 地址 + 偏移量” 这一结论,希望能对指针如何操作内存有更深入的理解。

另外,本文所讨论的指针均为变量指针,而函数指针不能进行加减运算(会报warning: pointer to a function used in arithmetic),我会另写一篇文章讨论函数指针。

相关文章
|
24天前
|
弹性计算 人工智能 架构师
阿里云携手Altair共拓云上工业仿真新机遇
2024年9月12日,「2024 Altair 技术大会杭州站」成功召开,阿里云弹性计算产品运营与生态负责人何川,与Altair中国技术总监赵阳在会上联合发布了最新的“云上CAE一体机”。
阿里云携手Altair共拓云上工业仿真新机遇
|
16天前
|
存储 关系型数据库 分布式数据库
GraphRAG:基于PolarDB+通义千问+LangChain的知识图谱+大模型最佳实践
本文介绍了如何使用PolarDB、通义千问和LangChain搭建GraphRAG系统,结合知识图谱和向量检索提升问答质量。通过实例展示了单独使用向量检索和图检索的局限性,并通过图+向量联合搜索增强了问答准确性。PolarDB支持AGE图引擎和pgvector插件,实现图数据和向量数据的统一存储与检索,提升了RAG系统的性能和效果。
|
20天前
|
机器学习/深度学习 算法 大数据
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
2024“华为杯”数学建模竞赛,对ABCDEF每个题进行详细的分析,涵盖风电场功率优化、WLAN网络吞吐量、磁性元件损耗建模、地理环境问题、高速公路应急车道启用和X射线脉冲星建模等多领域问题,解析了问题类型、专业和技能的需要。
2577 22
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
|
18天前
|
人工智能 IDE 程序员
期盼已久!通义灵码 AI 程序员开启邀测,全流程开发仅用几分钟
在云栖大会上,阿里云云原生应用平台负责人丁宇宣布,「通义灵码」完成全面升级,并正式发布 AI 程序员。
|
3天前
|
JSON 自然语言处理 数据管理
阿里云百炼产品月刊【2024年9月】
阿里云百炼产品月刊【2024年9月】,涵盖本月产品和功能发布、活动,应用实践等内容,帮助您快速了解阿里云百炼产品的最新动态。
阿里云百炼产品月刊【2024年9月】
|
2天前
|
存储 人工智能 搜索推荐
数据治理,是时候打破刻板印象了
瓴羊智能数据建设与治理产品Datapin全面升级,可演进扩展的数据架构体系为企业数据治理预留发展空间,推出敏捷版用以解决企业数据量不大但需构建数据的场景问题,基于大模型打造的DataAgent更是为企业用好数据资产提供了便利。
164 2
|
20天前
|
机器学习/深度学习 算法 数据可视化
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码
2024年中国研究生数学建模竞赛C题聚焦磁性元件磁芯损耗建模。题目背景介绍了电能变换技术的发展与应用,强调磁性元件在功率变换器中的重要性。磁芯损耗受多种因素影响,现有模型难以精确预测。题目要求通过数据分析建立高精度磁芯损耗模型。具体任务包括励磁波形分类、修正斯坦麦茨方程、分析影响因素、构建预测模型及优化设计条件。涉及数据预处理、特征提取、机器学习及优化算法等技术。适合电气、材料、计算机等多个专业学生参与。
1576 16
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码
|
22天前
|
编解码 JSON 自然语言处理
通义千问重磅开源Qwen2.5,性能超越Llama
击败Meta,阿里Qwen2.5再登全球开源大模型王座
978 14
|
4天前
|
Linux 虚拟化 开发者
一键将CentOs的yum源更换为国内阿里yum源
一键将CentOs的yum源更换为国内阿里yum源
221 2
|
17天前
|
人工智能 开发框架 Java
重磅发布!AI 驱动的 Java 开发框架:Spring AI Alibaba
随着生成式 AI 的快速发展,基于 AI 开发框架构建 AI 应用的诉求迅速增长,涌现出了包括 LangChain、LlamaIndex 等开发框架,但大部分框架只提供了 Python 语言的实现。但这些开发框架对于国内习惯了 Spring 开发范式的 Java 开发者而言,并非十分友好和丝滑。因此,我们基于 Spring AI 发布并快速演进 Spring AI Alibaba,通过提供一种方便的 API 抽象,帮助 Java 开发者简化 AI 应用的开发。同时,提供了完整的开源配套,包括可观测、网关、消息队列、配置中心等。
735 9