 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jujitsu Lizard Guest
|
Posted: Wed Dec 17, 2008 6:20 pm Post subject: Copying Aggregate Data Types in C |
|
|
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
|
Posted: Wed Dec 17, 2008 6:20 pm Post subject: Re: Copying Aggregate Data Types in C |
|
|
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 |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|