Path: utzoo!attcan!uunet!cs.utexas.edu!news-server.csri.toronto.edu!utgpu!watserv1!watcgl!andrewt From: andrewt@watnow.waterloo.edu (Andrew Thomas) Newsgroups: comp.sys.amiga.tech Subject: Re: Manx-C and realloc() Message-ID: Date: 10 Apr 90 16:00:24 GMT References: <36252@dcatla.UUCP> <440@oregon.oacis.org> <3418@newton.physics.purdue.edu> <28705@cup.portal.com> Sender: daemon@watcgl.waterloo.edu Distribution: na Organization: University of Waterloo, Waterloo, Ontario, Canada Lines: 41 In-reply-to: Sullivan@cup.portal.com's message of 7 Apr 90 21:52:35 GMT In article <28705@cup.portal.com> Sullivan@cup.portal.com (sullivan - segall) writes: > >I am using Manx 3.6a, and had the need for realloc() the other > >day. Unfortunately the linker couldn't find it, and neither could I! > > #define realloc(x,s) (free(x),malloc(x,s)) Doesn't realloc also copy the contents of the original memory, so you would need something like: void* realloc (void* x, int oldsize, int size) { void* y; if (size == 0) return (NULL); y = (void*) malloc (size); if (y == NULL) return (NULL); if (oldsize > size) oldsize = size; bcopy (x, y, oldsize); free (x); return (y); } Realloc guarantees that the contents don't change from the old chunk of memory to the new one, up to the smaller of the two block sizes. Without knowing the format of the memory list header, you cannot find the size of the old chunk, so you have to send it to the homebrew realloc yourself. This is why realloc is supposed to be part of the standard memory management library. The above is UNIX code, not Amiga. I have never encountered a two-argument malloc command (shows how much Amiga programming I do). In order for the preceding #define to work, though, malloc (x,y) would have to be identical to realloc(x,y) except that it does not free x. This I doubt. Hope this helps. -- Andrew Thomas andrewt@watnow.waterloo.edu Systems Design Eng. University of Waterloo "If a million people do a stupid thing, it's still a stupid thing." - Opus