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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  3. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  4. Subject: Re: strcpy implementation question
  5. Message-ID: <9224017.23144@mulga.cs.mu.OZ.AU>
  6. Sender: news@cs.mu.OZ.AU
  7. Organization: Computer Science, University of Melbourne, Australia
  8. References: <PINKAS.92Aug21114508@caraway.intel.com> <PINKAS.92Aug25163006@caraway.intel.com> <14213@goanna.cs.rmit.oz.au>
  9. Date: Thu, 27 Aug 1992 07:43:05 GMT
  10. Lines: 45
  11.  
  12. ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
  13.  
  14. >In article <PINKAS.92Aug25163006@caraway.intel.com>, pinkas@caraway.intel.com (Israel Pinkas) writes:
  15. >> The fact that the memory has not been initialized is irrelevant.  If the
  16. >> source didn't care about it, neither will the destination..
  17. >
  18. >This is wrong.  It isn' the source or the destination that cares, it is
  19. >strcpy() that cares.  strcpy() MUST NOT READ any uninitialised memory
  20. >locations.
  21.  
  22. I'm sorry, but this is wrong.
  23. The code to implement strcpy() does NOT have to be ansi-conformant - hell,
  24. it doesn't even have to be written in C! It IS allowed to read
  25. uninitialised memory locations, so long as that doesn't stop it doing its
  26. job correctly.
  27.  
  28. The following function correctly implements strcpy on my machine:
  29.  
  30.     #define MINDLESS_USE_OF_UNITIALIZED_MEMORY 
  31.  
  32.     char *strcpy(char *dest, const char *src) {
  33.         char *retval = dest;
  34.  
  35.     #ifdef MINDLESS_USE_OF_UNINITIALIZED_MEMORY
  36.         char unused[4];
  37.         unused[0] = (unused[1] *= (unused[2] += unused[3]));
  38.     #endif
  39.  
  40.         while (*dest++ = *src++) ;
  41.         return retval;
  42.     }
  43.  
  44. It might not work on *your* machine, but that's irrelevant.
  45.  
  46. If the compiler can determine that there referencing memory past
  47. the end of the string will not cause any side-effects, then it is perfectly
  48. entitled to do so.
  49. [Of course, strcpy is not allowed to write past the end of the destination
  50. string, because that *would* cause detectable side-effects.]
  51.  
  52. -- 
  53. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  54. This .signature virus is a self-referential statement that is true - but 
  55. you will only be able to consistently believe it if you copy it to your own
  56. .signature file!
  57.