Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!unmvax!deimos.cis.ksu.edu!uxc!uxc.cso.uiuc.edu!m.cs.uiuc.edu!kenny From: kenny@m.cs.uiuc.edu Newsgroups: comp.lang.c Subject: Conformant arrays-- how to? Message-ID: <4700036@m.cs.uiuc.edu> Date: 1 May 89 19:28:00 GMT Lines: 57 Nf-ID: #N:m.cs.uiuc.edu:4700036:000:2313 Nf-From: m.cs.uiuc.edu!kenny May 1 14:28:00 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@CS.UIUC.EDU University of Illinois 1304 W. Springfield Ave. Urbana, Illinois, 61801 Voice: (217) 333-5821