简体   繁体   中英

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. 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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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