假设Linux上的x86-64 ABI,在C++中的什么条件下结构传递给寄存器中的函数而不是堆栈中的函数?他们在什么条件下返回登记册?课程的答案是否会改变?
如果它有助于简化答案,则可以假设单个参数/返回值而没有浮点值.
我知道用C编码,函数的返回值使用%eax寄存器返回给调用者.
使用c ++,也可以返回结构而不仅仅是'Primitive'类型,所以当一个函数返回一个结构时,存储的返回值在哪里(堆栈,堆等)?
示例代码:
class Student
{
private:
int m_id;
public:
Student(int id)
{
m_id = id;
};
~Student();
int getId()
{
return m_id;
};
};
Student myFunc()
{
return Student(123);
}
int main()
{
//How does 'student1' get the value from the function?
//Does 'myFunc' write directly to the stack of main?
Student student1 = myFunc();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我想调用一个返回 a 的函数struct,这个函数有__cdecl调用约定。
调用约定如何__cdecl返回 a struct?
我是 C 新手,只是一个关于返回结构的问题。我听到有人说可以返回一个结构体。例如:
struct MyObj{
int x,y,z;
};
struct MyObj foo(){
struct MyObj foo_a;
foo_a.x = 10;
foo_a.y = 10;
foo_a.z = 10;
return foo_a;
}
int main () {
struct MyObj main_a = foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
foo_a是foo的堆栈,在经过这么foo结束,堆栈将unwinded,其手段foo_a实际上并不存在的main函数的栈指针main_a是main持有实际上是一个非法的指针,那么它是怎么样的工作?
assembly ×2
c ×2
c++ ×2
abi ×1
allocation ×1
nasm ×1
return-value ×1
struct ×1
x86 ×1
x86-64 ×1