home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
- From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
- Subject: Re: strcpy implementation question
- Message-ID: <9224017.23144@mulga.cs.mu.OZ.AU>
- Sender: news@cs.mu.OZ.AU
- Organization: Computer Science, University of Melbourne, Australia
- References: <PINKAS.92Aug21114508@caraway.intel.com> <PINKAS.92Aug25163006@caraway.intel.com> <14213@goanna.cs.rmit.oz.au>
- Date: Thu, 27 Aug 1992 07:43:05 GMT
- Lines: 45
-
- ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
-
- >In article <PINKAS.92Aug25163006@caraway.intel.com>, pinkas@caraway.intel.com (Israel Pinkas) writes:
- >> The fact that the memory has not been initialized is irrelevant. If the
- >> source didn't care about it, neither will the destination..
- >
- >This is wrong. It isn' the source or the destination that cares, it is
- >strcpy() that cares. strcpy() MUST NOT READ any uninitialised memory
- >locations.
-
- I'm sorry, but this is wrong.
- The code to implement strcpy() does NOT have to be ansi-conformant - hell,
- it doesn't even have to be written in C! It IS allowed to read
- uninitialised memory locations, so long as that doesn't stop it doing its
- job correctly.
-
- The following function correctly implements strcpy on my machine:
-
- #define MINDLESS_USE_OF_UNITIALIZED_MEMORY
-
- char *strcpy(char *dest, const char *src) {
- char *retval = dest;
-
- #ifdef MINDLESS_USE_OF_UNINITIALIZED_MEMORY
- char unused[4];
- unused[0] = (unused[1] *= (unused[2] += unused[3]));
- #endif
-
- while (*dest++ = *src++) ;
- return retval;
- }
-
- It might not work on *your* machine, but that's irrelevant.
-
- If the compiler can determine that there referencing memory past
- the end of the string will not cause any side-effects, then it is perfectly
- entitled to do so.
- [Of course, strcpy is not allowed to write past the end of the destination
- string, because that *would* cause detectable side-effects.]
-
- --
- Fergus Henderson fjh@munta.cs.mu.OZ.AU
- This .signature virus is a self-referential statement that is true - but
- you will only be able to consistently believe it if you copy it to your own
- .signature file!
-