Strings in static memory instances count

前端 未结 4 2336
无人及你
无人及你 2021-02-19 09:45

As I know compiletime C-like strings are kept in static memory as only one instance. For instance I got both true on gcc 4.6 running example below. But I wonder is

4条回答
  •  囚心锁ツ
    2021-02-19 10:13

    No, it's implementation dependent for both C and C++.

    C11 §6.4.5/7 String literals

    It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

    C++11 §2.14.5/12 String literals

    Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation-defined. The effect of attempting to modify a string literal is undefined.

提交回复
热议问题