什么是指针的一个参考值之间的低级别差? [英] What's the low-level difference between a pointer an a reference?

查看:110
本文介绍了什么是指针的一个参考值之间的低级别差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们有这个code:

  INT富= 100;
INT和放大器;基准= foo的;
为int *指针=&放大器;参考;

有在参考的数据和指针的数据没有实际的二进制差异。 (它们都包含在内存中的位置

第2部分

那么,做指针和引用(之间的所有其他差异<一个href=\"http://stackoverflow.com/questions/57483/difference-between-pointer-variable-and-reference-variable-in-c\">discussed这里)进来吗?该编译器执行它们或者它们实际​​上是不同类型的变量上assemeb​​ly水平?换句话说,执行以下操作产生相同的汇编语言?

 富= 100;
INT和放大器;基准= foo的;
基准= 5;富= 100;
为int *指针=&放大器; foo的;
*指针= 5;


解决方案

从理论上讲,他们可以以不同的方式来实现。

在实践中,每一个我见过的编译器编译指针和引用在同一台机器code。区别是完全在语言级别。

但是,像cdiggins说,你不应该的依赖的上泛化,直到你已经验证这是你的编译器和平台真实的。

If we have this code:

int foo=100;
int& reference = foo;
int* pointer = &reference;

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)

part 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.

But, like cdiggins says, you shouldn't depend on that generalization until you've verified it's true for your compiler and platform.

这篇关于什么是指针的一个参考值之间的低级别差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