/* @SUBTITLE "alloc2d - matrix memory allocation" */ #include /* Builds a C-like 2D matrix, with a side-vector for pointer-indirect * access to the matrix. */ extern char *alloc(); /* used for memory allocation */ char ** /* pointer to matrix */ alloc2d(rows, cols, elsize) int rows, cols; /* number of rows and cols */ int elsize; /* size of each matrix element (bytes) */ { char *block; /* the big block that is the matrix */ char **side; /* the side vector pointing to rows */ int rowsize = cols * elsize; /* length of each row */ char *arow; /* pointer to a row */ int row; /* index of a row */ block = alloc(rows * cols * elsize); side = (char **) alloc(rows * sizeof(char *)); arow = block; for (row = 0; row < rows; row++) { side[row] = arow; arow += rowsize; } return(side); }