K
Keith Thompson
88888 Dihedral said:Do you push so often in the C program stack for such trivial things?
Finally, something that might *vaguely* be responsive to what I
was asking. Are you saying you're concerned about the expense
of pushing values onto the stack to pass them to the function,
and then returning a struct value (presumably also on the stack)?
If you're interested in having an actual discussion, it would have
beeen helpful if you'd stated your rationale when you first made
the remark -- or at least after being asked.
The C standard says nothing about a "stack". It's true that the
vast majority of C implementations do use an in-memory contiguous
stack. But if the expense of the MakePair() function is such a
concern, you can always inline it (making it conditional on the
__STDC_VERSION__ if you're concerned about older implementations)
and ask your compiler to optimize it.
I don't think I'd bother unless actual measurements showed that it
was a significant problem.
And it's very common for parameters and function return values to
be passed in registers. In this case, the structure is just twice
the size of an int.
Do you often resort to macros for such trivial things?
Incidentally, in this particular case C99's compound literals make the
function largely unecessary -- though not all compilers support them.
The caller is responsible to allocate the structure either in the heap
or the program stack in my version of C programs.
Yes. So what is the advantage of pushing that responsibility onto
the caller? It makes the code more difficult to read and write,
for vanishingly little benefit.
[...]