From: brown@NCoast.ORG (Stan Brown)
Subject: Re: {m|c|farm}alloc
Organization: Oak Road Systems, Cleveland Ohio USA
Date: Thu, 24 Dec 1992 15:34:52 GMT
Keywords: memory allocation

In article <1992Dec22.100613.29860@aau.dk> psykseth@aau.dk (Seth Chaiklin) writes:
>Whats the difference between malloc, farmalloc, calloc, etc?
>I have read what is in the Turbo C 2.0 manual, so I know that malloc is 
>DOS specific while calloc can be found in Unix.  I know the general 
>differences between farmalloc and malloc as described in the manuals, 
>but what I am really interested in is to understand, WHEN one would 
>prefer to use one or the other. 

I haven't got Turbo C 2.0, but I do have Borland C 2.0.  I'd be willing
to bet the functions are the same between the two, so here goes.

calloc was in K&R1, and it's an ANSI standard function.  You call it
when you want to allocate memory and guarantee that the allocated memory
is set to 0 at the outset. 

malloc is also ANSI standard, but much to my surprise it was not in K&R1.
(However, it is in virtually any compiler you can buy today.)  It's
similar to calloc except it doesn't (necessarily) set all the allocated
memory to zeroes, and it takes one argument (the number of bytes) not
two (umbers multiplied together to produce the number of bytes desired).

If the Turbo C manual says malloc is DOS specific, then it's upholding
the fine tradition of accuracy in documentation that we've come to
expect from Borland (i.e. it's wrong).  The Borland C 2.0 manual does
correctly state that malloc is an ANSI function.

farmalloc is a Borland extension to the library, and you would use it
only when you're in the small or compact memory model (where data
pointers are 16 bits and the size of all data objects is limited to
64K).  If you want to run your code in small or compact model, but you
need one big array, you can declare it as a far pointer and allocate it
using farmalloc.  Then references to that array will be 32-bit
references, and it won't count against the 64K limit.  References to all
other variables will be 16-bit pointers, which are more efficient.  If
you're in compact, large, or huge memory model, there's no real
difference between malloc and farmalloc, so there's no reason to use
farmalloc.

By the way, all three functions return void pointers, so there's no need
to cast their results.  A correct programming example:

    #include <stdlib.h>    /* brings the prototype in scope */
    unsigned long *balances;
      . . .
    balances = calloc(NUM_BALANCES, sizeof *balances);
        /* or  malloc(NUM_BALANCES * (sizeof *balances)); */

You could replace the second argument to calloc with "sizeof(unsigned
long *)" and the result would be the same.  However, if you later
changed the type of the 'balances' array you would have to remember to
change the type given in the calloc call also.  With the code as given
above the compiler will always produce the desired code regardless of
the type of balances.


-- 
Stan Brown, Oak Road Systems                      brown@Ncoast.ORG
"If you click on its capital, Ouagadougou, you'll hear its name
pronounced--in this instance, a most helpful feature."  
                                  --PC Magazine, 92/12/22, pg 352
Xref: sparky comp.os.msdos.programmer:11694 comp.lang.c++:18529 comp.lang.c:19003