malloc+copy+free vs realloc (was Efficient coding)
Chris Torek
chris at mimsy.UUCP
Sat Oct 29 12:54:45 AEST 1988
In article <1730 at dataio.Data-IO.COM> bright at Data-IO.COM (Walter Bright) writes:
-I've seen a number of people say that the following:
- p = realloc(p,newsize);
- assert(p); /* fail if out of memory */
-is slower and/or less maintainable than:
- newp = malloc(newsize);
- assert(newp); /* fail if out of memory */
- memcpy(newp,p,(newsize < oldsize ? newsize : oldsize));
- free(p);
- oldsize = newsize;
- p = newp;
Obviously this is false. There is, however, one case where I do use
malloc+copy+free instead of realloc: If the data are important and
must not be destroyed by a failed realloc. The draft standard says
that this is already so; but I have my doubts about current implementations.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain: chris at mimsy.umd.edu Path: uunet!mimsy!chris
More information about the Comp.lang.c
mailing list