Conformant Arrays in C

Richard A. O'Keefe ok at quintus.UUCP
Mon Feb 22 14:17:34 AEST 1988


Background:
	David Hough sent me his comments on dpANS C.
	He wants a language which can do at least as much as Fortran could.

	One of his criteria was that you should be able to write
	subroutines that handle multi-dimensional arrays sensibly.

	I wrote back to him about some of his points.  I started to
	argue that this criterion required much too big a change to
	the language, and ended up talking myself out of that
	position.

	David Hough sent (part of) my very sketchy proposal to
	comp.lang.c.

I should point out that *no* part of the suggestion is original to me.
Conformant arrays are part of ISO Pascal, and Turing has them.

Doug Gwyn writes:
> I didn't see how this proposal would work.  For correct code
> to be generated, all but one of the array dimensions has to
> be known via the calling sequence.  There seem to be only
> two ways to achieve this:  (1) rely on explicit dimension
> parameters, as in the first example; (2) automatically pass
> extra size information along with the array address.  The
> second approach is incompatible with current treatment of
> arrays, unless the function prototype has parameters declared
> some special way (perhaps using yet another type qualifier),
> so that the compilers will pass array information specially
> for the particular function.  In other words, to make method
> (2) work, more must be involved than just the function
> definition.  Is this what was intended by your second example?
> (Since the body of the example didn't make use of ar, ac, etc.,
> it's hard to be sure.)
	[ there was meant to be an assert() checking the comments,
	  but I forgot.  Sorry about that, but it wasn't ready for
	  "publication".
	]
The answer is that I wrote that it would not be legal to call a
function with a conformant array parameter unless the actual
function definition or a prototype were in scope, so that the
compiler would know to pass the dimension information.  This is
easily checked by a tool like "lint":  simply mark in the symbol
table each function which is called through through an implicit
or old-fashioned declaration, and complain about any marked
function which has a conformation array parameter.  Functions
which take a variable number of parameters are already subject to
just such a restriction.  The declaration of conformant
dimension parameters "? id" is precisely the "special way" of
declaration which Doug Gwyn rightly claims to be necessary.

And yes, conformant array parameters *are* incompatible with the
current treatment of arrays.  If you have
	p[D1]...[Dn]
then
	p[E1]...[Ek]
would be a pointer only if none of Dk+1,...,Dn were a conformant dimension
parameter (?id) [that's "only if", not "if and only if"], and
 sizeof p[E1]...[Ek]
would be legal if and only if none of Dk+1,...,Dn were a conformant
dimension parameter.

Oddly enough, a weak spot in C comes to our aid:  Pascal has this
nasty problem:  what does
	procedure p(var a: array [la..ua: integer] of char;
		    var b: array [lb..ub: integer] of char);
	    begin
		a := b;
	    end {p};
mean?  C wouldn't have that problem, because it lacks array assignment.


I have managed to convince myself that
 o David Hough is right: something like this IS necessary if one is
   to be able to write mathematical libraries in C with at least the
   same generality as Fortran (e.g. EISPACK, LINPACK, IMSL, NAG, ...)
 o conformant array parameters, though a *hack*, are at least a
   hack which is known to work
 o conformant array parameters can be added to C without major
   distortion, though they would be exceptions to "sizeof" and
   quiet-conversion-to-pointer

I have NOT yet managed to convince myself that
 ? conformant array parameters should be added to C
but I think one could make a considerably better argument for this
addition than for 'noalias'.



More information about the Comp.lang.c mailing list