home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:13259 comp.std.c:2573
- Newsgroups: comp.lang.c,comp.std.c
- Path: sparky!uunet!cs.utexas.edu!convex!news.oc.com!utacfd.uta.edu!rwsys!sneaky!gordon
- From: gordon@sneaky.lonestar.org (Gordon Burditt)
- Subject: Re: strcpy implementation question
- Message-ID: <Bu3DxM.I9D@sneaky.lonestar.org>
- Organization: Gordon Burditt
- References: <PINKAS.92Aug24183511@caraway.intel.com> <1992Aug26.224904.7671@sneaky.lonestar.org> <PINKAS.92Sep2173635@skywalker.intel.com>
- Date: Sat, 5 Sep 1992 06:27:05 GMT
- Lines: 54
-
- >The source argument (the second argument) to strcpy is declared as 'const
- >char *', which states that the routine will not modify the source string.
-
- No, it says it won't modify the source string *USING THAT POINTER*.
- It doesn't prevent it from getting at the source through global variables,
- destination pointers saved from previous calls, or just generally using
- system-dependent methods (the implementation of strcpy() doesn't have
- to be portable) to wipe the whole data segment.
-
- But I'd still consider any implementation of strcpy() that did these
- things non-conforming, even without a statement "thou shalt not mess up
- unrelated variables".
-
- >> A better example would be the infamous "variable-length struct".
- >> You declare something like:
- >> struct foo {
- >> int type;
- >> ... bunch of other stuff here ...
- >> char name[1];
- >> } *fp;
- >> char x[100];
- >> and then use it like:
- >> fp = (struct foo *) malloc(sizeof(struct foo) + strlen(x));
- >> strcpy(fp->name, x);
- >> "name" has supposedly known size. x has supposedly known size.
- >> The compiler probably aligned both of them. But if the copying stops
- >> at 1 char or 1 word when x contains a 70-char string, I'm going to be
- >> annoyed. And a lot of programs will break.
-
- >I don't know of a single compiler that guarantees to place two static
- >variables which are declared in the same file next to each other. In other
- >words,
-
- This example may be bogus, but not for this reason. It does NOT assume
- that two static variables are next to each other. (Which two? There
- are only 2 choices, the source string and a pointer. Neither is the
- destination. And the program couldn't care less about those variables
- being next to each other.) I didn't show the function header, but I
- actually didn't intend that there be any static variables in the entire
- program (global variables wouldn't have been indented - a style convention
- you can't depend on everyone using). The storage allocated by one call
- to malloc() is guaranteed to be contiguous (within itself). The struct is
- at the beginning of it. The space allocated by malloc() is guaranteed
- aligned properly. The space computation guarantees that there's enough
- storage for the string that runs off the end of the struct. If there is
- extra padding inserted in the structure, that just gives extra margin for
- the claim that it won't run off the end of allocated storage.
-
- Ok, what IS bogus about this example? (1) Running off the end of the
- name[] array, but I have assured that there IS enough storage following
- it to hold the data. Is that good enough? Any other complaints?
-
- Gordon L. Burditt
- sneaky.lonestar.org!gordon
-