On 19 Jun 2007, at 08:48, Joshua Isom wrote:
On Jun 18, 2007, at 4:48 PM, Andy Lester wrote:
Is there a reason we use
memcpy( dest, src, sizeof(FOO) );
instead of
*dest = *src;
The latter should be the exact same code, but be much less likely
to be screwed up.
No, they're extremely different. In the first, the data of FOO is
copied to dest, so dest can be modified without changing src. In
the second, src and dest point to the same data. If you modify
one, all are modified.
Nonsense :)
If you want to clone something, or just move it to a new
location, you can't just set the pointer. If I'm missing
something, well maybe someone who knows more can provide more advice.
You're thinking of pointer assignment. Andy is assigning the pointed-
to structures. Consider with ints instead of structs
int *p, *q;
memcpy(p, q, sizeof(int)); /* copy int at q into int at p */
*p = *q; /* the same */
p = q; /* p and q point to same memory */
You're thinking of the third case - Andy is doing the second.
--
Andy Armstrong, hexten.net