copy a struct

Discussion in 'C Programming' started by Michael, Mar 11, 2005.

  1. Michael

    Michael Guest

    Hi all,

    I want to copy a structure, but the only thing
    I have is a pointer to it.

    Whats good here? Because the structure contains no pointers,
    but arrays and variables I thought of doing a memcpy:

    #include <stdio.h>
    #include <string.h>

    struct test {
    int a;
    int b[2];
    };

    int main (void)
    {
    struct test t1, t2;
    struct test *pt1;

    pt1 = &t1;
    t1.a = 1; t1.b[0] = 42; t1.b[1] = 173;

    memcpy(&t2, pt1, sizeof(t2) );
    printf ( "t1:a=%d, b_0=%d, b_1=%d\n", t1.a, t1.b[0], t1.b[1]);
    printf ( "t2:a=%d, b_0=%d, b_1=%d\n", t2.a, t2.b[0], t2.b[1]);

    return 0;
    }
    > gcc test.c -Wall
    > a.out

    t1:a=1, b_0=42, b_1=173
    t2:a=1, b_0=42, b_1=173

    It works, but I am a little confortable with this
    solution. Would it be better to use a function, which
    copy each variable?
    > gcc test.c -Wall
    > a.out

    t1:a=1, b_0=42, b_1=173
    t2:a=1, b_0=42, b_1=173


    Thanks in advance
    Michael
    Michael, Mar 11, 2005
    #1
    1. Advertising

  2. Michael

    -berlin.de Guest

    Michael <> wrote:
    > I want to copy a structure, but the only thing
    > I have is a pointer to it.


    > Whats good here? Because the structure contains no pointers,
    > but arrays and variables I thought of doing a memcpy:


    > #include <stdio.h>
    > #include <string.h>


    > struct test {
    > int a;
    > int b[2];
    > };


    > int main (void)
    > {
    > struct test t1, t2;
    > struct test *pt1;


    > pt1 = &t1;
    > t1.a = 1; t1.b[0] = 42; t1.b[1] = 173;


    > memcpy(&t2, pt1, sizeof(t2) );
    > printf ( "t1:a=%d, b_0=%d, b_1=%d\n", t1.a, t1.b[0], t1.b[1]);
    > printf ( "t2:a=%d, b_0=%d, b_1=%d\n", t2.a, t2.b[0], t2.b[1]);


    > return 0;
    > }
    > > gcc test.c -Wall
    > > a.out

    > t1:a=1, b_0=42, b_1=173
    > t2:a=1, b_0=42, b_1=173


    > It works, but I am a little confortable with this
    > solution. Would it be better to use a function, which
    > copy each variable?


    You're looking for too complicated a solution. All you need is

    t2 = *pt1;

    and no memcpy() or other function calls. Note that structures are
    "first class cititens" of C, you can copy them as a whole, pass
    them to functions by value or return them from functions etc.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Mar 11, 2005
    #2
    1. Advertising

  3. Michael

    bjrnove Guest

    Hi.

    memcpy is a good solution in this case since you wouldn't have to
    change anything to add something else into the struct. Another thing
    you can do is this:

    t2 = *pt1;

    Then it would be up to the compiler how to copy the items of the
    struct.

    --
    bjrnove
    bjrnove, Mar 11, 2005
    #3
  4. In article <d0s2e5$s7l$-bs.de>, Michael <> wrote:

    > Whats good here?


    A simple assignment?

    | #include <stdio.h>
    | #include <string.h>
    |
    | struct test {
    | int a;
    | int b[2];
    | };
    |
    | int main (void)
    | {
    | struct test t1, t2;
    struct test t3;
    | struct test *pt1;
    |
    | pt1 = &t1;
    | t1.a = 1; t1.b[0] = 42; t1.b[1] = 173;
    |
    | memcpy(&t2, pt1, sizeof(t2) );
    t3 = *pt1;
    | printf ( "t1:a=%d, b_0=%d, b_1=%d\n", t1.a, t1.b[0], t1.b[1]);
    | printf ( "t2:a=%d, b_0=%d, b_1=%d\n", t2.a, t2.b[0], t2.b[1]);
    printf ( "t3:a=%d, b_0=%d, b_1=%d\n", t3.a, t3.b[0], t3.b[1]);
    |
    | return 0;
    | }
    | > gcc test.c -Wall
    | > a.out
    | t1:a=1, b_0=42, b_1=173
    | t2:a=1, b_0=42, b_1=173
    t3:a=1, b_0=42, b_1=173

    > It works, but I am a little confortable with this
    > solution. Would it be better to use a function, which
    > copy each variable?


    Why are you trying to make it more complicated than it is?

    --
    Göran Larsson http://www.mitt-eget.com/
    Goran Larsson, Mar 11, 2005
    #4
  5. Michael

    pete Guest

    -berlin.de wrote:
    >
    > Michael <> wrote:
    > > I want to copy a structure, but the only thing
    > > I have is a pointer to it.

    >
    > > Whats good here? Because the structure contains no pointers,
    > > but arrays and variables I thought of doing a memcpy:

    >
    > > #include <stdio.h>
    > > #include <string.h>

    >
    > > struct test {
    > > int a;
    > > int b[2];
    > > };

    >
    > > int main (void)
    > > {
    > > struct test t1, t2;
    > > struct test *pt1;

    >
    > > pt1 = &t1;
    > > t1.a = 1; t1.b[0] = 42; t1.b[1] = 173;

    >
    > > memcpy(&t2, pt1, sizeof(t2) );
    > > printf ( "t1:a=%d, b_0=%d, b_1=%d\n", t1.a, t1.b[0], t1.b[1]);
    > > printf ( "t2:a=%d, b_0=%d, b_1=%d\n", t2.a, t2.b[0], t2.b[1]);

    >
    > > return 0;
    > > }
    > > > gcc test.c -Wall
    > > > a.out

    > > t1:a=1, b_0=42, b_1=173
    > > t2:a=1, b_0=42, b_1=173

    >
    > > It works, but I am a little confortable with this
    > > solution. Would it be better to use a function, which
    > > copy each variable?

    >
    > You're looking for too complicated a solution. All you need is
    >
    > t2 = *pt1;
    >
    > and no memcpy() or other function calls. Note that structures are
    > "first class cititens" of C, you can copy them as a whole, pass
    > them to functions by value or return them from functions etc.


    I would put structures a step below first class.
    You can't use any of the equality or relational operators
    with structures.

    --
    pete
    pete, Mar 11, 2005
    #5
  6. Michael

    Michael Guest

    Re: copy a struct , fin

    > Why are you trying to make it more complicated than it is?
    I don't know, I get it thanks!

    Thanks to all of you!
    Michael
    Michael, Mar 11, 2005
    #6
  7. Michael wrote on 11/03/05 :
    > I want to copy a structure, but the only thing
    > I have is a pointer to it.


    As long as the type is known and complete, you don't need more.


    f (T *p)
    {
    T s = *p;
    }

    end of the story.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "Clearly your code does not meet the original spec."
    "You are sentenced to 30 lashes with a wet noodle."
    -- Jerry Coffin in a.l.c.c++
    Emmanuel Delahaye, Mar 11, 2005
    #7
  8. Michael

    Malcolm Guest

    <-berlin.de> wrote
    >
    > You're looking for too complicated a solution. All you need is
    >
    > t2 = *pt1;
    >
    > and no memcpy() or other function calls. Note that structures are
    > "first class cititens" of C, you can copy them as a whole, pass
    > them to functions by value or return them from functions etc.
    >

    This is on my hit list of features that ought to be removed from the
    language. It breaks the rule that no C statement may compile to more than a
    few assembly instructions.
    Malcolm, Mar 11, 2005
    #8
  9. Michael

    Alan Balmer Guest

    On Fri, 11 Mar 2005 22:29:08 -0000, "Malcolm"
    <> wrote:

    >
    ><-berlin.de> wrote
    >>
    >> You're looking for too complicated a solution. All you need is
    >>
    >> t2 = *pt1;
    >>
    >> and no memcpy() or other function calls. Note that structures are
    >> "first class cititens" of C, you can copy them as a whole, pass
    >> them to functions by value or return them from functions etc.
    >>

    >This is on my hit list of features that ought to be removed from the
    >language. It breaks the rule that no C statement may compile to more than a
    >few assembly instructions.
    >

    That rule would seem to be heavily dependent on the machine's
    instruction set ;-)

    On many machines, this would be loading a source pointer, a
    destination pointer, a count, and one instruction.

    --
    Al Balmer
    Balmer Consulting
    Alan Balmer, Mar 11, 2005
    #9
  10. Michael

    Malcolm Guest

    "Alan Balmer" <> wrote
    > >This is on my hit list of features that ought to be removed from the
    > >language. It breaks the rule that no C statement may compile to more
    > >than a few assembly instructions.
    > >

    > That rule would seem to be heavily dependent on the machine's
    > instruction set ;-)
    >

    The other place the rule breaks down is where the compiler writer decides to
    implement reasonable precision floating-point on a small processor with no
    FPU. There's no easy way round this, since if you try to use functions
    called things like flt_div() code rapidly becomes unreadable.

    However you don't want a seemingly innocuous instruction like "x = y"
    actually accounting for a narrow bottleneck in your code. When you gobble
    lots of processor power, you wan't a warning.
    >
    > On many machines, this would be loading a source pointer, a
    > destination pointer, a count, and one instruction.
    >

    What we are really interested in is the execution speed, not the number of
    instructions.
    It wouldn't be too difficult to build a massively parallel memcpy(), but I
    don't think any are in common use. What you do have sometimes is the looping
    logic implemented on chip, which speeds things up.

    A lot of machines also have DMA engines, which can copy long structures in
    parallel with main program flow. So you need to call DMcopy(), do a bit of
    logic, and then retrieve your results. This is of course really awkward to
    program, and only useful for huge structures.
    Malcolm, Mar 13, 2005
    #10
    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. RA Scheltema
    Replies:
    3
    Views:
    380
    RA Scheltema
    Jan 6, 2004
  2. Gunnar G

    struct in struct

    Gunnar G, May 31, 2004, in forum: C++
    Replies:
    14
    Views:
    787
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,356
    Chris Fogelklou
    Apr 20, 2004
  4. Alex
    Replies:
    2
    Views:
    1,203
  5. Replies:
    26
    Views:
    2,091
    Roland Pibinger
    Sep 1, 2006
Loading...

Share This Page