pete said:
Al Bowers wrote:
The nondistinction of identical string literals,
rather than read only memory, is the reason for undefined behavior,
which the standards emphasize, by juxtaposition.
"by Definition read only"?
Why would you take such a position? This declaration has
nothing to do with identical string literals. The standard clearly
states in 6.7.8.32 on initialization
" On the other hand, the declaration
char *p = "abc";
defines p with type ‘‘pointer to char’’ and initializes it to
point to an object with type ‘‘array of char’’ with length 4
whose elements are initialized with a character string literal.
If an attempt is made to use p to modify the contents of the
array, the behavior is undefined. "
^^^^^^^^^^^^^^^^^^
C89 Last Draft
3.1.4 String literals
Semantics
"Identical string literals of either form need not be distinct.
If the program attempts to modify a string literal of either form,
the behavior is undefined."
N869
6.4.5 String literals
[#6] 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.
J.5 Common extensions
J.5.5 Writable string literals
[#1] String literals are modifiable (in which case,
identical string literals should denote distinct objects).
This is another issue which assures that it there is a common
extension of writable string literals then the implementation is
required to treat idential string literals as distinct objects.
If the implemention allows writable string literals then:
char *p1 = "Hello World";
char *p2 = "Hello World";
p1 and p2 points to seperate objects such that if you modify one
you will not modify the other.
If the implementation does not allow modification of string literals
then in the above declarations, p1 and p2 may point to the same object.
The point is there is no definition that requires read only string
literals. The implementation is free to make them read only or allow
modifications. Of course, from the point of view of writing portable
code, one should aways treat string literals as read only.