Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!think.com!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!mcsun!hp4nl!charon!guido From: guido@cwi.nl (Guido van Rossum) Newsgroups: comp.sys.mac.programmer Subject: Re: THINK C's malloc(),free() help please Message-ID: <3278@charon.cwi.nl> Date: 7 Apr 91 14:01:43 GMT References: <1991Apr6.203939.8742@umiami.ir.miami.edu> Sender: news@cwi.nl Lines: 30 (Question about what malloc and free do.) Don't worry, THINK C's malloc() and free() work fine. Freed memory is reused as you expect. The source is provided, but not much use since most of it is in assembly language, presumably for speed -- which they managed quite well in my opinion; in THINK 3.0 I had to use my own malloc() to get decent speed in a malloc-intensive application, but in 4.0 the library version was sufficient. What I understand from the assembly code is that it is a fairly thin layer on top of the ROM's NewPtr/DisposPtr interface, using zones somehow to keep many small blocks close together. The amazing thing is that free() is an almost direct interface to DisposPtr -- either its own administration looks the same as the ROM's, or it doesn't have an administration of its own, zones notwithstanding. The only bug that I see is that you can't realloc blocks to more than 15000 bytes (yes, this has bitten me a few times). You describe that you malloc() some blocks, the free() them, then malloc() some more, and expect to see the same addresses being reused, but instead see higher addresses being generated. This could either be a bug in your program (pointers are longer than ints, so you can't store pointers in ints or print them with %d or %x, and you *must* declare malloc; #include is the best way to do this), or a result from the strategy used by malloc when allocating small blocks (I can imaging that freed blocks go to the end of a rather long free list, while new blocks are allocated out of its beginning). --Guido van Rossum, CWI, Amsterdam "What a senseless waste of human life"