C++ 有没有办法找到引用的地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1950779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:41:25  来源:igfitidea点击:

Is there any way to find the address of a reference?

c++reference

提问by Sandeep

Is there any way to find the address of a reference?

有没有办法找到引用的地址?

Making it more specific: The address of the variable itself and not the address of the variable it is initialized with.

使其更具体:变量本身的地址,而不是初始化它的变量的地址。

回答by Brian R. Bondy

References don't have their own addresses. Although references may be implemented as pointers, there is no need or guarantee of this.

参考文献没有自己的地址。虽然引用可以实现为指针,但没有必要或保证这一点。

The C++ FAQsays it best:

C++ FAQ说得最好:

Unlike a pointer, once a reference is bound to an object, it can not be "reseated" to another object. The reference itself isn't an object (it has no identity; taking the address of a reference gives you the address of the referent; remember: the reference is its referent).

与指针不同,一旦引用绑定到一个对象,它就不能“重新定位”到另一个对象。引用本身不是一个对象(它没有身份;获取引用的地址会给你所指的地址;记住:引用是它的所指)。

Please also see my answer here for a comprehensive list of how references differ from pointers.

另请参阅我在此处的回答,以获取有关引用与指针有何不同完整列表

The reference is its referent

参考是它的参照物

回答by Martin York

NO. There is no way to get the address of a reference.
That is because a reference is not an object, it is an alias (this means it is another name for an object).

。没有办法获得引用的地址。
那是因为引用不是对象,而是别名(这意味着它是对象的另一个名称)。

int  x = 5;
int& y = x;

std::cout << &x << " : " << &y << "\n";

This will print out the same address.
This is because 'y' is just another name (an alias) for the object 'x'.

这将打印出相同的地址。
这是因为“y”只是对象“x”的另一个名称(别名)。

回答by Potatoswatter

The ISO standard says it best:

ISO 标准说得最好:

There shall be no references to references, no arrays of references, and no pointers to references.

不应有对引用的引用,没有引用数组,也没有指向引用的指针。

I don't like the logic a lot of people are using here, that you can't do it because the reference isn't "guaranteed to be just a pointer somewhere anyway." Just as int xmay be only a processor register with no address, but magically becomes a memory location when & xis used, it still may be possible for the compiler to allow what you want.

我不喜欢很多人在这里使用的逻辑,你不能这样做,因为引用并不“保证只是某个地方的指针”。正如int x可能只是一个没有地址的处理器寄存器,但在& x使用时神奇地变成了一个内存位置,编译器仍然有可能允许你想要的东西。

In the past, many compilers did allow exactly what you're asking for, eg

过去,许多编译器确实允许您所要求的内容,例如

int x, y;
int &r = x;
&r = &y; // use address as an lvalue; assign a new referent

I just checked and GCC will compile it, but with a strongly worded warning, and the resulting program is broken.

我刚刚检查过,GCC 会编译它,但是有一个措辞强烈的警告,结果程序被破坏了。

回答by AnT

No.

不。

As Bjarne Stroustrup says in TC++PL, a reference can be thought of as just another name for an existing entity (object or function). While this is not always the most precise description of the underlying low-level mechanism that implements references, it is a very good description of the concept the references are intended to implement at the language level. Not surprisingly, the language provides no means to obtain the address of reference itself.

正如 Bjarne Stroustrup 在 TC++PL 中所说,引用可以被认为只是现有实体(对象或函数)的另一个名称。虽然这并不总是对实现引用的底层机制的最精确描述,但它是对引用旨在在语言级别实现的概念的非常好的描述。毫不奇怪,该语言没有提供获取引用地址本身的方法。

At language level reference is not guaranteed to occupy a place in storage, and therefore in general case it has no address.

在语言级别引用不能保证在存储中占据一个位置,因此在一般情况下它没有地址。

回答by Ben Mesander

From another instance of this same question: $8.3.2/3 - "It is unspecified whether or not a reference requires storage (3.7).".

来自同一问题的另一个实例:$8.3.2/3 - “未指定引用是否需要存储(3.7)。”。

So the C++ standard allows the compiler/runtime implementor to choose whether or not a reference lives in a separate memory location. However note that if it does live in a separate memory location, you can't find its address in a standard-compliant manner. So don't do it.

因此,C++ 标准允许编译器/运行时实现者选择引用是否位于单独的内存位置。但是请注意,如果它确实存在于单独的内存位置,则无法以符合标准的方式找到其地址。所以不要这样做。

