[7.20.3.1/2] of C99 states: The calloc function allocates space for an
array of nmemb objects, each of whose size is size. The space is
initialized to all bits zero.
The way I read that, objects larger than SIZE_MAX still can not exist.
Even though calloc() can return a block of memory larger than
SIZE_MAX, what it's really doing is allocating space for some number
of objects whose individual sizes do not exceed SIZE_MAX.
The array is itself an object.
Fair enough. Then, what is the rationale for the premise that "no
object can be larger than SIZE_MAX bytes"? Is it because sizeof() is
undefined for such an object? Just because sizeof() is undefined for
objects larger than SIZE_MAX bytes doesn't seem to imply that objects
larger than SIZE_MAX bytes can't exist -- just that you can't
determine the size of them with sizeof(). That's my same rationale for
why undefined behavior of strlen() doesn't affect the maximum length
of a string.
The relation between all of this, as I see it (retracting my previous
incorrect statement that calloc returns an "array of objects" that is
not in itself an "object"), is:
- The upper limit of sizeof() does not imply the upper limit of the
size of an object.
- Nothing else in the standard seems to specify the maximum size of
an object either.
- Therefore an object has infinite maximum size, in theory.
- Therefore, the use of calloc() to allocate more than SIZE_MAX
bytes does not violate any upper limits on the size of a theoretical
object (which is infinite).
- Therefore, calloc() can be used to allocate an "extra large"
object larger than SIZE_MAX bytes. The maximum number of bytes that
calloc() can allocate is SIZE_MAX*SIZE_MAX, and *this* is therefore an
upper limit on the size of an object, in practice.
- So, that "extra large" object can exist regardless of sizeof()'s
limits, and can be obtained via calloc(). It is a real object.
- The standard does not explicitly place an upper limit on the
length of a string. A string has infinite maximum length, in theory.
- If the extra large object is filled with a consecutive sequence of
characters terminated by a 0 character, it fits the definition of a
string, and does not exceed any upper limits on the length of a
theoretical string (which is infinite).
- The upper limit of strlen() does not imply the upper limit of the
length of a string.
- The standard defines the "length of a string" as the "number of
bytes preceding the null character". Therefore the length of a string
placed in the maximum amount of memory allocatable by calloc() is
"SIZE_MAX * SIZE_MAX - 1". This, then, is the upper limit on the
length of a string in practice.
In other words, this shows that:
1) An object of maximum size SIZE_MAX * SIZE_MAX can be obtained
without violating the standard.
2) A string of maximum length SIZE_MAX * SIZE_MAX - 1 can be
constructed in that object without violating the standard.
3) The maximum theoretical size of an object (and length of a
string) is infinite, according to the standard (but correct me if I'm
wrong about the object max size, that blows a hole in the whole
thing).
4) The maximum practical size of an object is SIZE_MAX * SIZE_MAX,
because there is no way to obtain a larger object.
5) The maximum practical length of a string residing in memory is
SIZE_MAX * SIZE_MAX - 1, because there is no way to obtain a larger
object that the string can be constructed in.
So I guess that means the maximum length of a string *in memory* is
SIZE_MAX * SIZE_MAX - 1. However, note that that only applies to
strings that exist in memory; also note that the standard does not
require strings to reside in memory (and does not require that a
"pointer to a string", as defined in 7.1.1/1, must exist for a string
to exist).
Going back to the example of a string read from the standard input
stream with fgetc(), then, the actual maximum length of a string is
still infinite.
Jason