Next: Higher Level Array Up: Memory Allocation Previous: Memory Allocation

Basic Memory Allocation

The basic macros are as follows:

     ALLOC( ptr, n_items )

Allocates n_items elements of the correct type, assigning the result to the argument ptr.

    FREE( ptr )

Frees the memory pointed to by the argument ptr.

    REALLOC( ptr, n_items )

Changes the size of the memory pointed to by ptr to be of size n_items elements, possibly changing the value of ptr in the process.

    ALLOC_VAR_SIZED_STRUCT( ptr, element_type, n_elements )

Allocates a variable sized structure, which must be of a specific form. The last element of the structure must be an array of size 1, and this array will constitute the variable-sized part of the structure. The argument element_type must be the type of this last element, and the argument n_elements is the desired number of elements to allocate for this array, in addition to the memory for the first part of the structure. An example of usage is:

            struct {  int   a;
                      float b;
                      char  data[1];
                   }                    *ptr;

            ALLOC_VAR_SIZED_STRUCT( ptr, char, 10 );

            ptr->a = 1;
            ptr->b = 2.5;
            ptr->data[0] = 'a';
            ptr->data[9] = 'i';

    ALLOC2D( ptr, n1, n2 )
    ALLOC3D( ptr, n1, n2, n3 )
    ALLOC4D( ptr, n1, n2, n3, n4 )
    ALLOC5D( ptr, n1, n2, n3, n4, n5 )

Allocates a 2 to 5 dimensional array of size n1 by n2, etc. and stores the result in the specified pointer, ptr. In the 2 dimensional case, this is accomplished with only 2 memory allocations, one to allocate n1 times n2 elements for the storage, and the second to allocate n1 pointers into the first memory area. In general, there is one memory allocation for each dimension required.

    FREE2D( ptr )
    FREE3D( ptr )
    FREE4D( ptr )
    FREE5D( ptr )

Frees the memory associated with the multi-dimensional array.

Next: Higher Level Array Up: Memory Allocation Previous: Memory Allocation
Fri Feb 17 15:37:42 EST 1995