为什么有些c ++编译器会让你获取文字的地址?

时间:2022-12-27 21:13:08

A C++ compiler that I will not name lets you take the address of a literal, int *p = &42;

我不会命名的C ++编译器允许你获取文字的地址,int * p =&42;

Clearly 42 is an r-value and most compilers refuse to do so.

显然42是一个r值,大多数编译器拒绝这样做。

Why would a compiler allow this? What could you do with this other than shoot yourself in the foot?

为什么编译器允许这样做?除了射击自己之外,你能做些什么呢?

5 个解决方案

#1


What if you needed a pointer to an integer with the value of 42? :)

如果你需要一个指向值为42的整数的指针怎么办? :)

C++ references are much like automatically dereferenced pointers. One can create a constant reference to a literal, like this:

C ++引用很像自动解引用指针。可以创建对文字的常量引用,如下所示:

const int &x = 42;

It effectively requires the compiler to initialize a pointer with the address of an integer with the value 42, as you might subsequently do this:

它实际上要求编译器使用值为42的整数的地址初始化指针,因为您可能随后执行此操作:

const int *y = &x;

Combine that with the fact that compilers need to have logic to distinguish between a value which has not had its address taken, and one which has, so it knows to store it in memory. The first need not have a memory location, as it can be entirely temporary and stored in a register, or it may be eliminated by optimization. Taking the address of the value potentially introduces an alias the compiler can't track and inhibits optimization. So, applying the & operator may force the value, whatever it is, into memory.

将此与编译器需要具有逻辑来区分未采用其地址的值和具有的地址的事实相结合,因此它知道将其存储在存储器中。第一个不需要具有存储器位置,因为它可以完全是临时的并存储在寄存器中,或者可以通过优化来消除。获取值的地址可能会引入编译器无法跟踪和禁止优化的别名。因此,应用&运算符可能会将值(无论它是什么)强制到内存中。

So, it's possible you found a bug that combined these two effects.

因此,您可能会发现一个将这两种效果结合起来的错误。

#2


Because 42 is the answer to life, the universe and everything. When asked for its address it is the answer itself.

因为42是生命,宇宙和一切的答案。当被问到它的地址时,它就是答案本身。

#3


Tongue slightly (nut by no means totally) in cheek:

脸颊略微(绝不是完全坚硬)的舌头:

I'd say that in C++ application code taking the address of an integer whether lvalue or rvalue is almost always a mistake. Even using integers, for doing anything much more than controlling loops or counting is probably a design error, and if you need to pass an integer to a function which might change it, use a reference.

我会说在C ++应用程序代码中取一个整数的地址,无论是左值还是右值几乎总是一个错误。即使使用整数,除了控制循环或计数之外做任何事情都可能是设计错误,如果你需要将一个整数传递给可能改变它的函数,请使用引用。

#4


Found something related to rvalue references in C++0x -- move semantics http://www.artima.com/cppsource/rvalue.html

在C ++ 0x中找到与rvalue引用相关的东西 - 移动语义http://www.artima.com/cppsource/rvalue.html

#5


It effectively requires the compiler to initialize a pointer with the address of an integer with the value 42

它实际上要求编译器使用值为42的整数的地址初始化指针

Then why, in some compilers, we can't take the address of a literal directly ?

那么为什么在某些编译器中我们不能直接获取文字的地址呢?

int* ptr = &10;

The reference:

int& ref = 10;

is almost the same thing as a pointer, though...

几乎和指针一样,但......

#1


What if you needed a pointer to an integer with the value of 42? :)

如果你需要一个指向值为42的整数的指针怎么办? :)

C++ references are much like automatically dereferenced pointers. One can create a constant reference to a literal, like this:

C ++引用很像自动解引用指针。可以创建对文字的常量引用,如下所示:

const int &x = 42;

It effectively requires the compiler to initialize a pointer with the address of an integer with the value 42, as you might subsequently do this:

它实际上要求编译器使用值为42的整数的地址初始化指针,因为您可能随后执行此操作:

const int *y = &x;

Combine that with the fact that compilers need to have logic to distinguish between a value which has not had its address taken, and one which has, so it knows to store it in memory. The first need not have a memory location, as it can be entirely temporary and stored in a register, or it may be eliminated by optimization. Taking the address of the value potentially introduces an alias the compiler can't track and inhibits optimization. So, applying the & operator may force the value, whatever it is, into memory.

将此与编译器需要具有逻辑来区分未采用其地址的值和具有的地址的事实相结合,因此它知道将其存储在存储器中。第一个不需要具有存储器位置,因为它可以完全是临时的并存储在寄存器中,或者可以通过优化来消除。获取值的地址可能会引入编译器无法跟踪和禁止优化的别名。因此,应用&运算符可能会将值(无论它是什么)强制到内存中。

So, it's possible you found a bug that combined these two effects.

因此,您可能会发现一个将这两种效果结合起来的错误。

#2


Because 42 is the answer to life, the universe and everything. When asked for its address it is the answer itself.

因为42是生命,宇宙和一切的答案。当被问到它的地址时,它就是答案本身。

#3


Tongue slightly (nut by no means totally) in cheek:

脸颊略微(绝不是完全坚硬)的舌头:

I'd say that in C++ application code taking the address of an integer whether lvalue or rvalue is almost always a mistake. Even using integers, for doing anything much more than controlling loops or counting is probably a design error, and if you need to pass an integer to a function which might change it, use a reference.

我会说在C ++应用程序代码中取一个整数的地址,无论是左值还是右值几乎总是一个错误。即使使用整数,除了控制循环或计数之外做任何事情都可能是设计错误,如果你需要将一个整数传递给可能改变它的函数,请使用引用。

#4


Found something related to rvalue references in C++0x -- move semantics http://www.artima.com/cppsource/rvalue.html

在C ++ 0x中找到与rvalue引用相关的东西 - 移动语义http://www.artima.com/cppsource/rvalue.html

#5


It effectively requires the compiler to initialize a pointer with the address of an integer with the value 42

它实际上要求编译器使用值为42的整数的地址初始化指针

Then why, in some compilers, we can't take the address of a literal directly ?

那么为什么在某些编译器中我们不能直接获取文字的地址呢?

int* ptr = &10;

The reference:

int& ref = 10;

is almost the same thing as a pointer, though...

几乎和指针一样,但......