简体   繁体   English

当我声明一个引用变量时,堆栈会发生什么? C ++

[英]What happen to stack when i declare a reference variable? C++

When i declare a variable, it will be allocated in stack at a certain index of memory right? 当我声明一个变量时,它将在堆栈中的某个内存索引处分配,对吗?

But when i declare a reference variable it will point to the same index of the other one, so no new space have to be allocated in stack... 但是当我声明一个引用变量时,它将指向另一个变量的相同索引,因此不必在堆栈中分配新的空间。

How does the c++ handle this situation? C ++如何处理这种情况?

i mean pratically, it have a table that contains the association between names and indexes? 我的意思是说,它有一个包含名称和索引之间的关联的表?

The compiler do all the work? 编译器完成所有工作吗?

I hope to be clear... If anybody have some manuals or stuff about that i'll be very pleased! 我希望清楚一点...如果有人有一些手册或相关内容,我将非常高兴!

Thanks to all and sorry if i wrote in a bad english! 谢谢所有人,如果我用不好的英语写的话,抱歉!

The C++ standard does not specify ABI so this is implementation-defined. C ++标准未指定ABI,因此这是实现定义的。 But the usual approach is that a reference is implemented as a pointer and so a pointer is allocated on stack. 但是通常的方法是将引用实现为指针,因此将指针分配在堆栈上。 However if this reference is not passed anywhere, it may be optimized away and simply replaced by the variable it points to. 但是,如果此引用未在任何地方传递,则可以对其进行优化,并仅用其指向的变量替换即可。

On a typical implementation, the allocation of the variable depends on how you declare it and where you declare it. 在典型的实现中,变量的分配取决于声明方式和声明位置。

Commonly, variables defined outside of a function are placed in one area of memory (neither stack nor heap). 通常,将在函数外部定义的变量放置在内存的一个区域中(堆栈或堆均无)。 Constants may be placed in a different area or in the executable. 常量可以放在其他区域或可执行文件中。

Variables defined inside a function that are not static may be allocated on the stack. 在函数内部定义的非static变量可以在堆栈上分配。 They could be placed in registers and not on the stack. 它们可以放在寄存器中而不是堆栈中。 Depends on the compiler and the optimization settings. 取决于编译器和优化设置。 The variables could be "optimized away" and not exist in the final executable. 变量可以被“优化掉”,而不存在于最终的可执行文件中。

Many compilers treat references like pointers. 许多编译器将引用视为指针。 So when you pass a variable by reference, the compiler may pass by pointer. 因此,当您通过引用传递变量时,编译器可能会通过指针传递。

C++14 [dcl.ref]/4 says: C ++ 14 [dcl.ref] / 4说:

It is unspecified whether or not a reference requires storage 未确定参考是否需要存储

For a situation like int a; int &b = a; 对于int a; int &b = a;这样的情况int a; int &b = a; int a; int &b = a; the compiler may indeed just store two identifiers that both refer to the same address in its table of identifiers while compiling. 实际上,编译器可能只是在编译时在其标识符表中存储了两个都引用相同地址的标识符。 There would probably be no extra runtime storage required here. 此处可能不需要额外的运行时存储。 Indeed, the rules of C++ say that int b; int &a = b; 确实,C ++的规则说int b; int &a = b; int b; int &a = b; results in exactly the same situation. 结果完全一样。

When a function accepts a parameter by reference: if the compiler was not able to optimize over the function call then it is likely that an address will be passed. 当函数通过引用接受参数时:如果编译器无法通过函数调用进行优化,则可能会传递一个地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我们将引用分配给变量时会发生什么? - What happen when we assign a reference to a variable? 当我们使用VS在C ++项目中将库作为附加依赖项引用时,究竟发生了什么? - What exactly happen when we reference a library as additional dependency in a C++ project using VS? 如果未分配返回未定义对象类型引用的C ++函数的返回值,会发生什么? - What should happen when the return value from a C++ function that returns a reference of an undefined object type is not assigned? 我可以在引用成员上声明C ++吗? - C++ can i declare on reference member? 什么是C ++中的参考变量? - What is a reference variable in C++? 如何在C ++ 11中声明堆栈引用? - How can I declare a stack reference in C++11? 我可以在C ++中“转发声明”什么? - What can I “forward declare” in C++? 与 c++ 中指定的数组大小相比,当我尝试再添加一个值时会发生什么? - What will happen when i try to add one more value compared to the specified size of array in c++? 在 C++ 中,当没有为类声明构造函数时,如果我构造一个带参数的对象会发生什么? - In C++, when no constructor is declared for a class, what will happen if I construct an object with arguments? C++ 中如何发生堆栈下溢? - How can a stack underflow happen in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM