Conformant Arrays in C (a better solution?)

David Keppel pardo at june.cs.washington.edu
Wed Mar 2 07:52:19 AEST 1988


In article <1988Feb29.205138.28452 at sq.uucp> msb at sq.UUCP (Mark Brader) writes:
 >
 >Karl Heuer (karl at haddock.ima.isc.com) writes:
 >> ... Alternately, you could petition X3J11 to add this with the syntax
 >>   float **a = d2malloc( float, nrows, ncols );
 >> where the first argument is the type. ... [in general]
 >> ... this would require assistance from the compiler via a builtin.
 >
 >Well, there is also this:
 >
 >	float **a;
 >	d2malloc (a, float, nrows, ncols);
 >
 >with
 >	#define d2malloc(var,type,nr,nc) { \
 >		int _i, _j = (nc);\
 >		(var) = ((type) **) malloc ((_i = (nr)) * sizeof ((type) *));\
 >		while (_i) (var)[--(_i)] = ((type) *) malloc (sizeof ((type)));\
 >	}
 >[Not tested; checks of return status of malloc() omitted for brevity]
 >But I find this at least equally unsatisfactory.

Or possibly:

    float **a = (float **) dmalloc( dims, dimsvals, ptrsizes );

where "dims" is an array of n+1 ints for n dimensions, zero terminated,
sizebase is the sizes of the intermediate objects:

    static int dismvals[] = { 5, 6 }
    static int ptrsizes = { sizeof(float), sizeof(float *) }

This assumes that the important thing about the intermediate type pointers
is their size and not their format.  While (I think) this is mostly portable,
I can bet that it isn't entirely portable.  I'm not sure that making it a
builtin would make it any more portable.

	;-D on  (Just my $0.02 in real *copper* pennies)  Pardo



More information about the Comp.lang.c mailing list