Xref: utzoo comp.lang.c:29630 comp.sys.att:9816 Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uunet!isc-br!tau-ceti!cims2!proto!joe From: joe@proto.COM (Joe Huffman) Newsgroups: comp.lang.c,comp.sys.att Subject: Re: bcopy and bzero on AT&T sysV Summary: Posted memmove() doesn't work with large data model 80x86 cpu. Keywords: bcopy bzero AT&T Message-ID: <1307@proto.COM> Date: 16 Jun 90 14:51:18 GMT References: <213@cti1.UUCP> <1990Jun14.233837.22318@virtech.uucp> <16891@haddock.ima.isc.com> Organization: Prototronics; Sandpoint, Idaho Lines: 49 In article <16891@haddock.ima.isc.com>, karl@haddock.ima.isc.com (Karl Heuer) writes: > #include > /* > * 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@proto.com uunet!proto!joe FAX: 208-263-8772