By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,603 Members | 1,477 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,603 IT Pros & Developers. It's quick & easy.

Best Way To Copy Complex Structs

P: n/a
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));

Nov 10 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

Nov 10 '06 #2

P: n/a
aj
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.
Nov 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.