On 11/26/2011 4:18 AM, Bogdan wrote:
>[... function linkage mechanisms ...]
>
> I guess that it depends on the output variable size. When we need to
> return a structure I guess that the variable stack should be always
> used.
On one compiler I've used, struct-valued functions were handled
rather differently. The function was rewritten like a void function
with an extra, invisible argument pointing to a temporary struct that
would receive the value. That is, you'd write
struct foo func(int bar) {
struct foo result;
...
return result;
}
struct foo baz = func(42);
.... and the compiler would rewrite to something like
void func(struct foo* _output, int bar) {
struct foo result;
...
*_output = result;
}
struct foo _temp;
struct foo baz;
func(&_temp, 42);
baz = _temp;
(Why use _temp instead of just func(&baz, 42)? Because the
compiler had to guard against the possibility that func() might
use baz directly -- as a global, say, or via another pointer.)
The lesson here is that words like "always" are risky in this
connection. Platforms differ, and present implementors with
differing challenges, and those challenges are met with different
strategies. This is probably why the Standard says so very little
about such matters: To give the implementors maximum freedom to
exploit the strengths and avoid the weaknesses of the platforms.
--
Eric Sosman
d