A
Antriksh Pany
Is there a notion of 'maximum number of function arguments'? As in, is
there such a limit at all by the standard (probably not) or specific
compilers/platforms? Or just the stack size shall determine such a
limit?
Secondly, if a function is required to receive multiple parameters,
what are the pros and cons of using the parameters individually vs
having a container structure for them? Let us assume in this case that
the container structure has to be created only for the sake for
passing parameters to the function, and not otherwise.
Some benefits of using the individual parameters, as they occur to me,
are:
1. No need to have an explicit wrapper structure, which otherwise is
not useful at all.
2. The code to copy parameters to structure members now needn't exist.
3. The first few (usually left to right) parameters (usually two or
three) can be directly copied to and accessed from registers if
needed. This is typically used for most fastcall calling conventions,
and maybe even otherwise.
Benefits of using a wrapper structure:
1. Just the structure pointer needs to be passed, thus possibly saving
on some stack space (although if the original structure was on the
stack anyway (likely), there may not be any savings).
2. The function definition _looks_ neat.
3. Each variable passed is going to be independently aligned on
whatever is the alignment boundary (usually 32 bit), as opposed to a
possibly more efficient memory usage within a structure. But I think
this may not be a cause for concern in most cases.
Overall, I am leaning towards passing individual members as is. I do
not know how correct I am in the assumptions I make above, or if I
have missed something important. Please do make
suggestions/corrections.
- Antriksh
P.S. In case this is already discussed, I apologize. I did some search
but did not find a discussion on the matter. Please do provide the
relevant link(s).
there such a limit at all by the standard (probably not) or specific
compilers/platforms? Or just the stack size shall determine such a
limit?
Secondly, if a function is required to receive multiple parameters,
what are the pros and cons of using the parameters individually vs
having a container structure for them? Let us assume in this case that
the container structure has to be created only for the sake for
passing parameters to the function, and not otherwise.
Some benefits of using the individual parameters, as they occur to me,
are:
1. No need to have an explicit wrapper structure, which otherwise is
not useful at all.
2. The code to copy parameters to structure members now needn't exist.
3. The first few (usually left to right) parameters (usually two or
three) can be directly copied to and accessed from registers if
needed. This is typically used for most fastcall calling conventions,
and maybe even otherwise.
Benefits of using a wrapper structure:
1. Just the structure pointer needs to be passed, thus possibly saving
on some stack space (although if the original structure was on the
stack anyway (likely), there may not be any savings).
2. The function definition _looks_ neat.
3. Each variable passed is going to be independently aligned on
whatever is the alignment boundary (usually 32 bit), as opposed to a
possibly more efficient memory usage within a structure. But I think
this may not be a cause for concern in most cases.
Overall, I am leaning towards passing individual members as is. I do
not know how correct I am in the assumptions I make above, or if I
have missed something important. Please do make
suggestions/corrections.
- Antriksh
P.S. In case this is already discussed, I apologize. I did some search
but did not find a discussion on the matter. Please do provide the
relevant link(s).