bcopy and bzero on AT&T sysV

Joe Huffman joe at proto.COM
Sun Jun 17 00:51:18 AEST 1990


In article <16891 at haddock.ima.isc.com>, karl at haddock.ima.isc.com (Karl Heuer) 
writes:
> #include <string.h>
> /*
>  * Same as memcpy, but with guaranteed semantics on overlap.  This algorithm
>  * assumes trichotomy is valid even when the operands don't point into the
>  * same array.
>  */
> void *memmove(void *adest, void *asrc, size_t n) {
>     if (n != 0) {
>         register char *dest = (char *)adest;
>         register char *src = (char *)asrc;
>         if (src > dest) {
>             do *dest++ = *src++; while (--n != 0);
>         } else if (src < dest) {
>             src += n;
>             dest += n;
>             do *--dest = *--src; while (--n != 0);
>         }
>     }
>     return (adest);
> }

The comparisions 'if(src > dest)' and 'if(src < dest)' will only compare
the offset with most (all?) C compilers for the 80x86.  If you are moving
memory contents between different segments then it is quite possible to 
have both conditions fail.  This would result in no memory being moved 
with the algorithm as written.

Also, when in real mode two pointers can point to the same memory location
with different segment values.  Hence 'if(src > dest)' could test true 
even the absolute value of src were less than dest.  

The way to write the function would be to normalized the pointers before
doing the comparision, then just do one comparision.  That is, don't do 
'else if' just do 'else'.  Unless you are concerned about being passed
pointers to the same exact memory locations in which case you should do
'else if(src != dest)'.  The '==' and '!=' comparisions do full 
comparisions on the pointers, not just the offset portion.  You still
must do the normalization however.

This comparision of offset only for '>' and '<' 'feature' has bit me 
(and many others) more times than I care to admit.  And it results in
some extremely subtle bugs that disappear and reappear at odd times.

-- 
joe at proto.com
uunet!proto!joe
FAX: 208-263-8772



More information about the Comp.sys.att mailing list