> On Friday, February 24, 2023 at 11:55:33?AM UTC-5, Tim Rentsch wrote:
>
>> James Kuyper <
james...@alumni.caltech.edu> writes:
>>
>>> Note, in particular, that because writing to the memory allocated to
>>> store the string corresponding to a string literal has undefined
>>> behavior, it's entirely permissible for an implementation to make all
>>> occurrences of "Hello, world!" in a given program point at the same
>>> location in memory, and to make "Hello, world!" + 7 == "world!". Both
>>> optimizations have actually been implemented by many implementations.
>>
>> It's true that two or more string literals may have some bytes in
>> common, but that's not because writing to a byte in a string
>> literal has undefined behavior; it's because there is an explicit
>> statement in the C++ standard (and also the C standard) that allows
>> it. If anything the implication goes the other direction: because
>> pre-standard C implementations stored multiple string literals in
>> the same memory, when C was standardized it was pretty much a
>> necessity that storing into the bytes of a string literal had to
>> be undefined behavior.
>
> True. But the fact that storing into the bytes of a string literal
> has undefined behavior is what makes storing multiple string
> literals in overlapping memory workable. We're really saying the
> same thing, from two different points of view.
mine has it going in the opposite direction. I'm hard pressed to