passing copy of a pointer to a variable vs passing the copy

Discussion in 'C Programming' started by pereges, May 30, 2008.

  1. pereges

    pereges Guest

    which one do you think is better ? I need to make my program efficient
    and in some places I have passed the copy of a variable which makes
    life some what easy while writing huge expressions but they do requrie
    much more memory than a simple pointer.
     
    pereges, May 30, 2008
    #1
    1. Advertising

  2. On May 30, 9:20 am, pereges <> wrote:
    > which one do you think is better ? I need to make my program efficient
    > and in some places I have passed the copy of a variable which makes
    > life some what easy while writing huge expressions but they do requrie
    > much more memory than a simple pointer.


    Can you give an example? What sort of variable?

    -David
     
    David Resnick, May 30, 2008
    #2
    1. Advertising

  3. pereges <> wrote:
    > which one do you think is better ? I need to make my program efficient
    > and in some places I have passed the copy of a variable


    In C you can only pass copies of variables to functions.

    > which makes life some what easy while writing huge expressions


    I what respect?

    > but they do requrie much more memory than a simple pointer.


    Which variables take much more memory than a simple pointer?
    Most variables you can pass to afunction are at least of
    comparable size to the one of a pointer. The only exception
    is structures, which can definitely be quite a lot larger than
    pointers, and that's why they often get passed via a pointer
    instead of by value, despite C allowing it.

    But I guess I am misunderstanding your intents. Could you
    post some code that illustrates what yiu are doing?

    And, of course, the usual warning: don't optimize prematurely
    but instead measure where the bottle-necks are in your program.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, May 30, 2008
    #3
  4. pereges

    pereges Guest

    On May 30, 7:14 pm, (Jens Thoms Toerring) wrote:

    > Which variables take much more memory than a simple pointer?
    > Most variables you can pass to afunction are at least of
    > comparable size to the one of a pointer. The only exception
    > is structures, which can definitely be quite a lot larger than
    > pointers, and that's why they often get passed via a pointer
    > instead of by value, despite C allowing it.
    >
    > But I guess I am misunderstanding your intents. Could you
    > post some code that illustrates what yiu are doing?
    >
    > And, of course, the usual warning: don't optimize prematurely
    > but instead measure where the bottle-necks are in your program.
    >


    Yes, I'm talking about a structure. For example I want to store a mesh
    containing close to 200,000 vertices and 400,000 triangles. Here's my
    data structure:

    typedef struct vector_struct
    {
    double x, y, z;
    }vector;

    typedef vector vertex;

    typedef struct triangle_struct
    {
    int v[3];
    vector normal;
    }triangle;

    /* ======== Data structure for the mesh ========== */

    typedef struct object_struct
    {
    vector *vertexlist;
    triangle *trianglelist;
    unsigned long int nvert;
    unsigned long int ntri;
    }object;
     
    pereges, May 30, 2008
    #4
  5. pereges

    pereges Guest

    On May 30, 7:23 pm, pereges <> wrote:
    > Yes, I'm talking about a structure. For example I want to store a mesh
    > containing close to 200,000 vertices and 400,000 triangles. Here's my
    > data structure:
    >
    > typedef struct vector_struct
    > {
    > double x, y, z;
    >
    > }vector;
    >
    > typedef vector vertex;
    >
    > typedef struct triangle_struct
    > {
    > int v[3];
    > vector normal;
    >
    > }triangle;
    >
    > /* ======== Data structure for the mesh ========== */
    >
    > typedef struct object_struct
    > {
    > vector *vertexlist;
    > triangle *trianglelist;
    > unsigned long int nvert;
    > unsigned long int ntri;
    >
    > }object;


    In this mesh structure, vertexlist and triangle list are pointers to
    the array of vertices and triangles respectively.
     
    pereges, May 30, 2008
    #5
  6. pereges <> wrote:
    > On May 30, 7:23 pm, pereges <> wrote:
    > > Yes, I'm talking about a structure. For example I want to store a mesh
    > > containing close to 200,000 vertices and 400,000 triangles. Here's my
    > > data structure:
    > >
    > > typedef struct vector_struct
    > > {
    > > double x, y, z;
    > >
    > > }vector;
    > >
    > > typedef vector vertex;
    > >
    > > typedef struct triangle_struct
    > > {
    > > int v[3];
    > > vector normal;
    > >
    > > }triangle;
    > >
    > > /* ======== Data structure for the mesh ========== */
    > >
    > > typedef struct object_struct
    > > {
    > > vector *vertexlist;
    > > triangle *trianglelist;
    > > unsigned long int nvert;
    > > unsigned long int ntri;
    > >
    > > }object;


    > In this mesh structure, vertexlist and triangle list are pointers to
    > the array of vertices and triangles respectively.


    If you pass a pointer to the structure to a function you safe
    a bit of work in copying it and a bit of memory since no copy
    needs to be made (but not that much for such a rather small
    structure). The main difference is that you can easily change
    all the elements in the original structure without passing it
    also back to the caller since you are working on the original
    itself and not a copy (although you can prevent that by quali-
    fying the pointer as 'const' inthe functions argument list).

    I don't see why dealing with a pointer instead of a copy of
    the structure would be more difficult, you just have to use
    a '->' between the pointer and the elements name you want
    to access instead of a '.' between the structures name and
    the element.

    In the overwhelming majority of programs I have seen pointers
    to structures get passed to fucntions instead of the structures
    by value, so it's a very common idiom and most people will un-
    derstand it quite fine. And, yes it might be a bit more effi-
    cient speed- and memory-wise, but I wouldn't expect a doubling
    of the speed of your application (or anything near that) just
    from using pointers;-)
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, May 30, 2008
    #6
  7. pereges <> writes:
    > On May 30, 7:14 pm, (Jens Thoms Toerring) wrote:
    >> Which variables take much more memory than a simple pointer?
    >> Most variables you can pass to afunction are at least of
    >> comparable size to the one of a pointer. The only exception
    >> is structures, which can definitely be quite a lot larger than
    >> pointers, and that's why they often get passed via a pointer
    >> instead of by value, despite C allowing it.
    >>
    >> But I guess I am misunderstanding your intents. Could you
    >> post some code that illustrates what yiu are doing?
    >>
    >> And, of course, the usual warning: don't optimize prematurely
    >> but instead measure where the bottle-necks are in your program.
    >>

    >
    > Yes, I'm talking about a structure. For example I want to store a mesh
    > containing close to 200,000 vertices and 400,000 triangles. Here's my
    > data structure:
    >
    > typedef struct vector_struct
    > {
    > double x, y, z;
    > }vector;
    >
    > typedef vector vertex;
    >
    > typedef struct triangle_struct
    > {
    > int v[3];
    > vector normal;
    > }triangle;
    >
    > /* ======== Data structure for the mesh ========== */
    >
    > typedef struct object_struct
    > {
    > vector *vertexlist;
    > triangle *trianglelist;
    > unsigned long int nvert;
    > unsigned long int ntri;
    > }object;


    On one implementation I tried, your type "object" has a size of 16
    bytes; that's probably typical. On most systems, it's probably going
    to be about 4 times the size of a pointer.

    Passing the structure itself will obviously impose some overhead
    because you're passing more information.

    On the other hand, it might save some code space because, within the
    function, you save a pointer dereference every time you access a
    member of the structure. Or, depending on how member access is
    implemented by your compiler, it might not help at all.

    Measure it.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 30, 2008
    #7
  8. pereges

    pereges Guest

    On May 30, 8:35 pm, (Jens Thoms Toerring) wrote:
    > If you pass a pointer to the structure to a function you safe
    > a bit of work in copying it and a bit of memory since no copy
    > needs to be made (but not that much for such a rather small
    > structure). The main difference is that you can easily change
    > all the elements in the original structure without passing it
    > also back to the caller since you are working on the original
    > itself and not a copy (although you can prevent that by quali-
    > fying the pointer as 'const' inthe functions argument list).
    >
    > I don't see why dealing with a pointer instead of a copy of
    > the structure would be more difficult, you just have to use
    > a '->' between the pointer and the elements name you want
    > to access instead of a '.' between the structures name and
    > the element.
    >
    > In the overwhelming majority of programs I have seen pointers
    > to structures get passed to fucntions instead of the structures
    > by value, so it's a very common idiom and most people will un-
    > derstand it quite fine. And, yes it might be a bit more effi-
    > cient speed- and memory-wise, but I wouldn't expect a doubling
    > of the speed of your application (or anything near that) just
    > from using pointers;-)


    Well those are not the only structures that my ray tracing program is
    using. There is also a structure for BSP tree(binary space
    partitioning) which takes up a lot of memory. The list of rays also
    consume a lot of memory. My program works very well for smaller number
    of rays < 400 K or 500 K but I have seen it fails beyond that. I was
    thinking fo storing the the list of rays in a file rather than as an
    array in the memory. Probably that could help. I haven't used unions
    either.
     
    pereges, Jun 1, 2008
    #8
    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. Replies:
    10
    Views:
    717
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    852
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,286
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    644
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    464
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page