Difference between returning result and assigning result through pointer argument?

半腔热情 提交于 2019-12-25 07:29:12

问题


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.

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?


回答1:


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).

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.



来源:https://stackoverflow.com/questions/18137520/difference-between-returning-result-and-assigning-result-through-pointer-argumen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!