Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!ames!ucbcad!ucbvax!decvax!tektronix!sequent!mntgfx!gssc!geoffs From: geoffs@gssc.UUCP (Geoff Shapiro) Newsgroups: comp.sys.ibm.pc Subject: Re: MSC 4.0 and the Twilight Zone Message-ID: <405@gssc.UUCP> Date: Wed, 6-May-87 11:46:56 EDT Article-I.D.: gssc.405 Posted: Wed May 6 11:46:56 1987 Date-Received: Sun, 10-May-87 02:09:18 EDT References: <2454@uw-june.UUCP> <2088@hoptoad.uucp> Reply-To: geoffs@gssc.UUCP (Geoff Shapiro) Organization: Graphic Software Systems, Beaverton Or Lines: 36 In article <2088@hoptoad.uucp> farren@hoptoad.UUCP (Mike Farren) writes: >In article <2454@uw-june.UUCP> roper@uw-june.UUCP (Michael Roper) writes: >>Can anyone please explain the behavior of the following few lines of code? >[which doesn't work] >> >> char *s; >> >> s = strdup (argv[1]); >> strcat (s, ".U"); >> do_nothing (s); > >Note that you are defining s (and, later, do_nothing_str) as a pointer to >char, but you are never creating an array that the pointers will point to. >As a result, the strcat and strdup routines, which expect that the pointers >they receive WILL point to such arrays, go ahead and blindly copy the strings >they have received as arguments to the place that the pointers point to, >which is the address represented by whatever happened to be in the pointers >when the routine was entered.... Mike Faren's explanation would be a sufficient cause of the problem if the explanation were correct, but I believe that Mike Faren has been led a little bit astray in his understanding of what strdup expects. Strdup takes a pointer to an existing string and returns a pointer to a duplicate of the passed in string; it internally calls malloc to allocate enough memory to hold the cloned string. What I do see as a possible problem, is that strdup probably only allocates enough bytes to hold a string of the length of the passed-in string. Therefore, when Mike Roper uses strcat to append extra characters onto the end of the dup'ed string, he may be overwriting memory not belonging to the string! Your guess is as good as mine as to what effect this has. Geoffs Graphic Software Systems Beaverton, Or. (503) 641-2200