home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!newsgate.watson.ibm.com!yktnews!admin!curt
- From: curt@watson.ibm.com (Curt McDowell)
- Subject: Re: strcpy implementation question
- Sender: news@watson.ibm.com (NNTP News Poster)
- Message-ID: <1992Aug27.153441.29151@watson.ibm.com>
- Date: Thu, 27 Aug 1992 15:34:41 GMT
- Lines: 63
- Disclaimer: This posting represents the poster's views, not necessarily those of IBM
- References: <PINKAS.92Aug21114508@caraway.intel.com> <PINKAS.92Aug25163006@caraway.intel.com> <14213@goanna.cs.rmit.oz.au> <9224017.23144@mulga.cs.mu.OZ.AU>
- Nntp-Posting-Host: gorby.watson.ibm.com
- Organization: IBM T.J. Watson Research Center
-
- In article <9224017.23144@mulga.cs.mu.OZ.AU>, fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
- > 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.
-
- Well, I wouldn't state it that way. I'd say it must not read anything
- outside of its own stack, global variables in the C library, or other memory
- it legally allocates.
-
- > 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.
-
- It better work on your machine. "unused" is legally allocated local stack
- space, so of course you can reference it. Since the initial contents are
- undefined, the result unused[0] is undefined. Fine.
-
- > 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.
-
- No compiler can determine this for the general strcpy().
-
- A good, portable strcpy MAY NOT access extra bytes before or after the
- source string, just the exact bytes of the string. That's because the
- memory outside the string may be illegal address space, particularly
- where virtual memory or hardware parity checking is concerned, and you'd
- trap trying to read it.
-
- You laugh. Then, you write an optimized assembly memcpy() for a real
- C library. Then.... you stop laughing :-|
-
- > Fergus Henderson fjh@munta.cs.mu.OZ.AU
-
- --
- Curt McDowell
- IBM T. J. Watson Research Center
-