How to safely copy a struct

Discussion in 'C++' started by Angus, Mar 13, 2008.

  1. Angus

    Angus Guest

    Hello

    I am working with a struct (provided by an API I need to use) which
    has int arrays, char* and char buffers. I have been told there may be
    a problem because in previous code struct was copied using = and a
    deep copy is required.

    Do I have to copy each item in struct individually?

    eg

    strcpy(sttarget.szString, stsource.szString);
    sttarget.intitem = stsource.intitem;

    etc

    Or is there some other effective way?

    Angus
    Angus, Mar 13, 2008
    #1
    1. Advertising

  2. Angus

    Eric Pruneau Guest

    "Angus" <> a écrit dans le message de news:
    ...
    > Hello
    >
    > I am working with a struct (provided by an API I need to use) which
    > has int arrays, char* and char buffers. I have been told there may be
    > a problem because in previous code struct was copied using = and a
    > deep copy is required.
    >
    > Do I have to copy each item in struct individually?


    Yes

    >
    > eg
    >
    > strcpy(sttarget.szString, stsource.szString);
    > sttarget.intitem = stsource.intitem;
    >
    > etc
    >
    > Or is there some other effective way?


    I dont think so.
    Eric Pruneau, Mar 13, 2008
    #2
    1. Advertising

  3. On 2008-03-13 19:10, Angus wrote:
    > Hello
    >
    > I am working with a struct (provided by an API I need to use) which
    > has int arrays, char* and char buffers. I have been told there may be
    > a problem because in previous code struct was copied using = and a
    > deep copy is required.
    >
    > Do I have to copy each item in struct individually?
    >
    > eg
    >
    > strcpy(sttarget.szString, stsource.szString);
    > sttarget.intitem = stsource.intitem;
    >
    > etc
    >
    > Or is there some other effective way?


    You might be able to create a copy-constructor for the struct and
    perform all the member-copying in it.

    --
    Erik Wikström
    Erik Wikström, Mar 13, 2008
    #3
  4. Angus wrote:
    > Hello
    >
    > I am working with a struct (provided by an API I need to use) which
    > has int arrays, char* and char buffers. I have been told there may be
    > a problem because in previous code struct was copied using = and a
    > deep copy is required.
    >
    > Do I have to copy each item in struct individually?


    It kind of depends on how they're used.

    >
    > eg
    >
    > strcpy(sttarget.szString, stsource.szString);


    strcpy does not do what you think it does (if szString is a char *).
    Some implementations have a (non-standard) "strdup" function.

    > sttarget.intitem = stsource.intitem;
    >
    > etc
    >
    > Or is there some other effective way?


    You could do a struct assignment and follow up by fixing the deeper
    elements.

    If you used types like std::string, then there would be no need to do
    the deep copy as the assignment operator does the copy of the internal
    buffers for you.
    Gianni Mariani, Mar 14, 2008
    #4
  5. Angus

    Angus Guest

    On 14 Mar, 15:00, Gianni Mariani <> wrote:
    > Angus wrote:
    > > Hello

    >
    > > I am working with a struct (provided by an API I need to use) which
    > > has int arrays, char* and char buffers.  I have been told there may be
    > > a problem because in previous code struct was copied using =  and a
    > > deep copy is required.

    >
    > > Do I have to copy each item in struct individually?

    >
    > It kind of depends on how they're used.
    >
    >
    >
    > > eg

    >
    > > strcpy(sttarget.szString, stsource.szString);

    >
    > strcpy does not do what you think it does (if szString is a char *).
    > Some implementations have a (non-standard) "strdup" function.
    >
    > > sttarget.intitem = stsource.intitem;

    >
    > > etc

    >
    > > Or is there some other effective way?

    >
    > You could do a struct assignment and follow up by fixing the deeper
    > elements.
    >
    > If you used types like std::string, then there would be no need to do
    > the deep copy as the assignment operator does the copy of the internal
    > buffers for you.


    What is wrong with just doing this:

    ev is of type RXEvent* - as is m_evt.

    RXEvent is a union.

    m_evt = new RXEvent[sizeof(*ev)];
    memcpy(static_cast<RXEvent*>(m_evt), ev, sizeof(*ev));

    and delete'ing of course when done.

    It seems to work? Any downsides to this approach? It saves copying
    each member individually (there are a lot of them).
    Angus, Mar 14, 2008
    #5
  6. Angus

    Kai-Uwe Bux Guest

    Angus wrote:

    > On 14 Mar, 15:00, Gianni Mariani <> wrote:
    >> Angus wrote:
    >> > Hello

    >>
    >> > I am working with a struct (provided by an API I need to use) which
    >> > has int arrays, char* and char buffers.  I have been told there may be
    >> > a problem because in previous code struct was copied using =  and a
    >> > deep copy is required.

    >>
    >> > Do I have to copy each item in struct individually?

    >>
    >> It kind of depends on how they're used.
    >>
    >>
    >>
    >> > eg

    >>
    >> > strcpy(sttarget.szString, stsource.szString);

    >>
    >> strcpy does not do what you think it does (if szString is a char *).
    >> Some implementations have a (non-standard) "strdup" function.
    >>
    >> > sttarget.intitem = stsource.intitem;

    >>
    >> > etc

    >>
    >> > Or is there some other effective way?

    >>
    >> You could do a struct assignment and follow up by fixing the deeper
    >> elements.
    >>
    >> If you used types like std::string, then there would be no need to do
    >> the deep copy as the assignment operator does the copy of the internal
    >> buffers for you.

    >
    > What is wrong with just doing this:
    >
    > ev is of type RXEvent* - as is m_evt.
    >
    > RXEvent is a union.
    >
    > m_evt = new RXEvent[sizeof(*ev)];


    Huh? This allocates an array of RXEvent of lenght sizeof(RXEvent). Later,
    you only use the first entry. Why do you waste so much space?


    > memcpy(static_cast<RXEvent*>(m_evt), ev, sizeof(*ev));
    >
    > and delete'ing of course when done.
    >
    > It seems to work? Any downsides to this approach? It saves copying
    > each member individually (there are a lot of them).


    For pointer members (e.g., char*), it will not do a deep copy.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Mar 14, 2008
    #6
    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. benn
    Replies:
    2
    Views:
    595
  2. DDK
    Replies:
    2
    Views:
    700
    Rick Spiewak
    May 26, 2004
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,373
    Chris Fogelklou
    Apr 20, 2004
  4. Alex
    Replies:
    2
    Views:
    1,223
  5. Replies:
    26
    Views:
    2,113
    Roland Pibinger
    Sep 1, 2006
Loading...

Share This Page