Re: malloc, calloc, and all that Jazz...

matt@nospam.uts.edu.au
Fri, 29 Mar 1996 12:35:00 +1100 (EADT)

Joshua Graham Pitcher wrote this...

> Okay proggers, something to get your teeth into!

> I don't really understand how calloc and malloc works. Is calloc
> (and malloc and dealloc) executed at compile time, or are they
> proper run time functions?

run time, otherwise you would waste disk space, imagine allocating 50M
of memory at compile time rather than run time!! the disk wastage
would be horrendous.

> If they are run time functions (which I think they are) where is the
> necessary info relating to these functions held? (for example,
> tables showing which memory has been alloced and dealloced, etc).

it is called the heap. different versions of malloc() work
differently, some are optimized for space, zome are optimized for
speed, some arent optimized :) you'd need to check the source for the
particular version you are running to see how it works. there are
enough versions of malloc() out there that are free that you can look
at to get the idea of how it works.

> If I malloc 32 bytes, then malloc 64 bytes, dealloc the first 32
> bytes and then malloc another 24 bytes, will this 24 byte allocation
> slot in where the 32 byte allocation used to be? If so, how does it
> keep track of memory usage?

there is no function such as dealloc(), there is free() and
realloc(). free() will free _all_ the memory you allocated at a
certain region. realloc() will allow you to change the size of memory
you have allocated.

> I did check a few books before posting, but non gave any meaty
> descriptions of how these functions worked.

look in a book on memory management, because this is exactly what it
is. typically you can also find this in operating systems books.
although these also handle how the OS does memory management which is
significantly different from application memory management.

Matt

-- 
#!/bin/sh
echo '16i[q]sa[ln0=aln100%Pln100/snlbx]sbA0D3F204445524F42snlbxq'|dc;exit
  Matthew Keenan   Data Network Admin   Information Technology Division
        University of Technology     Sydney Australia

It's nice to be in a position where people apologize because they assume there's humor in your work, based on past experience, but they're not sure where it is. -- Rob Pike