问题描述
我是新的C,有一件事我无法理解。
当函数返回的东西是不是比寄存器大 - 我的编译器把它放在EAX。
当我回到大结构(没有指针,但结构本身) - 它是通过栈返回
我的问题是:编译器如何知道如何调用由另一个对象导出的功能?
有一个调用约定(如STDCALL),但它是关于传递参数,不读返回值吧?
有应该像一些规则,如果返回值声明为EAX比大,比把它从[BP -...]。
还有一:这将是正确地说,对象我想返回,比寄存器大应存放在堆和指针prevent都比堆栈操作恢复
感谢。
返回值传递给调用者的方式是函数调用约定的一部分。请参见。
例如,对于 CDECL
:
[...]
The stack manipulations are going to be much much faster than the heap manipulations necessary if you allocate memory on the heap, so stack is always faster. The only reason (in C) you might want to return a pointer to something on the heap is because it won't fit on the stack.
Clarification:
In the last sentence above, "the only reason you might want..." should not be interpreted as "there is normally no reason to return a pointer". Rather, I mean "if you can do what you need without returning a pointer, the only reason to decide to use a pointer anyway is...".
Of course there are many valid reasons to return pointers from functions as Chris states in his own answer, but I 'm only talking about the cases where you don't need to do so.
In other words, return by value when you can; use pointers when you must.
这篇关于C:通过堆栈/寄存器的问题返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!