Am Montag, 18. Juni 2007 23:48 schrieb Andy Lester:
> 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.

I'm using a lot of the first kind. The main reason is documenting the bulk 
copy operation (which is basically wrong - that's a docu thingy, if even).

Using 'sizeof(*dest)' is slightly better because the probability of 
overwriting too small dest areas is minimized - not the correctness of the 
code.

As Andy said: the best thing is using...

  *dest = *src;

... which will fail (IIRC) on all incompatible pointers. Compilers of course 
know about the size of the involved structure and can optimize the (possible 
generated) memcpy to unrolled item copies.

There are 2 addendums though:

a) as that construct seems not to be known widely I'd write it like:

  *dest = *src;   /* copy contents of structure FOO */

or some such.

b) sometimes inside parrot guts only parts of structures are copied to 
compatible pointers, which still will need the memcpy(3) call.

leo

Reply via email to