If you take an address of a reference, by definition in the C++ standard, you will get the address of what it refers to, rather than the address of the reference, if in fact that reference even exists as a separate entity in the runtime environment or not.

如果您获取引用的地址,根据 C++ 标准中的定义,您将获得它所引用的地址,而不是引用的地址,如果实际上该引用甚至作为单独的实体存在于运行时环境中或不。

回答by Jesse Emond

Just use the '&' operator. e.g :

只需使用“&”运算符。例如:

int x = 3;
int &y = x;
cout<<&y<<endl;

This will return the address of x since y is nothing more than the address of x.

这将返回 x 的地址,因为 y 只不过是 x 的地址。

回答by Drew Dormann

Not reliably, as references don't have to have a unique location in addressable memory.

不可靠,因为引用不必在可寻址内存中具有唯一位置。

回答by MSN

Not by itself. If you want its "address", shove it in a struct or class. Even then that isn't necessarily guaranteed to get you within the vicinity of what you probably want to do which is using a pointer. If you want proof, the sizeof of a reference is equal to the referent type. Try it with char & and see.

不是靠自己。如果您想要它的“地址”,请将其放入结构或类中。即使那样,也不一定能保证让您接近您可能想要使用指针执行的操作。如果你想要证明,引用的 sizeof 等于被引用类型。用 char & 试试看。

回答by Jim Fell

It is possible, but not strictly using C++. Since the reference is passed as a parameter of a function, its value will be stored on the stack or in a register. This is hardware architecture dependent.Access to these values will require inline assembly. Consult the reference manual for the processor you are using to determine stack behavior and register addresses. Corrupting the stack or registers can very easily cause BSOD, data loss, or even permanent damage to your system.Proceed with extreme caution.

这是可能的,但不是严格使用 C++。由于引用作为函数的参数传递,其值将存储在堆栈或寄存器中。 这取决于硬件架构。访问这些值需要内联汇编。请查阅您正在使用的处理器的参考手册,以确定堆栈行为和寄存器地址。 损坏堆栈或寄存器很容易导致 BSOD、数据丢失,甚至对您的系统造成永久性损坏。谨慎行事。

回答by tieuthanhliem

If you implement a reference as a member of a struct, you then can get its address:

如果将引用实现为结构的成员,则可以获取其地址:

struct TestRef{
  int& r;
  int i;
  TestRef(int& ref): r(ref){
  }
};

The reference indeed a pointer (in my case using Xcode compiler) and you can update it's value to re-assign the reference to a new variable. To do so we need to find out the address of the reference and trick it value to address of other variable

引用确实是一个指针(在我的情况下使用 Xcode 编译器),您可以更新它的值以将引用重新分配给新变量。为此,我们需要找出引用的地址并将其值欺骗为其他变量的地址

Now the address of the reference TestRef.r is the address of TestRef object.Because r is the first member of TestRef.

现在引用TestRef.r的地址就是TestRef对象的地址。因为r是TestRef的第一个成员。

You can re-assign the reference by updating the value store in the memory of TestRef.r.

您可以通过更新 TestRef.r 内存中的值存储来重新分配引用。

This code below shows that you can get address of reference and you and re-assign a reference to a difference variable. Note: my OS is X64 OS (I use Xcode MacBook Pro 2015, MacOs 10.15.1).

下面的代码显示您可以获取引用地址,然后将引用重新分配给差异变量。注意:我的操作系统是 X64 操作系统(我使用 Xcode MacBook Pro 2015,MacOs 10.15.1)。

#include <iostream>
using namespace std;

struct TestRef{
  int& r;
  int i;
  TestRef(int& ref): r(ref){}
};

int main(int argc, const char * argv[]) {
  int i = 10;
  int j = 11;
  TestRef r(i); // r.r is reference to i

  cout << r.r << " " << i << " " << j << endl; // Output: 10 10 11

  int64_t* p = (int64_t*)&r; // int32_t in 32 bit OS; 
  // Note: 
  // p is the address of TestRef r and also the address of the reference r.r
  // *p is the address of i variable
  //
  // Difficult to understand? r.r indeed a pointer to i variable 
  // *p will return the address inside the memory of r.r
  // that is the address of i variable
  // this statement is true: *p == &i  
  // ------>
  // now we change the value of *p to the address of j 
  // then r.r will be the reference of j instead the reference of i

  *p = (int64_t)&j;          // int32_t in 32 bit OS; 

  cout << r.r << " " << i << " " << j << endl; // Output: 11 10 11
  return 0;
}

So in fact you can work around to re-assign a reference, like a hacker.

因此,实际上您可以像黑客一样解决重新分配参考的问题。