返回对象存储在哪里? [英] Where is the return object stored?

查看:83
本文介绍了返回对象存储在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常了解函数如何按值返回对象.但是我想从较低的层次上理解它.合理的组装水平.

I generally understand how a function returns an object by value. But I wanted to understand it on the lower level. Assembly level if reasonable.

我理解这段代码

ClassA fun(){
    ClassA a;
    a.set(...);
    return a;
}

在内部转换为

void fun(Class& ret){
    ClassA a;
    a.set(...);
    ret.ClassA::ClassA(a);
}

可以有效地在返回值上调用复制构造函数.

Which effectively call the copy constructor on the return value.

我还了解到,有些优化(例如NRVO)可以生成以下代码,从而避免了复制构造函数.

I also understand that there are some optimizations (like NRVO) that could generate the following code avoiding the copy constructor.

void fun(Class& ret){
    ret.set(...);
}

但是我的问题有点基本.实际上,它与对象无关.甚至可以是原始类型.

However my question is a bit more basic. It doesn't really have to do with Objects in specific. It could be even primitive types.

让我们说我们有以下代码:

Lets say we have this code:

int fun(){
   return 0;
}
int main(){
    fun();
}

我的问题是返回对象存储在内存中的位置.

My question is where is the return object stored in memory.

如果我们看一下堆栈...有一个 main 的堆栈框架,然后是 fun 的堆栈框架.返回对象是否存储在某个地址中,例如可能在两个堆栈帧之间?或者,它可能存储在 main 堆栈帧中的某个位置(可能是在生成的代码中通过引用传递的地址).

If we look at the stack... There is the stack frame of main and then the stack frame of fun. Is the return object stored in some address like maybe between the two stack frames? Or maybe it is stored somewhere in the main stack frame (and possibly that's the address which is passed by reference in the generated code).

我已经考虑过了,第二个似乎更实用,但是我不明白编译器如何知道要在 main 的堆栈帧中放入多少内存?它会计算最大的返回类型是什么,并且即使可能浪费一些内存也要进行推算?还是它是动态完成的,它仅在调用函数之前分配该空间?

I have thought about it and the second one seems more practical however I don't understand how the compiler know how much memory to push in the stack frame of main? Does it calculate what is the largest return type and push that even though there could be some wasted memory? Or is it done dynamically, it allocates that space only before the function is called?

推荐答案

C ++语言规范未指定这些底层详细信息.它们由每个C ++实现指定,并且实际实现细节因平台而异.

The C++ language specification does not specify these low level details. They are specified by each C++ implementation, and the actual implementation details vary from platform to platform.

几乎在每种情况下,简单的本机类型的返回值都会在特定的指定CPU寄存器中返回.当一个函数返回一个类实例时,细节会根据实现的不同而变化.有几种常见的方法,但是典型的情况是调用方负责在调用函数并将附加的隐藏参数传递给函数之前,在堆栈上为栈中的返回值分配足够的空间,函数将在该函数中进行复制.返回值(如果是RVO,则构造它).或者,该参数是隐式的,该函数可以在调用的堆栈帧之后在堆栈上为返回值本身找到空间.

In nearly every case, a return value that's a simple, native type gets returned in a certain, designated, CPU register. When a function returns a class instance, the details vary, depending on the implementation. There are several common approaches, but the typical case would be the caller being responsible for allocating sufficient space for the return value on the stack, before calling the function and passing an additional hidden parameter, to the function, where the function is going to copy the returned value (or construct it, in case of RVO). Or, the parameter is implicit, and the function can find the space, on the stack, for the return value itself, after the call's stack frame.

给定的C ++实现也有可能仍将使用CPU寄存器来返回足够小以适合单个CPU寄存器的类.或者,也许保留了一些CPU寄存器用于返回稍大的类.

It's also possible that a given C++ implementation will still use a CPU register to return classes that are small enough to fit into a single CPU register. Or, perhaps, a few CPU registers are reserved for returning slightly bigger classes.

详细信息各不相同,您将需要查阅C ++编译器或操作系统的文档,以确定适用于您的特定详细信息.

The details vary, and you will need to consult the documentation for your C++ compiler, or your operating system, to determine the specific details that apply to you.

这篇关于返回对象存储在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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