Passing sizes of arrays without a separate argument for the length

Evan Bigall evan at ctt.ctt.bellcore.com
Tue Sep 20 23:38:03 AEST 1988


In article <8809191507.AA17512 at ucbvax.Berkeley.EDU> U23405 at UICVM (Michael J. Steiner) writes:

>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] (provided that the data starts
>at array[1], as in PASCAL), since arrays should have at least a few
>elements.
>
>Any comments or suggestions are appreciated.

Well, I can think of two potential problems first, if you have an array of char
you are limited to arrays 256 (depending on your byte size) bytes long.

Second, on many machines the operation of converting a float to an int is very
expensive (and on some it isnt of course).

You can avoid both these problems though, by just storing the actual int in
the beginning of the array (taking most probably 4 chars or half a float).
But, if you are going to go to that extreme why not just declare a struct 
and let the compiler do all the messy stuff for you?

I guess the real question is what exactly are you trying to accompish?  
If you are just trying to speed the code up by not passing an extra argument 
I dont think this is the way (of course I didnt read the 200 articles on
argument passing so take this with a grain of salt).

I'm also not sure if this is a good idea if your goal is to make the code is
more readable.  Most C programmers are used to arrays starting at 0 and would
probably go through and fix all your posthole errors for you.  You would have
to clearly document the whole business.

Evan


I barely have the authority to speak for myself, certainly not anybody else.
(201)699-4909 evan at ctt.bellcore.com or {backbone}!bellcore!ctt!evan



More information about the Comp.lang.c mailing list