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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!stanford.edu!leland.Stanford.EDU!dkeisen
  3. From: dkeisen@leland.Stanford.EDU (Dave Eisen)
  4. Subject: Re: Memory functions  -  memcpy & memmove
  5. Message-ID: <1992Nov7.165047.9553@leland.Stanford.EDU>
  6. Sender: news@leland.Stanford.EDU (Mr News)
  7. Organization: Sequoia Peripherals, Inc.
  8. References: <BxArt0.9pE@murphy.com> <BxB88A.1Au@portal.hq.videocart.com>
  9. Date: Sat, 7 Nov 92 16:50:47 GMT
  10. Lines: 39
  11.  
  12. In article <BxB88A.1Au@portal.hq.videocart.com> dfuller@portal.hq.videocart.com (Dave Fuller) writes something like this about memmove:
  13.  
  14.    if (source > dest) {
  15.       for(;size > 0; size --)
  16.          *dest++ = *source++;
  17.    } else {
  18.       for(size --; size >= 0; size --)
  19.          dest[size] = source[size];
  20.    }
  21.  
  22. I'd recommend the same thing with the caveat that this is not ANSI
  23. standard code and will not work on some systems. In fact, there is
  24. no reasonable way to write memmove in standard C; this is why the
  25. standard added this function.
  26.  
  27. The problem with the above code is that it is not legal to compare
  28. two pointers (except for equality) unless the pointers point into
  29. the same object. Such comparisons give undefined behavior which
  30. means it can do anything at all it wants.
  31.  
  32. That being said, I wouldn't worry too much about this problem. On
  33. any reasonable system (except one that is specifically designed
  34. to hunt down non-portabilities in code) this should work. All that
  35. is required is that the computer always return *some* answer when 
  36. comparing two pointers, that it not core dump or explode or something.
  37.  
  38. Because if the two pointers point into the same object the above
  39. code works. That is, after all, what it was designed to do. And if
  40. the two pointers don't point into the same object, then there is
  41. no problem with overlapping regions and you can copy either way
  42. you want to. Either way, you're covered.
  43.  
  44.  
  45.  
  46. -- 
  47. Dave Eisen                               Sequoia Peripherals: (415) 967-5644
  48. dkeisen@leland.Stanford.EDU              Home:                (415) 321-5154
  49.        There's something in my library to offend everybody. 
  50.           --- Washington Coalition Against Censorship
  51.