memcpy vs. memmove

Karl Heuer karl at haddock.ima.isc.com
Thu Sep 15 02:21:12 AEST 1988


In article <2063 at cuuxb.ATT.COM> mmengel at cuuxb.ATT.COM (Marc Mengel) writes:
>To check whether the source and destination overlap, you have to compare two
>pointer values.  You can only do this legally ($3.3.8) if the two pointers
>point into the same array/aggregate.

Watch it -- the first `you' refers to the implementation; the second refers to
the user.  Certainly the implementation is free to generate any code that
happens to work.

>Hence you need memmove() for shifting items in an array, and memcpy() for
>moving items between arrays.  Using memmove() on items not in the same array
>is therefore not guaranteed to work, as the comparison of pointers not in the
>same array/aggregate is undefined.

No, memmove() is guaranteed to work on *any* valid pointers.  What you've
really proved is that memmove() cannot be portably implemented in C.

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint
Followups to comp.std.c.



More information about the Comp.std.c mailing list