Conformant arrays-- how to?

kenny at m.cs.uiuc.edu kenny at m.cs.uiuc.edu
Tue May 2 05:28:00 AEST 1989


Some time ago, I saw in this newsgroup a discussion for a proposal to
implement conformant arrays of more than one dimension in C.  If I
remember correctly, the proposed scheme was to allow a function to be
expressed as:

	return_type f (int m,		/* Number of rows */
	               int n,		/* Number of columns */
	               double x [] [n]);/* x is a m x n array */
	{
	    .... code for f ....
	}

The generated code would use standard rules for pointer arithmetic,
except that the variable n would enter into the computation:

	sizeof (x [0] [0]) == sizeof (double)
	sizeof (x [0]) == n * sizeof (double)
	sizeof x == sizeof (double (*)[n]) [ == sizeof (double *) ?? ]

and when these rules are applied, we get C's usual row-major storage
layout.

Has anyone tried implementing such a scheme?  (I recall that X3J11
rejected it, quite correctly, because of `insufficient prior art.')
What are the pitfalls to avoid?  At first glance, it seems trivial to
implement, but I suspect otherwise, since dmr didn't do it that way (I
have generally discovered that dmr's less-than-obvious decisions were
right -- with one or two insignificant exceptions).

In the absence of such a scheme, what do people consider to be the
most robust way of handling conformant 2-D arrays?  Two obvious
schemes come to mind:

	- Represent each array by an edge vector that points to the
rows; x [i] [j] is applied to an object of type (double **) and the
pointer chain gives the correct value.

	- Pass to the function (in an argument called Px) the address
&(x [0] [0]), and then replace all references to x [i] [j] with 
Px [n * i + j].  How portable is this latter scheme?  I can imagine
some brain-damaged compiler inserting a pad of unused storage between
rows of the array and defeating this attempt to replicate C's address
calculation.

Of course, with the second scheme, we *could* make the caller use the
same representation, and declare x to be
	double x [m * n + 1] /* replace m and n with the appropriate
				constants, of course */
which should be completely portable, but rather awkward.


Kevin Kenny			UUCP: {uunet,pur-ee,convex}!uiucdcs!kenny
Department of Computer Science	ARPA Internet or CSNet: kenny at CS.UIUC.EDU
University of Illinois
1304 W. Springfield Ave.
Urbana, Illinois, 61801		Voice: (217) 333-5821



More information about the Comp.lang.c mailing list