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

前提是你 提交于 2019-12-10 15:39:54

问题


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?


回答1:


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.




回答2:


Let us put some specs together.


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


If we only use character array objects or allocated memory for a character array, the longest string size is SIZE_MAX. The maximum return value for strlen(x_big) is SIZE_MAX - 1.


If code is successful with the following, then code has ventured far and in that case, a string may be constructed longer than SIZE_MAX, but I suspect either calloc() will return NULL first or (char *) ptr will fail.

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


来源:https://stackoverflow.com/questions/41870273/can-a-standards-conforming-string-be-longer-than-size-max-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!