C++ – Why Use size_t for Indexing and Array Size Representation

c++size-t

According to C++ – should you size_t with a regular array?

ยง 18.2 6 The type size_t is an implementation-defined unsigned integer
type that is large enough to contain the size in bytes of any object.

I don't understand why this guarantees a type size_t to be big enough for an array index or big enough to represent the number of elements in an array.

For example:

int array[1000];

for (size_t i = 0; i < 1000; ++i) {

}

It seems unrelated to me why a "large enough number to contain the size in bytes of an object" == guarantees a type size_t to be big enough for an array index".

Best Answer

An array is an object. If size_t can represent the size of the array in bytes, then it can also surely represent any index into it, since an individual element has at least one byte size.

An array with a larger size than that is simply not allowed by the language.