简体   繁体   English

在堆栈上创建的C ++函数返回对象

[英]C++ Function returning object created on the stack

I know that when I use __stdcall (also true for other calling conventions) the returned value is stored in the eax register. 我知道当我使用__stdcall (对于其他调用约定也是如此),返回的值存储在eax寄存器中。 I was wondering how does the following happen: 我想知道以下情况如何发生:

class MyObject
{
private:
    int fourBytesInt;
    long fourBytesLong;
    char name[256];
};

MyObject ReturnMe()
{
    MyObject myObj = MyObject();

    return myObj;
}

int main(void)
{
    MyObject myObj = ReturnMe();

    return 0;
}

sizeof(myObj) is 264 bytes, how does ReturnMe function can return such a large object since the register can hold 32 bit at max (x86 architecture). sizeof(myObj)是264字节, ReturnMe函数如何返回这么大的对象,因为寄存器最大可以容纳32位(x86体系结构)。

Thanks! 谢谢!

The return value is only placed in the register for data types of small enough size. 对于足够小的数据类型,返回值仅放置在寄存器中。 Otherwise, they are returned as a copy on the stack. 否则,它们将作为副本返回堆栈。 Or sometimes the copy can be ellided. 有时副本可能被省略。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM