Why is max length of C string literal different from max char[]?

后端 未结 3 1682
面向向阳花
面向向阳花 2020-12-06 10:31

Clarification: Given that a string literal can be rewritten as a const char[] (see below), imposing a lower max length on literals than on

相关标签:
3条回答
  • 2020-12-06 10:41

    Sorry about the late answer, but I'd like to illustrate the difference between the two cases (Richard J. Ross already pointed out that they're not equivalent.)

    Suppose you try this:

    const char __THE_LITERAL[] = { 'f', 'o', 'o', '\0' };
    const char* str = __THE_LITERAL;
    char *str_writable = (char *) str;  // Not so const anymore
    str_writable[0] = 'g';
    

    Now str contains "goo".

    But if you do this:

    const char* str = "foo";
    char *str_writable = (char *) str;
    str_writable[0] = 'g';
    

    Result: segfault! (on my platform, at least.)

    Here is the fundamental difference: In the first case you have an array which is initialized to "foo", but in the second case you have an actual string literal.

    On a side note,

    const char __THE_LITERAL[] = { 'f', 'o', 'o', '\0' };
    

    is exactly equivalent to

    const char __THE_LITERAL[] = "foo";
    

    Here the = acts as an array initializer rather than as assignment. This is very different from

    const char *str = "foo";
    

    where the address of the string literal is assigned to str.

    0 讨论(0)
  • 2020-12-06 10:49

    The limit on string literals is a compile-time requirement; there's a similar limit on the length of a logical source line. A compiler might use a fixed-size data structure to hold source lines and string literals.

    (C99 increases these particular limits from 509 to 4095 characters.)

    On the other hand, an object (such as an array of char) can be built at run time. The limits are likely imposed by the target machine architecture, not by the design of the compiler.

    Note that these are not upper bounds imposed on programs. A compiler is not required to impose any finite limits at all. If a compiler does impose a limit on line length, it must be at least 509 or 4095 characters. (Most actual compilers, I think, don't impose fixed limits; rather they allocate memory dynamically.)

    0 讨论(0)
  • 2020-12-06 10:54

    It's not that 509 characters is the limit for a string, it's the minimum required for ANSI compatibility, as explained here.

    I think that the makers of the standard pulled the number 509 out of their ass, but unless we get some official documentation from this, there is no way for us to know.

    As far as how many characters can actually be in a string literal, that is compiler-dependent.

    Here are some examples:

    • MSVC: 2048
    • GCC: No Limit (up to 100,000 characters), but gives warning after 510 characters:

      String literal of length 100000 exceeds maximum length 509 that C90 compilers are required to support

    0 讨论(0)
提交回复
热议问题