c – Why is the string text constant?

As we all know, the C string literal is immutable, and the result of modifying the string literal is undefined. For example,

char * str = "Hello!";
str[1] ='a';

This will bring uncertain behavior.

Except for string literals Placed in static memory. So they exist in the entire program. I want to know why string literals have such attributes.

There are several different reasons.

One is to allow string literals to be stored in read-only memory (as others have already mentioned).

p>

Another method is to allow string literals to be merged. If a program uses the same string literal in several different places, it is good to allow (but not necessarily require) the compiler to merge them. So you can get multiple pointers to the same memory instead of each pointer occupying a separate memory block. This also applies when two string literals are not necessarily the same but have the same ending:

char *foo = "long string";
char *bar = "string";

In this case, bar may be foo 5 (if I If the calculation is correct).

In any case, if the string literal is allowed to be modified, it can modify other string literals that happen to have the same content. At the same time, there are not too many points to be honest – Either there are enough string literals to overlap, most people may want the compiler to run slower just to save (possibly) dozens of bytes of memory.

When writing the first standard, There are already compilers that use all three techniques (and there may be several others). Since there is no way to describe a behavior you get from modifying string literals, and no one obviously thinks it is an important support capability , So they did the obvious fact: even trying to do so will lead to undefined behavior.

As we all know, C string literals are immutable, modify the string The result of the text is undefined. For example,

char * str = "Hello!";
str[1] ='a';

pre>

This will bring indeterminate behavior.

Except string literals are placed in static memory. So they exist In the entire program. I want to know why string literals have such attributes.

There are several different reasons.

< p>One is to allow string literals to be stored in read-only memory (as others have already mentioned).

The other method is to allow string literals to be merged. If a program is If the same string literal is used in different places, it is good to allow (but not necessarily require) the compiler to merge them, so you can get multiple pointers to the same memory instead of each pointer occupying a separate Memory block. When two string literals are not necessarily the same but have the same ending, this also applies:

char *foo = "long string";
char *bar = "string";

In this case, bar may be foo 5 (if I calculate it correctly).

In either case, if allowed Modify the string literal, it can modify other string literals that happen to have the same content. At the same time, there are not too many points to be honest-either there are enough string literals to overlap, most people may want the compiler to run slower Just to save (possibly) dozens of bytes of memory or so.

When the first standard was written, there were already compilers that used all three technologies (and possibly several others). There is no way to describe a behavior you get from modifying a string literal, and no one obviously thinks it is an important support ability, so they did the obvious fact: even trying to do so will result in undefined behavior.

Leave a Comment

Your email address will not be published.