Passing sizes of arrays without a separate argument for the length

Wayne Mesard mesard at bbn.com
Tue Sep 20 23:38:38 AEST 1988


>From article <8809191507.AA17512 at ucbvax.Berkeley.EDU>, by U23405 at UICVM (Michael J. Steiner):
> In other words, I had an idea of putting the maximum array dimension in
> the array itself. This should work with most arrays (since chars, floats,
> unsigneds, shorts, etc. can be converted to integers). Also, there is always
> room for the size of the array in array[0] 

If I were to adopt your scheme, I'd want to do it consistently, since I
would otherwise hopelessly confuse myself and anyone reading my code (if
A[0] was sometimes data, and other times size info).  But most of the
time I either don't need to know an array's length: e.g., do you want to
store a strings allocated size, or it's current length?; or I get this
information elsewhere: from a #define'd constant (as should be the case
in your example).  This means that most of the time I'd be wasting the
storage in the zeroth element of the array as well as processing time to
assign it the length of the array unnecessarily.

And that's to say nothing of the inefficiency of having to dereference
to get the size (especially when the array is a float) when the
alternative is accessing a parameter whose value is most likely in a
register (again, as in your example).

Don't try to reinvent Pascal.  We still haven't recovered from the last
time it was attempted:-)

-- 
unsigned *Wayne_Mesard();    The last thing one knows in constructing
MESARD at BBN.COM               a work is what to put first.
BBN, Cambridge, MA                                      -- Blaise Pascal



More information about the Comp.lang.c mailing list