What#39;s the low-level difference between a pointer an a reference?(指针和引用之间的低级区别是什么?)
问题描述
如果我们有这个代码:
int foo=100;
int& reference = foo;
int* pointer = &reference;
引用的数据和指针的数据没有实际的二进制差异.(它们都包含 foo 在内存中的位置)
There's no actual binary difference in the reference's data and the pointer's data. (they both contain the location in memory of foo)
第 2 部分
那么指针和引用之间的所有其他差异在哪里(在这里讨论)进来?编译器是否强制执行它们,或者它们实际上是汇编级别上的不同类型的变量?换句话说,以下是否产生相同的汇编语言?
So where do all the other differences between pointers and references (discussed here) come in? Does the compiler enforce them or are they actually different types of variables on the assemebly level? In other words, do the following produce the same assembly language?
foo=100;
int& reference=foo;
reference=5;
foo=100;
int* pointer=&foo;
*pointer=5;
推荐答案
理论上,它们可以以不同的方式实现.
Theoretically, they could be implemented in different ways.
实际上,我见过的每个编译器都编译指向相同机器代码的指针和引用.区别完全在于语言层面.
In practice, every compiler I've seen compiles pointers and references to the same machine code. The distinction is entirely at the language level.
但是,就像 cdiggins 所说的那样,您不应该依赖这种概括,直到您验证它对您的编译器和平台来说是正确的.
But, like cdiggins says, you shouldn't depend on that generalization until you've verified it's true for your compiler and platform.
这篇关于指针和引用之间的低级区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:指针和引用之间的低级区别是什么?
基础教程推荐
- 我有静态或动态 boost 库吗? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 这个宏可以转换成函数吗? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
