问题
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