Conformant Arrays in C (a better solution?)

Karl Heuer karl at haddock.ISC.COM
Sat Feb 27 12:10:03 AEST 1988


In article <8802240725.AA22255 at jade.berkeley.edu> ERICMC at USU.BITNET (Eric McQueen) writes:
>Summary:  Standard run-time function would be a better solution.
>
>As has been pointed out several times [recently in <2336 at umd5.umd.edu> by
>Chris Torek (chris at trantor.umd.edu)], the following is already possible:
>void matmul( double **a, double **b, double **c, int p, int q, int r) {...}
>and is much simpler.  The only problem I see with this is that it is
>difficult to declare such an array in the calling procedure.

Yes.

>[X3J11 should standardize a function to allocate such row-vector arrays, e.g.
>  float **a = d2malloc( sizeof(float), nrows, ncols ); ]

Now we have a problem.  How does the routine know that the row vectors should
be "float *" rather than some other pointer type?  This design implicitly
assumes that all pointers look alike.

Now, you can (and do) assert that only "strange systems" have this problem,
but that doesn't make it go away.  If you insist that an ANSI implementation
must supply d2malloc(), and that strange systems must therefore be non-ANSI,
you're going against the X3J11 charter to work on as many systems as possible.
If you allow an implementation to omit d2malloc(), it kind of defeats the
purpose of putting it into the standard.

To put it another way, if I know of a system where your function can't
possibly be implemented, I can't comfortably use it in my portable programs,
even if it somehow gets put in the standard.

What can we salvage?  As you mentioned, you could make explicit functions for
the most common datatypes (int, long, float, double).  The implicit rule that
all struct pointers are equivalent allows you to do it for generic structs,
too; this ought to be useful.

Alternately, you could petition X3J11 to add this with the syntax
  float **a = d2malloc( float, nrows, ncols );
where the first argument is the type.  On non-strange systems this has the
simple implementation you outlined; the first argument is used only as a
parameter to sizeof().  On strange systems, this would require assistance from
the compiler via a builtin.  (Cf. <stdarg.h>)

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list