4

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".

csguy
  • 1,354
  • 2
  • 17
  • 37

2 Answers2

4

It simply means, the largest array supported by the platform will be indexable with size_t.

Consider:

int array[1000];

for (uint8_t i; i < 1000; ++i) {}

This is clearly wrong, uint8 does not have range to index that array. size_t does, always, guaranteed by the standard.

As to why it is bytes, using sizeof array gives byte size. There needs to be a type guaranteed to be able to represent the result.

I guess it would make more technical sense to use ptrdiff_t to index arrays, because that is what array index kind of is: *(array+index). But that's not so common, I guess it looks uglier, is longer to type and may be more confusing.

Note that C++ standard does not make any similar guarantees about any other type. But this range issue is somewhat theoretical in most practical cases, as you can be sure that a 64 bit integer can also index anything that fits in the memory. It's more important for communicating intent.

hyde
  • 60,639
  • 21
  • 115
  • 176
  • I can see how it is good for communicating intent, but I still fail to see how being able to represent the largest size in bytes means it can also represent the largest possible index as well. For example: if the largest possible size in bytes of an object is 100, that means a size_t type is atmost 100. It also means the largest index is 100? And the largest size array is 100 elements? But arent array elements different sizes as well? (the size of double x[100] != the size of int x[100]) – csguy Jan 14 '20 at 06:16
  • @csguy Nowhere does it say that `size_t` cannot represent integer values *larger* than the largest possible index or size in bytes of an object. Your quote in the question simply says that it can *at least* represent the size of any object in bytes. – walnut Jan 14 '20 at 06:18
  • @csguy If the largest possible object is 100 bytes, `size_t` would not need to be higher than `100`. I think you are missing the point here: the size of an element is always greater than or equal to one byte. – iz_ Jan 14 '20 at 06:25
  • 1
    @csguy You can't have array elements smaller than sizeof 1 (maybe related, `sizeof char` is 1 by definition, no matter how many bits `char` is in some platform). So array can't have indexes higher than its size in bytes (when by "byte" we mean same as `char`). – hyde Jan 14 '20 at 06:36
4

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.

walnut
  • 21,629
  • 4
  • 23
  • 59
  • can you elaborate on "since an individual element has at least one byte size." – csguy Jan 14 '20 at 06:12
  • @csguy A byte is the fundamental unit of storage in C++. An object cannot be smaller than one byte and each element of an array is a non-overlapping subobject of the array. – walnut Jan 14 '20 at 06:15
  • in your comment below you corrected my understanding in saying that size_t "can at least represent the size of any object in bytes." This still makes me confused as to why it implies that "it can also surely represent any index into it". The 2 still seem unrelated to me, even moreso as size_t represents atleast the size of any object in bytes and not atmost the size of any object in bytes – csguy Jan 14 '20 at 06:26
  • @csguy The number of elements in an array is always smaller than or equal to the size of the array in bytes. (Because each element has at least one byte size.) Therefore if all possible array sizes in bytes fit into `size_t`, then so do all possible indices as well (because they are a subrange of the former). I am not really sure how to express in more detail. Maybe you can add a more detailed explanation for the problem you are seeing to the question? – walnut Jan 14 '20 at 06:30
  • @csguy The least amount of space an object can take up is 1 byte. Therefore, elements of an array take up at least 1 byte. Therefore, the maximum index is always less than or equal to the size of the array in bytes. – iz_ Jan 14 '20 at 06:31
  • @ walnut @iz_ thanks, i get it now. sorry for being slow – csguy Jan 14 '20 at 06:44