M
Malcolm McLean
Time of writing != compile time.Tubular Technician said:Array sizes need to be known at compile time; furthermore according
to ISO/IEC 9899:1999 5.2.4.1 a single object is not guaranteed to be
able to be larger than 65535 bytes, which according to 5.2.4.2.1 also
happens to be the guaranteed range of an unsigned int (UINT_MAX).
So an index type of unsigned int ("natural" int size?) seems perfectly
fine to me.
Let's say we want to calculate a standard deviation. The prototype is
double stdev(double *x, N);
what type should N be? If you don't know how big the maximum array is going
to be, which you don't for this function - except that it fits in memory -
it must be a size_t.
Thus we must write
size_t i;
for(i=0;i<N;i++)
{
}
Of course that is misleading, because i is not in any shape or form a size
type. It is an index counter. The implications of introducing size_t simply
weren't thought through.
You find that functions like stdev() are by no means uncommon. Very
frequently you will not hard code the size of an array, until maybe in the
very top layer of code.
The worse problem is that, frequently, you don't know the exact size of an
array but you know that it will be small. For instance the number of
children in a class. Should that be a size_t or not? If we sort the class by
grade, qsort() takes two size_ts. However people will naturally gib at using
a size_t when an int, realistically, is going to be enough. So you get
inconsistency.
Most integers are ultimately used as index variables. Not every integer, of
course, for instance if you dealing with amounts of money you may choose to
represent the sums by integers. But every time you add up a list of amounts
of moeny, you will have one index integer to iterate through the array and
another to count it. Programs don't spend their time doing calculations, but
on moving data from one place to another. Something like 20% of all
mainframe cycles are used in sorting, for instance.
Even if an integer is a type field, typically that is used as an array
index. For instance if we have an emum {MR, MRS, MISS, MS, DR, REV, PROF,
LORD} we will probably have an array of strings we index into to help us
construct letters.
That's the problem. Really virtually every integer in the program should beWhat I was wondering in my original post is, if an object *does*
represent a size/index, but said value is neither the result of either
sizeof nor does it involve any of the standard library functions taking
or returning a size_t, how common is it to make it a size_t nonetheless?
a size_t, because they will almost all end up being used to derive index
calculations. But that is unlikely to be accepted, partly because of the
unsignedness and efficiency considerations, but mainly because to type
"size_t i;" is so counter-intutitive.
That's why I think think size_t will ultimately have to go, and the
introduction of 64-bit types on the desktop will be the catalyst for this,
because it will no longer be true that int can index an arbitrary array.