Path: utzoo!mnetor!uunet!lll-winken!lll-lcc!ames!ll-xn!mit-eddie!uw-beaver!uw-june!pardo From: pardo@june.cs.washington.edu (David Keppel) Newsgroups: comp.lang.c Subject: Re: Conformant Arrays in C (a better solution?) Message-ID: <4340@june.cs.washington.edu> Date: 1 Mar 88 21:52:19 GMT References: <8802240725.AA22255@jade.berkeley.edu> <2739@haddock.ISC.COM> <1988Feb29.205138.28452@sq.uucp> Reply-To: pardo@uw-june.UUCP (David Keppel) Organization: U of Washington, Computer Science, Seattle Lines: 37 In article <1988Feb29.205138.28452@sq.uucp> msb@sq.UUCP (Mark Brader) writes: > >Karl Heuer (karl@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