Re: pointers...

Discussion in 'C Programming' started by Thomas Matthews, Jun 25, 2003.

  1. 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:
     
    Thomas Matthews, Jun 25, 2003
    #1
    1. Advertising

  2. Thomas Matthews

    Simon Biber Guest

    "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:
     
    Simon Biber, Jun 26, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil
    Replies:
    1
    Views:
    661
    llewelly
    Sep 16, 2003
  2. muser
    Replies:
    3
    Views:
    775
    Ron Natalie
    Sep 18, 2003
  3. A
    Replies:
    3
    Views:
    466
    Alan Kelon
    Oct 29, 2003
  4. Xamalek
    Replies:
    7
    Views:
    697
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    695
Loading...

Share This Page