Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!rex!ames!ads.com!adiseker From: adiseker@ADS.COM (Andrew Diseker) Newsgroups: comp.sys.mac.programmer Subject: Re: malloc() in Think C programming on the Mac Message-ID: Date: 27 Jan 91 23:18:15 GMT References: <1991Jan24.060336.12684@nlm.nih.gov> <4978@awdprime.UUCP> Sender: usenet@ads.com (USENET News) Distribution: comp Organization: Advanced Decision Systems, Mtn. View, CA (415) 960-7300 Lines: 47 In article <4978@awdprime.UUCP> eswu@ub.cc.umich.edu writes: > >[In article 12426, werner@bio.NLM.NIH.GOV (Craig Werner) > asks why he gets NULL when he calls malloc() in THINK C.] > >The problem lies in the THINK C libraries. In their >implementation of malloc, they have an if-test to see >if you are allocating more than 16K of memory. If so, >it just returns NULL with no check of available system >resources. [Pretty arbitrary. Also, this restriction is >not mentioned in their docs. Fortunately, they supply >source code to their libraries.] > > I'm sure Rich Siegel will be sure to correct my correction 8), but here's the scoop. The above paragraph is not entirely true. The (re,m,c)alloc() code in ThinkC 4.0 is implemented such that requested allocations of more than 15000 bytes are handled by NewPtr(). Blocks less than 15000 are "allocated" from a linked list of NewPtr'd blocks of 15000 bytes. If there is a "free" space in a block of more than the requested size, the offset to the space is added to the block's ptr, and the result is returned. The problem with the scheme is that "garbage" is not collected. The free() call doesn't actually free space, such that 15000byte blocks that have no "allocated" spaces in them are never DisposPtr'd. Eventually, your application will run out of memory, if it runs long enough. I found a bug in free() anyway that doesn't seem to mark the "freed" memory in the block, so that blocks don't even get re-used often enough. I went back to the 3.0 version of alloc.c, and made the mods to the calling sequences etc. that made them ANSI compliant, ( at least ThinkC 4.0 compliant 8^). This version calls NewPtr and DisposPtr for every (*)alloc and free, thus it's slower, but more robust. E-mail me for a copy, if you'd like one. >-- Eng-Shien Wu, "My views only, does not represent IBM's" > > X Windows, IBM Advanced Workstations Div, Austin, Texas > Internet: eswu@ub.cc.umich.edu VNET: ausvmq(engshien) -- Andrew Diseker ( )-( ) Mouse-Tested! Advanced Decision Systems /o o\ Hacker-Approved! UUCP: sun!sundc!potomac!adiseker / =v= \ ,--_ Internet: adiseker@potomac.ads.com ;;---;; `--'