Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Best Way To Copy Complex Structs

Reply
Thread Tools

Best Way To Copy Complex Structs

 
 
mieskola
Guest
Posts: n/a
 
      11-10-2006
Given the following struct definition:

struct Data
{
int i;
ComplexType c;
};


do either of the following copying methods have a programmatic
advantage? Style?

Data d;
Data e;

d.i = 4;
..
..
..


METHOD 1: e = d;
METHOD 2: memcpy(&e,&d, sizeof(d));

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      11-10-2006
mieskola wrote:

> Given the following struct definition:
>
> struct Data
> {
> int i;
> ComplexType c;
> };
>
>
> do either of the following copying methods have a programmatic
> advantage? Style?
>
> Data d;
> Data e;
>
> d.i = 4;
> .
> .
> .
>
>
> METHOD 1: e = d;
> METHOD 2: memcpy(&e,&d, sizeof(d));


The second is only guaranteed to work if ComplexType is a POD. Method 1
works for any assignable type. It's also easier to read.

 
Reply With Quote
 
 
 
 
aj@bookac.com
Guest
Posts: n/a
 
      11-10-2006
Thanks Rolf. I much prefer method 1 myself.

AJ


Rolf Magnus wrote:
> mieskola wrote:
>
> > Given the following struct definition:
> >
> > struct Data
> > {
> > int i;
> > ComplexType c;
> > };
> >
> >
> > do either of the following copying methods have a programmatic
> > advantage? Style?
> >
> > Data d;
> > Data e;
> >
> > d.i = 4;
> > .
> > .
> > .
> >
> >
> > METHOD 1: e = d;
> > METHOD 2: memcpy(&e,&d, sizeof(d));

>
> The second is only guaranteed to work if ComplexType is a POD. Method 1
> works for any assignable type. It's also easier to read.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Packed structs vs. unpacked structs: what's the difference? Daniel Rudy C Programming 15 04-10-2006 08:10 AM
Copy constructor: why can't I copy objects as if they were structs? rdc02271 C++ 24 12-27-2005 12:38 PM
Array of structs instead of an array with pointers to structs? Paminu C Programming 5 10-11-2005 07:18 PM
const structs in other structs Chris Hauxwell C Programming 6 04-27-2004 07:03 PM
structs with fields that are structs Patricia Van Hise C Programming 5 04-05-2004 01:37 AM



Advertisments