Reputation: 47978
When defining structs in C, there are considerations regarding padding, if struct size is a concern, its common to re-arrange the values to avoid padding. (see: Structure padding and packing)
My questions is:
Do the same (or similar) rules apply to function arguments? ... is there any advantage in arranging arguments to avoid argument padding bytes?
Assuming this isn't an inline
(where it wont matter), or static
function where the compiler could re-arrange arguments.
Accepting that the real world measurable improvement is likely to be small.
... in practice if function call overhead is a concern, it may be worth inlining the function. nevertheless, inlining isnt always an option (libraries or function pointers for eg).
Upvotes: 13
Views: 1742
Reputation: 7374
If the argument is small enough to be passed in a register, then the size is immaterial.
Generally the answer is no because compilers often widen arguments when they are passed on the stack to a function. For example:
So it doesn't pay to group your one and two byte arguments together because either the argument will be passed in a register or the compiler will probably treat them as being four or eight bytes in length anyway.
Upvotes: 4
Reputation: 241681
How arguments are passed to a function varies from architecture to architecture, so it's not possible to provide any sort of definite answer. However, for most modern architectures, the first few parameters are passed in registers, not on the stack, and it matters little how the parameters are aligned, because narrow arguments are not multiplexed into a single register.
Upvotes: 3