Can a standard string be longer than SIZE_MAX characters?

I did not find anything in the C11 standard, stating that the string cannot be longer SIZE_MAX(where SIZE_MAXdenotes the maximum value of the type size_t). For instance. if SIZE_MAXthere is long, but in my implementation there is a type long longthat is strictly larger than long, then I could define and index such a string with long long.

However, this will mean some unusual situations: strlenfor example, it may not be possible to return the actual size of the string, since the result will be converted to size_tat the end, so the length string SIZE_MAX+1will be displayed as having a size of 0, for example. Does this, for example, violate the standard and thus prevent the existence of such strings? For reference, 7.24.6.3 only says that:

7.24.6.3 strlen function

Summary

#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 preceding the terminating null character.

Am I missing something, or will it be completely true in (standard implementation) C11?

+4
2

[6.5.3.4 sizeof alignof]:

4 sizeof char, unsigned char char ( ), 1. , . 102). , , , .

5 , ( ) - size_t, "stddef.h" ( ).

, size_t, SIZE_MAX.

: calloc, . [7.22.3.2 calloc]:

calloc nmemb, . 0.

, size_t, SIZE_MAX calloc. NULL, .

+3

.


- , . C11 §7.1.1 1 ( )


sizeof... , ..... C11 §6.5.3.4 4

size_t sizeof; C11 §7.19 2

size_t SIZE_MAX C11 7.20.3 2


, SIZE_MAX. strlen(x_big) SIZE_MAX - 1.


, , , SIZE_MAX, , calloc() NULL , (char *) ptr .

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

Source: https://habr.com/ru/post/1667853/


All Articles