home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!fjh
- From: fjh@cs.mu.OZ.AU (Fergus Henderson)
- Subject: Re: strcpy implementation question
- Message-ID: <9224103.3422@mulga.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> <9224017.23144@mulga.cs.mu.OZ.AU> <1992Aug27.153441.29151@watson.ibm.com>
- Date: Thu, 27 Aug 1992 17:45:03 GMT
- Lines: 68
-
- curt@watson.ibm.com (Curt McDowell) writes:
-
- >> 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.
-
- Well, NOT fine, if you were trying to write strictly conformant ANSI
- C code. An ANSI C implementation is well within its rights to dump core
- if you reference uninitialised memory - the idea is that some machines may
- have a special bit-pattern to represent uninitialised memory, and referencing
- this causes a trap.
-
- >> 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().
-
- Yes it could!
-
- >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.
-
- As I said above, even simply accessing uninitialized memory may cause a trap.
-
- On the other hand, if you are NOT concerned with writing a portable strcpy,
- and you (or the compiler) know that accessing memory after the string will
- not causes a trap (or any other similar problem) then you are quite
- entitled to access that memory. There are machines that have neither
- virtual memory nor hardware parity checking, and for which READING memory
- NEVER causes any 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!
- --
- 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!
-