C ++引用会占用内存吗 [英] C++ do references occupy memory

查看:88
本文介绍了C ++引用会占用内存吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读到引用只是符号表中存在的变量的别名.考虑以下代码

I have read that reference is just an alias to a variable existing in the symbol table. Consider the following code

int main()
{
    int y = 6;
    int &z = y;
    int k = 43;
    test(2,y,5,78);
    cout << &y << "\n";
    cout << &z << "\n";
    cout << &k << "\n";
}

void test(int a,int & x, int g, int h)
{
    cout << &a << "\n";
    cout << &x << "\n";
    cout << &g << "\n";
    cout << &h << "\n";
}

对于输出,我得到

0039F740
0039F848
0039F748
0039F74C
0039F848
0039F848
0039F830

如果引用未占用堆栈中的内存,为什么要偏移内存.例如.在功能测试中,局部变量a为0039F740,而g为0039F748.g不应该是0039F744吗?

If reference does not occupy memory in the stack, why is the memory being offset. Eg. In the function test, local variable a is at 0039F740 but g is at 0039F748. Shouldn't g be at 0039F744?

有人可以深入解释吗?

推荐答案

您的函数有四个参数.

每个参数都必须传递给函数.

Each parameter must be passed to the function.

参数之一是引用这一事实并不能改变这个基本事实.您看到的额外空间是该函数的引用参数.

The fact that one of the parameters is a reference does not change this basic fact. The additional space you see is the reference parameter to the function.

在这种情况下,引用实际上只是变相的指针.当您在本地范围内有一个引用,引用该本地范围内的一个对象时,大多数C ++编译器确实会对其进行优化,以使该引用不会占用任何实际内存.

In this context, the reference is really just a pointer in disguise. When you have a reference in a local scope referencing to an object in the local scope, most C++ compilers will, indeed, optimize it away so that the reference does not take up any actual memory.

但是函数调用是一个全新的球类游戏.该函数希望接收对某个对象的引用.该函数不能以心灵感应的方式知道将什么作为参考传递给它.无论调用什么函数,都要负责提供参考参数.不用说,将需要一些字节来传递该信息,即作为引用传递的对象的地址(最近我是否提到过有关指针的内容?)

But a function call is an entirely new ball game. The function expects to receive a reference to some object. The function cannot know telepathically what is being passed to it as a reference. Whatever is calling the function is responsible for supplying the reference parameter. It goes without saying that a few bytes will be needed to pass along that information, namely the address of an object that's passed as a reference (did I mention something about pointers, recently?)

如果函数是在 static 范围内声明的(没有外部链接),并且选择了足够积极的优化级别进行编译,则C ++编译器将内联函数调用,并且能够优化参考参数.

It is certainly possible that if the function was declared with static scope (no external linkage), and a sufficiently aggressive optimization level is selected for compilation, your C++ compiler will inline the function call, and be able to optimize the reference parameter away.

但是声明具有外部链接的函数通常会导致编译器不必费心尝试内联函数调用.它将继续进行并生成一个成熟的独立函数,该函数希望拥有它有权使用的每个参数.

But declaring a function with external linkage generally results in the compiler not bothering to attempt to inline the function call. It will proceed and generate a full-blown, standalone function, that expects to have each and every parameter it is entitled to.

以更一般的方式回答您的问题:C ++标准不要求引用应该占用内存,但是不要求引用不应该占用内存.C ++编译器可以自由地以任何方式编译代码,只要结果正确无误,以及预期结果是正确的即可.如果在特定情况下C ++编译器想出了如何优化引用,以使其实际上不存在"于其自身的离散对象中,则可以自由地这样做.但这不是必需的.

To answer your question in a more general way: the C++ standard does not require that references should occupy memory, but it does not require that they should not. The C++ compiler is free to compile the code in any way, as long as the results are correct, and what they are expected to be. If, in a particular situation, a C++ compiler figures out how to optimize away a reference so that it does not actually "exist" as a discrete object of its own, it is free to do so. But it is not required to do that.

这篇关于C ++引用会占用内存吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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