home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16181 < prev    next >
Encoding:
Text File  |  1992-11-08  |  1.9 KB  |  46 lines

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