符合标准的字符串是否可以超过 SIZE_MAX 个字符?

Can a standards-conforming string be longer than SIZE_MAX characters?

我没有在 C11 标准中找到任何规定字符串长度不能超过 SIZE_MAX(其中 SIZE_MAX 表示 size_t 类型的最大值)字符的内容。例如。如果 size_maxlong,并且在我的实现中有一个严格大于 longlong long 类型,那么我可以使用 [= 定义和索引这样的字符串16=].

然而,这意味着一些不寻常的情况:例如,strlen,可能无法 return 字符串的实际大小,因为结果将被转换为 size_t 最后,因此长度为 SIZE_MAX+1 的字符串将被报告为大小为 0,例如。例如,这是否会违反标准,从而阻止此类字符串的存在?作为参考,7.24.6.3 只说:

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.

我是否遗漏了什么,或者这在 C11(给定的符合标准的实现)中是否完全有效?

来自[6.5.3.4 sizeof和alignof运算符]:

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

因此任何数组的大小都不能超过size_t可以容纳的大小,因此字符串的大小不能超过SIZE_MAX

编辑:从使用 calloc 开始,请参阅 [7.22.3.2 调用函数]:

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.

它为数组分配 space,但数组大小必须适合 size_t,因此您不能使用 calloc 分配超过 SIZE_MAX。如果你这样做,它必须 return NULL

让我们把一些规格放在一起。


A string is a contiguous sequence of characters terminated by and including the first null character. C11 §7.1.1 1 (my emphasis)


When sizeof is applied ... operand that has array type, the result is the total number of bytes in the array.... . C11 §6.5.3.4 4

size_t which is the unsigned integer type of the result of the sizeof operator; C11 §7.19 2

The limit of size_t is SIZE_MAX C11 7.20.3 2


如果我们只使用字符数组对象或为字符数组分配内存,最长的字符串大小是SIZE_MAX . strlen(x_big) 的最大 return 值为 SIZE_MAX - 1


如果以下代码成功,那么代码已经冒险了很多,在这种情况下,字符串 可能构造得比 SIZE_MAX 长,但我怀疑要么calloc() 将 return NULL 首先或 (char *) ptr 将失败。

double *ptr = calloc(SIZE_MAX, sizeof *ptr);
assert(ptr);
char *s_waybig = (char *) ptr;