ALLOC2D(3SP)

NAME
Alloc2d, Free2d, Alloc3d, Free3d - allocate arrays

SYNOPSIS

#include <speech.h>

char **Alloc2d (int dim1, int dim2, int size)

int Free2d (char **array)

char ***Alloc3d (int dim1, int dim2, int dim3, int size)

int Free3d (char ***array)

DESCRIPTION

These routines are used to create arrays at runtime that can be accessed like typical C arrays.

Example:
    int i,j;

    char **a;

    a = Alloc2d( 10, 10, sizeof(int) );

    for( i = 0; i < 10; i++ ) {
        for( j = 0; j < 10; j++ ) {
            a[i][j] = i * j;
        }
    }

Alloc2d() is used to allocate a two dimesional array in which each element array is of size size. Free2d() should be used to free the arrays allocated with Alloc2d().

Alloc3d is used to allocate a three dimensional array in which each element is of size size. Free3d should be used to free arrays allocated with Alloc3d().

EXAMPLES

  float **twod;
  float **threed;
  twod = (float **) Alloc2d(10, 10, sizeof(float));
  if (twod == NULL) {
    fprintf(stderr, "error allocating memory for twod.");
    exit(1);
  }
  threed = (float **) Alloc3d(20, 10, 10, sizeof(float));
  if (threed == NULL) {
    fprintf(stderr, "error allocating memory for threed.");
    exit(1);
  }
 .
 .
 .
  Free2d ((char **) twod);
  Free3d ((char ***) threed);

DIAGNOSTICS

Alloc2d() and Alloc3d() returns a negative number on error and sets ErrorString.

SEE ALSO
ErrorString(3SP), malloc(3), free(3),

BUGS

AUTHORS
Pieter Vermeulen
Carnegie Mellon University