Path: utzoo!utgpu!water!watmath!clyde!att!gargoyle!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: malloc+copy+free vs realloc (was Efficient coding) Message-ID: <14232@mimsy.UUCP> Date: 29 Oct 88 02:54:45 GMT References: <3105@hubcap.UUCP> <34112@XAIT.XEROX.COM> <1700@dataio.Data-IO.COM> <1730@dataio.Data-IO.COM> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 19 In article <1730@dataio.Data-IO.COM> bright@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@mimsy.umd.edu Path: uunet!mimsy!chris