Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Passing structs, pointers to structs or lots of variables?

0 views
Skip to first unread message

John Smith

unread,
Nov 11, 2002, 1:09:40 PM11/11/02
to
I have a function which has lots and lots of parameters (twelve to be
exact). It uses these paramaters to create a system descriptor string.
I
was told to change the function to only use a single parameter -- a
pointer
to a structure with all of the data, because this is more efficient.
This
seems a bit counter intuitive to me, as every time you access a member
of
this structure pointer, you need to resolve a pointer, which I would
imagine
would be more expensive than putting the data on the stack.

So my question is, what are the real advantages/dissadvantages of the
following (style comments are welcome as well): Passing multiple
parameters, passing a structure, or passing a pointer to a structure?


John


[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]

Francis Glassborow

unread,
Nov 12, 2002, 11:46:09 AM11/12/02
to
In message <aqomaf$m0k$1...@news.storm.ca>, John Smith
<som...@microsoft.com> writes

>I have a function which has lots and lots of parameters (twelve to be
>exact). It uses these paramaters to create a system descriptor string.
>I
>was told to change the function to only use a single parameter -- a
>pointer
>to a structure with all of the data, because this is more efficient.

Definition of efficiency?

>This
>seems a bit counter intuitive to me, as every time you access a member
>of
>this structure pointer, you need to resolve a pointer, which I would
>imagine
>would be more expensive than putting the data on the stack.

Quite possibly not.


>
>So my question is, what are the real advantages/dissadvantages of the
>following (style comments are welcome as well): Passing multiple
>parameters,

Means that the programmer has to remember the order in which the
arguments must be provided. It also means that any possible default
values can only by provided for parameters at the tail end (well
overloading can help with this problem)

Anytime you have more than half a dozen parameters you have a memory
burden for the user that suggests investigating alternatives.

> passing a structure,
Requires copying, which with that number of parameters may well not be
done into registers. So you pay for accessing a struct and for copying
it.


> or passing a pointer to a structure?

(or reference)
Now you can reset the arguments you want to change (in C++ you can even
provide a ctor that fills in default values for all arguments). You pay
for dereferencing a pointer but the cost will vary from zero to
insignificant depending on the quality of optimisation and the
facilities of the underlying hardware.
In fact I can imagine circumstances where this mechanism could result in

measurable gains as the whole of the struct could be held in cache where

access would be very fast.

--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

rishi

unread,
Nov 12, 2002, 5:51:24 PM11/12/02
to
Hi
it dependes really on the cost of copying the twelve objects the stack
verses
dereferencing the pointer to access data. If your objects are heavy
then its possible that copying them on stack may be bad but if they
are lightweight
objects with no data members, no virtual functions or no ctors then
passing them
by value might be okay and sometimes advisable..
-rishi
"John Smith" <som...@microsoft.com> wrote in message
news:<aqomaf$m0k$1...@news.storm.ca>...

> I have a function which has lots and lots of parameters (twelve to be
> exact). It uses these paramaters to create a system descriptor
string.
> I
> was told to change the function to only use a single parameter -- a
> pointer
> to a structure with all of the data, because this is more efficient.
> This
> seems a bit counter intuitive to me, as every time you access a member
> of
> this structure pointer, you need to resolve a pointer, which I would
> imagine
> would be more expensive than putting the data on the stack.
>
> So my question is, what are the real advantages/dissadvantages of the
> following (style comments are welcome as well): Passing multiple
> parameters, passing a structure, or passing a pointer to a structure?

[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]

Raoul Gough

unread,
Nov 12, 2002, 7:33:01 PM11/12/02
to
"John Smith" <som...@microsoft.com> wrote in message
news:aqomaf$m0k$1...@news.storm.ca...
> I have a function which has lots and lots of parameters (twelve to
be
> exact). It uses these paramaters to create a system descriptor
string.
> I
> was told to change the function to only use a single parameter -- a
> pointer
> to a structure with all of the data, because this is more efficient.
> This
> seems a bit counter intuitive to me, as every time you access a
member
> of
> this structure pointer, you need to resolve a pointer, which I would
> imagine
> would be more expensive than putting the data on the stack.

At the machine code level, the differences will probably be quite
minor. Whether the stuff is in a structure or as separate parameters
on the stack, you're still storing and retrieving values at various
offsets from a base pointer (pointer to the structure or pointer to
the stack frame). At worst, this would use up an extra machine
register in the called function. You also have the extra overhead of
storing a pointer to the structure on the stack, which you wouldn't
need with raw parameters. If you are really optimizing the code to
that kind of level (why?), the only way to know how much difference it
makes is to measure it. You'll probably find that there is no
measurable difference.

>
> So my question is, what are the real advantages/dissadvantages of
the
> following (style comments are welcome as well): Passing multiple
> parameters, passing a structure, or passing a pointer to a
structure?

