I did not find anything in the C11 standard stating that a string could not be longer than SIZE_MAX
(where SIZE_MAX
denotes the maximum value of the size_t
type) characters. E.g. if size_max
is long
, and in my implementation there is a long long
type that is strictly larger than long
, then I could define and index such a string using a long long
.
However, this would imply some unusual situations: strlen
, for instance, might be unable to return the actual size of the string, since the result would be converted into size_t
at the end, thus a string of length SIZE_MAX+1
would be reported as having size 0, for instance. Would this, for instance, violate the standard, and thus prevent such strings from existing? For reference, 7.24.6.3 only says that:
7.24.6.3 The strlen function
Synopsis
#include <string.h>
size_t strlen(const char *s);
Description
The strlen function computes the length of the string pointed to by s.
Returns
The strlen function returns the number of characters that precede the terminating null character.
Did I miss something, or would this be perfectly valid in (a given standards-conforming implementation of) C11?
From [6.5.3.4 The sizeof and alignof operators]:
4 When sizeof is applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1. When applied to an operand that has array type, the result is the total number of bytes in the array. 102) When applied to an operand that has structure or union type, the result is the total number of bytes in such an object, including internal and trailing padding.
5 The value of the result of both operators is implementation-defined, and its type (an unsigned integer type) is size_t, defined in
"stddef.h"
(and other headers).
It follows that the size of any array cannot be more than size_t
can hold, and thus the size of a string cannot be more than SIZE_MAX
.
Edit: as of using calloc
, see [7.22.3.2 The calloc function]:
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.
It allocates space for an array, but array size must fit into size_t
, so you can't allocate more than SIZE_MAX
with calloc. It must return NULL
if you try so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With