Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!cornell!batcomputer!braner
From: braner@batcomputer.tn.cornell.edu (braner)
Newsgroups: comp.sys.atari.st
Subject: Re: malloc bugs (s.b. Malloc bugs)
Summary: Malloc vs malloc: use the latter
Message-ID: <5919@batcomputer.tn.cornell.edu>
Date: 12 Aug 88 20:12:01 GMT
References: <3308@druhi.ATT.COM> <1104@atari.UUCP> <364@bdt.UUCP> <399@clio.math.lsa.umich.edu> <961@lakesys.UUCP>
Reply-To: braner@tcgould.tn.cornell.edu (braner)
Organization: Cornell Theory Center, Cornell University, Ithaca NY
Lines: 31

[]

Use malloc() (lowercase m), the library function that came with your C
compiler.  Assuming you got a decent compiler (Laser, Megamax, MWC, etc)
(I don't know about Alcyon) that will keep you out of trouble.
Make sure you use free() for what was malloc()ed, not Free().

The only case where you need Malloc() (uppercase M, the GEMDOG function)
is when you absolutely need to allocate more than 64K at once, since
malloc() takes an unsigned int as its argument, and an int on the 68000
is (or should be) 16 bits.  (Some compilers, like Lattice, use 32 bit
ints on the 68000, with a major execution speed penalty.)  The Malloc()
GEMDOG function takes a long argument.  Usually you can allocate large
things in smaller pieces (e.g. a large matrix row by row, initializing
a vector of pointers to rows as you go, and later accessing the matrix
elements by double indirection -- which is faster than 2-d matrix
access anyway!).  Use Free(), not free(), for freeing up what was Malloc()ed.

Actually Malloc() is not completely broken: you can use it a few times.
Just don't call it more than a few (say 20) times in a row, and try not
to call Free().  If you have to call Free(), then make sure you
are Free()-ing things in reverse order of Malloc()-ing them.
Malloc() was designed for the OS to allocate memory to programs
that are loaded, possibly in a chain of Pexec()s.  It was never
designed for allocating a zillion little bits and pieces and freeing 
them in arbitrary order.  It is too SLOW for those kind of applications
anyway.  That's where you need the _library_ function malloc().
It Malloc()s large chunks for you and manages the little pieces
locally.

- Moshe Braner