Dynamic multidimensional arrays

Karl Heuer karl at haddock.ISC.COM
Wed Jun 15 09:09:28 AEST 1988


In article <59 at cubsun.BIO.COLUMBIA.EDU> shenkin at cubsun.UUCP (Peter Shenkin) writes:
>[What about]
>	float **matrix( int nrl, int nrh, int ncl, int nch )
>[and its N-dimensional generalization?]
>	float **array( int Ndim, 
>	  int n1l, int n1h, int n2l, int n2h, ..., int nNl, int nNh )

Since normal arrays in C are zero-based, I don't see any reason to specify the
lower bounds.

If you allocate the entire array (including space for the pointers as well as
the contents) in a single chunk, then you don't need all those free_array()
routines -- the standard free() will work.  I've written it this way.

The N-dimensional generalization is incorrectly typed.  Since the number of
levels of indirection is not known at compile-time, it should be "void *".

There's a problem with the lack of generality.  You suggest implementing these
in triplicate: arrays of integer, float, or double are allowed.  What if some
other type is desired, e.g. long int, or some random struct?  I'd prefer a
syntax that expects the type-name as an argument to the macro:
	float **mtrx = array2d(float, nr, nc);
Unfortunately, on some architectures the only way to implement this correctly
requires a (minor) hook in the compiler.

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint
Followups to comp.lang.c only.



More information about the Comp.lang.c mailing list