返回结果并通过指针参数分配结果之间的区别? [英] Difference between returning result and assigning result through pointer argument?

查看:157
本文介绍了返回结果并通过指针参数分配结果之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个天真的编程语言,有效地为C code发电机,以及简化,我要重新present所有功能与C中没有返回类型,与返回类型为传递第一个参数,如果有的话。

I am developing a naive programming language, effectively a C code generator, and for simplification, I want to represent all functions as with no return type in C, with the return type being passed as the first parameter, if any.

我的问题是,这是否可以present兼容性和性能问题,我真的不知道究竟是什么区别,但通用逻辑表明,没有太多从函数指派返回值的差别寄信人地址VS人工手动操作的,地址是一个参数。但也许有一些额外的工作正在做?也许不同的优化一命呜呼?

My question is whether this could present a compatibility or performance issue, I am not really sure what exactly is the difference, but common logic suggests that there is not much of a difference from the function assigning the returned value to the return address vs doing that manually and the address is a parameter. But maybe there is some additional work being done? Maybe different optimizations kick in?

推荐答案

有关你可能是对的,没有什么区别复杂的返回类型。

For complex return types you're probably right, that there is no difference.

对于简单的返回类型,编译器将返回的结果在寄存器中,例如为

For simple return types, the compiler will return the result in a register, e.g. for

int foo(...);

但如果你传递一个指针导致存储,是这样的:

but if you pass a pointer to result storage, like this:

void foo (int* result, ...);

它不能(至少在一般情况下)。

it can't (at least not in the general case).

不过,我建议以后不用担心优化和忽视可能带来的性能影响。如果你不是一个经验丰富的语言设计者,你就会有其他的问题不用担心。后来,依赖于你的设计,你就会有其他的机会来优化。在例如:如果你生成所有的C code成一个文件,你可以让C编译器做积极的内联 - 这AFAICS将消除我们只是一直在谈论演出]区别

But let me suggest to worry about optimization later and ignore the possible performance impact. If you're not an experienced language designer, you'll have other problems to worry about. And later, dependent on you're design, you'll have other opportunities to optimization. In example: If you generate all C code into one file, you could let the c compiler do aggressive inlining - which AFAICS will remove the perfomance difference we just have been talking about.

这篇关于返回结果并通过指针参数分配结果之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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