home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:12680 comp.std.c:2493
- Path: sparky!uunet!elroy.jpl.nasa.gov!ames!olivea!mintaka.lcs.mit.edu!spdcc!iecc!johnl
- From: johnl@iecc.cambridge.ma.us (John R. Levine)
- Newsgroups: comp.lang.c,comp.std.c
- Subject: Re: strcpy implementation question
- Message-ID: <1992Aug23.194919.22007@iecc.cambridge.ma.us>
- Date: 23 Aug 92 19:49:19 GMT
- References: <PINKAS.92Aug21114508@caraway.intel.com> <1992Aug23.003930.9918@saaf.se>
- Organization: I.E.C.C.
- Lines: 45
-
- In article <1992Aug23.003930.9918@saaf.se> pausch@saaf.se (Paul Schlyter) writes:
- >>[I said the C standard says strcpy() has to copy exactly the string
- >>including the terminating null character]
- >>But it doesn't say "(including the terminating null character and any other
- >>garbage convenient for the compiler implementer)". You have to stop at the
- >>null byte.
- >
- >Even on a machine with word addressing, where each string is very likely to
- >occupy an integral number of machine words?
-
- C defines a string as a sequence of "char" items with the last one
- containing a zero. There has to be some way to address individual chars
- in an implementation or it's hard to see on what basis it claims to be C.
- For example, the string "ineptitude" is 11 chars long including the null
- character, not 12, 15 or some other convenient multiple. Consider the
- following code:
-
- static char buf[6];
-
- strcpy(buf+3, "cd");
- strcpy(buf, "ab");
-
- If buf doesn't end up containing "ab\0cd\0" I'd say that something was
- seriously wrong.
-
- The original question asked whether when using strcpy() to copy a string
- from one static array to another it would be an OK optimization to copy
- the whole array as full words in an unwound loop rather than looking for
- the null byte. Temporarily disregarding the total wrongheadedness of the
- idea, there are two other reasons not to copy words:
-
- 1. If the programmer wanted to copy the whole buffer, he would have used
- memcpy(), since that's what it's for.
-
- 2. It is my impression that the most common case where source and target
- array length are both known is when the source operand is a literal
- string. In that case the compiler knows the exact length of the actual
- string and can generate its unwound code, copying exactly the right number
- of characters at the end. This gets you most of the performance gain of
- the proposed optimization while still being correct.
-
- --
- John R. Levine, IECC, POB 349, Cambridge MA 02238, +1 617 492 3869
- johnl@iecc.cambridge.ma.us, {ima|spdcc|world}!iecc!johnl
- Re-elect Vice President Potatoe-Head !
-