C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Copying Aggregate Data Types in C

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language
View previous topic :: View next topic  
Author Message
Jujitsu Lizard
Guest





PostPosted: Wed Dec 17, 2008 6:20 pm    Post subject: Copying Aggregate Data Types in C Reply with quote



In the general case, if s1 and s2 are structures, am I allowed to just use:

s1 = s2;

?

I'm assuming the compiler will puke if that isn't legal.

And is that better or worse than:

memcpy(&s1, &s2, sizeof(s1));

?

Any guaranteed behavior by the compiler there?

For small structs I'm going to guess the compiler would just copy them a
word at a time. But for larger structs, I'd guess the compiler is going to
perhaps do a memcpy() anyway.

Thanks for all insight.

The Lizard.
Back to top
Guest






PostPosted: Wed Dec 17, 2008 6:20 pm    Post subject: Re: Copying Aggregate Data Types in C Reply with quote



On Dec 17, 8:09 pm, "Jujitsu Lizard" <jujitsu.liz...@gmail.com> wrote:
Quote:
In the general case, if s1 and s2 are structures, am I allowed to just use:

s1 = s2;

?

Yes. What "general case"? s1 and s2 must be of the same type.

Quote:
I'm assuming the compiler will puke if that isn't legal.

And is that better or worse than:

memcpy(&s1, &s2, sizeof(s1));

?

It's not better or worse, it's different. This copies padding bytes
too, you can also use it as long as sizeof s2 >= sizeof s1, so unlike
the former, the types do not matter.

Quote:
Any guaranteed behavior by the compiler there?

For small structs I'm going to guess the compiler would just copy them a
word at a time. But for larger structs, I'd guess the compiler is going to
perhaps do a memcpy() anyway.

The compiler can do whatever it want. Do not guess - take a look at a
particular implementation if you're curious enough.
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group