C (45/304)

From:Bart King
Date:07 Aug 2000 at 14:53:55
Subject:Re: memory allocation fcns

At 05-Aug-00 09:02:31, Jack York <jyork@voyager.net> wrote:

> I'm looking for some advice on which memory allocating fcn I
> should use.

Er, I'm assuming "fcn" stands for "function". This isn't IRC :)

> 1) What is the recommended or most generally used fcn of the
> three (AllocPooled, AllocVec, AllocMem)?

Depends. AllocMem() is used for antique compatibility with OS 1.x, it
is rarely required for use under any later revisions.

AllocVec() is the most common form of memory allocation, and the most
safest.

Pooled memory allocation has a sole use to prevent serious memory
fragmentation because it allows you to allocate a chunk of RAM and
manipulate it to smaller chunks. If you allocate lots of small chunks
of RAM, pooled memory is great, otherwise it has little point, and just
gets you confused.

> 2) How does malloc fall into this comparison? Is it used mainly
> only if portable code is desired or are there other reasons?

malloc() is like an additional "layer" to the memory interface of your
operating system. All systems have malloc() and without it, memory
allocation would be very difficult if you don't know your operating
system at low-level.

> 3) Does it make much difference as far as program size/speed
> as to which fcn is used?

Using malloc()? It probably eats an extra 4 or 5 CPU cycles while it
calls the real memory allocation function but that's nothing.

The "low-level" memory allocation routine on Amiga is AllocMem(), and
all other memory functions eventually call that (or something
undocumented private to the system, possibly).

> 4) I saw mentioned before that if pools are used, it is better to
> allocate a different pool for each type (one for int, one
> for char, etc.). Is this the case?

Would sound a bit stupid and a real waste of time if you ask me.

> 5) Not that I would do this but...if pools are used, is it necessary
> to check if puddles have been allocated once the pool has been
> obtained? That is, do you need to say if ( ptr = AllocPooled (
> instead of just ptr = AllocPooled (

All memory allocations should be checked whatever, unless you are
coding your own memory management routines (which I have done without
blowing my own trumpet :).



Bart King of Bartman Software : Software Developer for Amiga/PC/PHP4
Work: http://www.bartmansoftware.co.uk -- bart@bartmansoftware.co.uk
Own : http://bart.shagged.org -- bart@shagged.org
. . . . . . . damned that I do, damned that I don't. . . . . . . .