Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: pointers...

Reply
Thread Tools

Re: pointers...

 
 
Thomas Matthews
Guest
Posts: n/a
 
      06-25-2003
geo wrote:
> hi all
>
> say if i had :
>
> struct random_struct {
> .....
> .....
> .....
> }
>
> struct random_struct random;
>
> and then i had a function which used this structure. What is the best
> programming practice:
>
> void function(struct random_struct * r) {
> .......
> }
>
> function(&random);
>
> or:
>
> void function(struct random_struct r) {
> .......
> }
>
> function(random);
>
>
> and why?
>
> Thanks
> Ross


The first function is preferred. Structures may become very huge,
and moving them around costs a lot processor power (not to mention
space to hold all the temporary copies).

Passing a pointer to a structure is the preferred method. The
structure can remain in one place while pointers to it are passed
around. The idea is that a pointer takes up less space than a
structure. By keeping the structure in one place in memory,
the overhead of copying it is removed which lowers the execution
load on the processor (and reduces the memory requirements).

--
Thomas Matthews

C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
--
comp.lang.c.moderated - moderation address:
 
Reply With Quote
 
 
 
 
Simon Biber
Guest
Posts: n/a
 
      06-26-2003
"Thomas Matthews" <> wrote:
> The first function is preferred. Structures may become very
> huge, and moving them around costs a lot processor power (not
> to mention space to hold all the temporary copies).
>
> Passing a pointer to a structure is the preferred method. The
> structure can remain in one place while pointers to it are
> passed around. The idea is that a pointer takes up less space
> than a structure. By keeping the structure in one place in
> memory, the overhead of copying it is removed which lowers the
> execution load on the processor (and reduces the memory
> requirements).


If this is your reason for preferring the first function, then I
wholeheartedly disagree.

The only case where your argument applies is if the function
will not be making any changes to the struct. In that case,
neither of the functions is correct, as they should instead
use a pointer to const.

These two functions are only of use when you intend to modify
the struct within the function. In that case your argument is
completely invalid, as they have very different semantics --
the first will change the original in the caller, whereas the
second makes a copy of the struct so can be used as scratch
without affecting the caller's copy. The decision must be made
based on which one of these two behaviours is required.

--
Simon.
--
comp.lang.c.moderated - moderation address:
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments