I believe that in C99, modification of string literals is undefined behaviour. I don't have a copy of that standard but I do have a draft of C1X (n1570) which states in 6.4.5 paragraph 7:
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.
I have found a Stack Overflow question that touches on this topic and contains the following comment from Jonathan Leffler:
Originally, the C89 (C90) standard did not outlaw modifying literals because there was too much code written before the standard that would be broken by it.
But I have also seen lots of discussion of the type of string literals and the fact that they are char[N]
and not const char[N]
. I gather that this decision was taken so that the large body of existing code would not break.
Can anyone give me a definitive answer. Is string literal modification UB in C89?
The behavior is undefined if a program attempts to modify any portion of a string literal. Modifying a string literal frequently results in an access violation because string literals are typically stored in read-only memory.
A string literal with the prefix L is a wide string literal. A string literal without the prefix L is an ordinary or narrow string literal. The type of narrow string literal is array of char .
The valid string literals for the linkage specification to call programs are: "OS" OS linkage call. "OS nowiden"
There are three sets of literal types available in TypeScript today: strings, numbers, and booleans; by using literal types you can allow an exact value which a string, number, or boolean must have.
Yes, they are non-modifiable in C89.
(C90, 6.1.4) "If the program attempts to modify a string literal of either form, the behavior is undefined"
Even in K&R 2nd edition, there are quotes regarding the immutability of string literals.
(K&R2, 5.5) "the result is undefined if you try to modify the string contents"
(K&R2, Appendix C) "Strings are no longer modifiable, and so may be placed in read-only memory"
In the ANSI C89 Rationale, there is an explanation of why it is non-modifiable:
(ANSI C89 Rationale, 3.1.4) "String literals are specified to be unmodifiable. This specification allows implementations to share copies of strings with identical text, to place string literals in read-only memory, and perform certain optimizations."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With