How to safely copy a struct

A

Angus

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
 
E

Eric Pruneau

Angus said:
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.
 
E

Erik Wikström

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.
 
G

Gianni Mariani

Angus said:
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.
 
A

Angus

It kind of depends on how they're used.





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


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).
 
K

Kai-Uwe Bux

Angus said:
It kind of depends on how they're used.





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


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top