Memory Fault

Robert J. Drabek robert at cs.arizona.edu
Mon Jul 23 00:55:31 AEST 1990



> ... [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 at cs.Arizona.EDU
Department of Computer Science              uunet!arizona!robert
The University of Arizona                   602 621 4326
Tucson, AZ  85721



More information about the Comp.lang.c mailing list