If you're passing a struct parameter, reference to const is probably
the best default choice. I guess a struct does have an advantage in
terms of clarity compared to multiple parameters, since the code can
show meaningful names for the sub-parts:

info s;
s.some_name = 3;
s.extra_something = "xyz";
s.more_stuff = 8.0;
f (s);

instead of f(3, "xyz", 8.0);

On the other hand, twelve parameters is quite a lot for one function,
however you cut it. Maybe you can break them into functional groups
and wrap each group in a class with a meaningful name:

f (machine_type("bogo", 8000)
, os_version ("bogos", 3.2)
, keyboard_type ("uk", 102));

Or some such (trying to imagine what the twelve parameters could be).
Ideally, I suppose the twelve-parameter function would itself become a
member function of some suitable class, and get most of its inputs as
member variables.

Regards,
Raoul Gough.

Raoul Gough

unread,
Nov 12, 2002, 7:33:21 PM11/12/02
to
"John Smith" <som...@microsoft.com> wrote in message
news:aqomaf$m0k$1...@news.storm.ca...
> I have a function which has lots and lots of parameters (twelve to
be
> exact). It uses these paramaters to create a system descriptor
string.
> I
> was told to change the function to only use a single parameter -- a
> pointer
> to a structure with all of the data, because this is more efficient.
> This
> seems a bit counter intuitive to me, as every time you access a
member
> of
> this structure pointer, you need to resolve a pointer, which I would
> imagine
> would be more expensive than putting the data on the stack.

At the machine code level, the differences will probably be quite


minor. Whether the stuff is in a structure or as separate parameters
on the stack, you're still storing and retrieving values at various
offsets from a base pointer (pointer to the structure or pointer to
the stack frame). At worst, this would use up an extra machine
register in the called function. You also have the extra overhead of
storing a pointer to the structure on the stack, which you wouldn't
need with raw parameters. If you are really optimizing the code to
that kind of level (why?), the only way to know how much difference it
makes is to measure it. You'll probably find that there is no
measurable difference.

>


> So my question is, what are the real advantages/dissadvantages of
the
> following (style comments are welcome as well): Passing multiple
> parameters, passing a structure, or passing a pointer to a
structure?

If you're passing a struct parameter, reference to const is probably


the best default choice. I guess a struct does have an advantage in
terms of clarity compared to multiple parameters, since the code can
show meaningful names for the sub-parts:

info s;
s.some_name = 3;
s.extra_something = "xyz";
s.more_stuff = 8.0;
f (s);

instead of f(3, "xyz", 8.0);

On the other hand, twelve parameters is quite a lot for one function,
however you cut it. Maybe you can break them into functional groups
and wrap each group in a class with a meaningful name:

f (machine_type("bogo", 8000)
, os_version ("bogos", 3.2)
, keyboard_type ("uk", 102));

Or some such (trying to imagine what the twelve parameters could be).
Ideally, I suppose the twelve-parameter function would itself become a
member function of some suitable class, and get most of its inputs as

member variables. It really depends how many places need to use the
function as to whether it's worth going to the trouble.

Regards,
Raoul Gough.

Catalin Pitis

unread,
Nov 13, 2002, 6:25:42 PM11/13/02
to
In article <aqomaf$m0k$1...@news.storm.ca>, som...@microsoft.com says...

> I have a function which has lots and lots of parameters (twelve to be
> exact). It uses these paramaters to create a system descriptor string.
> I
> was told to change the function to only use a single parameter -- a
> pointer
> to a structure with all of the data, because this is more efficient.

I agree. Instead of copying 12 parameters on the stack, only a reference
is copied (4 bytes on most systems).

> This
> seems a bit counter intuitive to me, as every time you access a member
> of
> this structure pointer, you need to resolve a pointer, which I would
> imagine
> would be more expensive than putting the data on the stack.

I'm not convinced that accessing a structure member is more expensive.

>
> So my question is, what are the real advantages/dissadvantages of the
> following (style comments are welcome as well): Passing multiple
> parameters, passing a structure, or passing a pointer to a structure?
>
>

When dealing with a lot of parameters, I think is better to pass the
parameters as a structure (a reference to the structure, not pointer):

void f( const MyStructure& params) { ... }

Of course, there can be some additional code to write: initializing the
structure.

I think that the decision of wether using a structure or parameters
depend on the context the function will be used: the number of calls vs.
the number of structure initialisations.

In a context like the following (multiple calls using the same
structure):

MyStructure s;
// initialisation of s
f( s);
// some code
f( s);

it is better to use the structure.

But, if you need to initialize the structure before every function call,
passing the structure as a parameter doesn't present an advantage.

Catalin Pitis

0 new messages