

malloc()                 General Function                malloc()




Allocate dynamic memory

cchhaarr *mmaalllloocc(_s_i_z_e) uunnssiiggnneedd _s_i_z_e;

malloc helps to manage  a program's free-space arenas.  It uses a
circular,  first-fit algorithm  to select an  unused block  of at
least  size  bytes, marks  the  portion it  uses,  and returns  a
pointer to it.  The function free returns allocated memory to the
free memory pool.

Each area  allocated by malloc is rounded up  to the nearest even
number and  preceded by  an unsigned  int that contains  the true
length.  Thus, if  you ask for three bytes you  get four, and the
unsigned that precedes the newly allocated area is set to four.

When an area is freed, its low order bit is turned on; consolida-
tion occurs  when malloc passes  over an area as  it searches for
space.  The end  of each arena contains a block  with a length of
zero, followed by a pointer to the next arena.  Arenas point in a
circle.

The  most  common  problem  with  malloc  occurs when  a  program
modifies  more space  than it  allocates  with malloc.   This can
cause later  mallocs to crash with a  message that indicates that
the arena has been corrupted.

***** Example *****

This example  reads from the  standard input up  to NITEMS items,
each of  which is up to  MAXLEN long, sorts them,  and writes the
sorted  list  onto  the  standard  output.  It  demonstrates  the
functions qsort, malloc, free, exit, and strcmp.


#include <stdio.h>
#define NITEMS 512
#define MAXLEN 256
char *data[NITEMS];
char string[MAXLEN];



main()
{
        register char **cpp;
        register int count;
        extern int compare();
        extern char *malloc();
        extern char *gets();







COHERENT Lexicon                                           Page 1




malloc()                 General Function                malloc()



        for (cpp = &data[0]; cpp < &data[NITEMS]; cpp++) {
                if (gets(string) == NULL)
                        break;
                if ((*cpp = malloc(strlen(string) + 1)) == NULL)
                        exit(1);
                strcpy(*cpp, string);
        }



        count = cpp - &data[0];
        qsort(data, count, sizeof(char *), compare);



        for (cpp = &data[0]; cpp < &data[count]; cpp++) {
                printf("%s\n", *cpp);
                free(*cpp);
        }
        exit(0);
}



compare(p1, p2)
register char **p1, **p2;
{
        extern int strcmp();
        return(strcmp(*p1, *p2));
}


***** See Also *****

arena,  calloc(), free(),  general functions,  malloc.h, memok(),
realloc(), setbuf()

***** Diagnostics *****

malloc returns NULL if insufficient memory is available.

***** Notes *****

The commonest error  associated with mmaalllloocc is failing to declare
it  properly.  You  should always declare  mmaalllloocc as  returning a
pointer to char.











COHERENT Lexicon                                           Page 2


