关于在C/C ++/Assembly中返回多个值 [英] About returning more than one value in C/C++/Assembly

查看:101
本文介绍了关于在C/C ++/Assembly中返回多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些有关返回多个值的问题,例如从C ++函数返回多个值解决方案

是的,有时会这样做.如果您在cdecl下的 x86调用约定上阅读了Wikipedia页面:

cdecl的解释中有一些变化,特别是在如何返回值方面.结果,即使它们都使用"cdecl"约定并且不调用底层环境,为不同的操作系统平台和/或由不同的编译器编译的x86程序也可能是不兼容的. 某些编译器在寄存器对EAX:EDX中返回长度小于2个寄存器的简单数据结构,以及需要异常处理程序(例如,定义的构造函数,析构函数)进行特殊处理的较大的结构和类对象,或赋值)返回到内存中.为了传递在内存中",调用者分配内存并传递一个指向它的指针作为隐藏的第一个参数.被调用者会填充内存并返回指针,返回时会弹出隐藏的指针.

(重点是我的)

最终,归结为调用约定.编译器可能会优化代码以使用所需的任何寄存器,但是当您的代码与其他代码(例如操作系统)交互时,它需要遵循标准的调用约定,该约定通常使用1个寄存器来返回值. /p>

I have read some questions about returning more than one value such as What is the reason behind having only one return value in C++ and Java?, Returning multiple values from a C++ function and Why do most programming languages only support returning a single value from a function?.

I agree with most of the arguments used to prove that more than one return value is not strictly necessary and I understand why such feature hasn't been implemented, but I still can't understand why can't we use multiple caller-saved registers such as ECX and EDX to return such values.

Wouldn't it be faster to use the registers instead of creating a Class/Struct to store those values or passing arguments by reference/pointers, both of which use memory to store them? If it is possible to do such thing, does any C/C++ compiler use this feature to speed up the code?

Edit:

An ideal code would be like this:

(int, int) getTwoValues(void) { return 1, 2; }

int main(int argc, char** argv)
{
    // a and b are actually returned in registers
    // so future operations with a and b are faster
    (int a, int b) = getTwoValues();
    // do something with a and b

    return 0;
}

解决方案

Yes, this is sometimes done. If you read the Wikipedia page on x86 calling conventions under cdecl:

There are some variations in the interpretation of cdecl, particularly in how to return values. As a result, x86 programs compiled for different operating system platforms and/or by different compilers can be incompatible, even if they both use the "cdecl" convention and do not call out to the underlying environment. Some compilers return simple data structures with a length of 2 registers or less in the register pair EAX:EDX, and larger structures and class objects requiring special treatment by the exception handler (e.g., a defined constructor, destructor, or assignment) are returned in memory. To pass "in memory", the caller allocates memory and passes a pointer to it as a hidden first parameter; the callee populates the memory and returns the pointer, popping the hidden pointer when returning.

(emphasis mine)

Ultimately, it comes down to calling convention. It's possible for your compiler to optimize your code to use whatever registers it wants, but when your code interacts with other code (like the operating system), it needs to follow the standard calling conventions, which typically uses 1 register for returning values.

这篇关于关于在C/C ++/Assembly中返回多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