多个“ const reference”变量可以共享同一内存吗? [英] Can multiple 'const reference' variables share the same memory?
问题描述
我想知道使用多个常量引用是否会产生内存成本?同一范围内指向同一对象的变量:
const Animal&动物= getAnimal();
const Dog& dog = static_cast< const Dog&>(动物);
从概念上讲,动物
和 dog
是两个变量,每个变量的指针大小,因此将占用2个寄存器(或堆栈上2 *指针大小的区域)。
但是(假设(没有多重继承等),编译器可以知道它们在整个生命周期中都必须拥有相同的指针值。
因此,这两个变量可以共享一个寄存器(或
可以,我的意思是:
- C ++标准允许吗?
- 现代编译器会这样做吗?
< blockquote>
C ++标准允许吗?
当然,为什么不呢?您无法分辨出区别。所谓的如果规则是指允许编译器进行任何优化,只要可观察的行为与未进行任何优化相同即可(请注意:有一些例外,其中允许优化更改可观察行为)。
<从概念上讲,动物和狗是两个变量,每个变量的指针大小为...
不。从概念上讲,引用是别名。它们不需要占用任何空间,因为它们只是实际对象的不同名称。 C ++标准未指定引用的大小或引用的实现方式。 sizeof
引用可为您提供所引用对象的大小。引用的地址是被引用对象的地址。我不知道有什么方法可以区别引用是作为指针实现还是以其他方式实现(我强烈怀疑是否存在可移植的方法)。
现代编译器会这样做吗?
要回答这个问题,建议您使用一些实际的代码,并查看编译器的输出。这是一个很好的工具,可以帮助您: https://godbolt.org/ 。
PS:我觉得有点误会。实际上,在您的示例中, const
并不重要。具有 const
引用并不表示该值不会更改。这仅表示不允许您通过该引用更改值。也许最好举个小例子来说明:
struct foo {
const int&参考
};
int main(){
int x = 1;
foo f {x};
x = 42;
}
此处 f
持有 const
对 x
的引用。这并不意味着 x
永远不会被修改。这仅意味着 f
无法通过 ref
修改 x
。这在多线程环境中尤其重要,在该环境中,假设一个对象是 const
只是因为您有一个 const
引用,造成麻烦。
I'm wondering if there's a memory cost to having multiple "constant reference" variables in the same scope pointing to the same object:
const Animal& animal = getAnimal();
const Dog& dog = static_cast<const Dog&>(animal);
Conceptually, animal
and dog
are two variables, each of pointer size, and thus would take up 2 registers (or a 2*pointer-size region on the stack).
But (assuming that there's no multiple inheritance etc.), the compiler could know that they both must hold the same pointer value throughout their lifetime.
So, can these two variables share a single register (or a single pointer-sized region on the stack)?
By "can", I mean:
- Does the C++ standard allow it?
- Would modern compilers do it?
Does the C++ standard allow it?
Sure, why not. There is no way you could tell the difference. The so-called "as-if rule" allows compilers to do any optimization as long the observable behavior is the same as if it didn't do any optimizations (side note: there are exceptions, where optimizations are allowed to change observable behavior).
Conceptually, animal and dog are two variables, each of pointer size, ...
Nope. Conceptually, references are aliases. They need not take any space, because they are just a different name for the actual object. The C++ standard does not specify the size a reference takes or how it is implemented. sizeof
a reference gives you the size of the refered to object. The address of a reference is the address of the referd to object. I am not aware of any way to tell the difference if a reference is implemented as pointer or in any other way (and I strongly doubt that there is a portable way).
Would modern compilers do it?
To answer this I suggest you to take some real code and look at the compilers output. This is a nice tool to help with that: https://godbolt.org/.
PS: I sense a small misunderstanding. In fact the const
in your example is not that relevant. Having a const
reference does not imply that the value will not change. It only means that you are not allowed to change the value through that reference. Maybe best exaplained by a small example:
struct foo {
const int& ref;
};
int main() {
int x = 1;
foo f{x};
x = 42;
}
Here f
holds a const
reference to x
. That doesn't mean that x
will never get modified. It only means that f
cannot modify x
via ref
. This is especially important in a multithreaded environment, where assuming that an object is const
just because you have a const
reference will cause trouble.
这篇关于多个“ const reference”变量可以共享同一内存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!