Path: utzoo!attcan!uunet!mailrus!ncar!noao!arizona!robert From: robert@cs.arizona.edu (Robert J. Drabek) Newsgroups: comp.lang.c Subject: Re: Memory Fault Summary: answer Keywords: help Message-ID: <23377@megaron.cs.arizona.edu> Date: 22 Jul 90 14:55:31 GMT References: <189@S5000.UUCP> Organization: U of Arizona CS Dept, Tucson Lines: 38 > ... [Jay A. K.] he wrote the simped editor The code is really bogus. Sorry. > * CODE NOT TESTED, HOWEVER IT SHOULD WORK FINE FOR simped. We've heard that before. > int stringinx; This is not needed; see below. > if (buffer = (char *) malloc(strlen(string)) == NULL) if ((buffer = (char *) malloc(strlen(string) + 1)) == NULL) ^ ^^^ ^ 1 222 1 1. Missing set of parenthesis; without them buffer is (normally) being set to `1'. 2. The `+ 1' must be there since you need space for an end-of-string marker. > for (stringinx=0; stringinx <= strlen(string); ++stringinx) > buffer[stringinx] = string[stringinx]; Use `strcpy(buffer, string);' instead. The code you (or Jay) gave is Order n squared, i.e., very inefficient. > return(buffer); ^ ^ Just write `return buffer;', no parenthesis necessary. (Just a style point; no further comments solicited.) For a final remark, the last two lines could actually be collapsed as `return strcpy(buffer, string);' -- Robert J. Drabek robert@cs.Arizona.EDU Department of Computer Science uunet!arizona!robert The University of Arizona 602 621 4326 Tucson, AZ 85721