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

后端 未结 3 1695
面向向阳花
面向向阳花 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: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.)

提交回复
热议问题