home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!uunet.ca!wildcan!sq!msb
- From: msb@sq.sq.com (Mark Brader)
- Subject: Re: Memory functions - memcpy & memmove
- Message-ID: <1992Nov8.225440.6545@sq.sq.com>
- Organization: SoftQuad Inc., Toronto, Canada
- References: <BxArt0.9pE@murphy.com> <BxB88A.1Au@portal.hq.videocart.com> <1992Nov7.165047.9553@leland.Stanford.EDU>
- Date: Sun, 8 Nov 92 22:54:40 GMT
- Lines: 35
-
- In the second-last-referenced article, we have:
-
- > Write your own memmove (different name of course). ...
- > Let's say you call it moveit(char * dest, char *source, int size)
-
- [Part of it reads:]
-
- > for(size --; size >= 0; size --)
- > dest[size] = source[size];
-
- These parts of the code are correct as far as they go, but they do not
- provide a correct emulation of ANSI C's memmove(). The actual argument
- types of memmove are (void *, const void *, size_t). The variations
- in the first two arguments don't matter here, but the third one does.
-
- In ANSI C, size_t is guaranteed to be an unsigned type. (Note: this
- is not true on some pre-ANSI implementations that provide size_t!)
- moveit() should be able to move as many bytes as the implementation
- allows in an object, and its third argument should be either unsigned
- or unsigned long (if you don't have a size_t type that is one or the
- other, you'll have to guess which).
-
- Once you have made this change, the above loop is now unsafe; it fails
- badly if size==0 initially. The easy fix is to change the for-header to:
-
- while (size-- > 0) /* or != 0, according to taste */
-
- For another issue of concern, see the last-referenced article, another
- followup to the same base article, by Dave Eisen.
- --
- Mark Brader "It's simply a matter of style, and while there
- SoftQuad Inc., Toronto are many wrong styles, there really isn't any
- utzoo!sq!msb, msb@sq.com one right style." -- Ray Butterworth
-
- This article is in the public domain.
-