C++函数返回在堆栈上创建的对象
C++ Function returning object created on the stack
我知道当我使用__stdcall
时(对于其他调用约定也是如此),返回的值存储在eax
寄存器中。我想知道以下情况是如何发生的:
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)
是264字节,ReturnMe
函数怎么会返回这么大的对象,因为寄存器最多可以容纳32位(x86架构)。
谢谢!
返回值只放在足够小的数据类型的寄存器中。否则,它们将作为堆栈上的副本返回。或者有时可以省略副本。
相关文章:
- 使用基类指针创建对象时,缺少派生类析构函数
- 如何创建对象函数指针C++映射?
- C++创建对象数组
- 在 C++ 的 Switch Case 中创建对象后对对象调用方法
- 如何获取在 main() 函数中构造的类的创建对象?
- 基于文件中的条目创建对象
- 错误:创建对象后无法分配区域
- C++ 通过输入创建对象
- 堆还是堆栈用于创建对象?
- 使用 C++ 创建对象数组
- 使用unique_ptr创建对象
- C++递归地在类构造函数中创建对象
- 通过向构造函数其他对象引用页面来创建对象
- ReactiveX (rx) - 在对象上应用可观察对象,而不是在可观察对象中创建对象
- 如何在OSX上正确创建C++对象文件(.o)
- 编译器是否会创建vtable,而不考虑在c++中创建对象
- 创建对象并防止被破坏
- 在C++中,友元类可以从友元类创建对象吗
- 只在堆中创建C++对象
- 创建用户定义的复制构造函数时无法创建对象