Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!usc!apple!portal!cup.portal.com!Sullivan From: Sullivan@cup.portal.com (sullivan - segall) Newsgroups: comp.sys.amiga.tech Subject: Re: Manx-C and realloc() Message-ID: <28808@cup.portal.com> Date: 12 Apr 90 02:15:30 GMT References: <36252@dcatla.UUCP> <440@oregon.oacis.org><3418@newton.physics.purdue.edu> <28705@cup.portal.com> Distribution: na Organization: The Portal System (TM) Lines: 53 >In article <28705@cup.portal.com> Sullivan@cup.portal.com (sullivan - segall) writ e >s: >> >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. > Yeah, I noticed the problem with my post about 10 minutes after sending it, but since Portal has no way of unsending a message I was stuck. In general it is ok to memcpy the new size. (rather than the smaller of the two.) The only case where this would fail is when the original is just on the edge of a physical memory barrier. Otherwise the memcpy will succeed and just put a bit of garbage at the end of your new allocation. Really the pointer X shouldn't be used after it is freed either. I really meant that post as a quick kludge. Someone else posted the library source code for MANX malloc, ad free. -Sullivan Segall _________________________________________________________________ /V\ Sullivan was the first to learn how to jump without moving. ' Is it not proper that the student should surpass the teacher? To Quote the immortal Socrates: "I drank what?" -Sullivan _________________________________________________________________ Mail to: ...sun!portal!cup.portal.com!Sullivan or Sullivan@cup.portal.com