Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 10th, 2006, 08:35 PM
mieskola
Guest
 
Posts: n/a
Default Best Way To Copy Complex Structs

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));

  #2  
Old November 10th, 2006, 09:05 PM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: Best Way To Copy Complex Structs

mieskola wrote:
Quote:
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.

  #3  
Old November 10th, 2006, 09:15 PM
aj@bookac.com
Guest
 
Posts: n/a
Default Re: Best Way To Copy Complex Structs

Thanks Rolf. I much prefer method 1 myself.

AJ


Rolf Magnus wrote:
Quote:
mieskola wrote:
>
Quote:
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.
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles