Path: csiph.com!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: Order of addresses of string-literals inside a translation unit Date: Thu, 23 Feb 2023 13:52:17 -0800 Organization: None to speak of Lines: 54 Message-ID: <87lekooxri.fsf@nosuchdomain.example.com> References: <6pzJL.248610$PXw7.31730@fx45.iad> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: reader01.eternal-september.org; posting-host="a6bb8fd1960f980f53a9015c286413ef"; logging-data="2083893"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18yUAtn/lldQoE8Y7WPWdLT" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:+cgWztA8r/nAnXhlv0/kp3jzvJE= sha1:geQ9A/PW45YzIt3uuNtJXSYmSz4= Xref: csiph.com comp.lang.c++:89259 James Kuyper writes: > On 2/22/23 20:34, Richard Damon wrote: >> On 2/22/23 8:31 AM, Bonita Montero wrote: >>> Is there a mandated order of addresses of string-literals inside a >>> translation unit ? Or is there only the known requirement of order >>> of initialization inside a translation unit of objects with a con- >>> structor; because for that it wouldn't make a difference if the >>> order of addresses would be different from the order of initiali- >>> zation. >> >> As far as I know, there is NO promises about the order in memory of >> objects declared in a program. The order of construction within a >> translation unit is specified, but not their physical order. >> >> Sub-object within a large object (like array elements in an array, or >> members within a class/struct have some specification). >> >> The compiler is allowed to optimize string literals and thus put them in >> a different order. > > 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. That's true, but not just because string literals are read-only (and const, unlike in C). The standard *could* have required that two occurrences of "hello" in the same program must refer to distinct objects with unequal addresses (and code could have taken advantage of that guarantee). But in fact it explicitly says that it's unspecified. C++17 5.13.5p16 [lex.string]: Evaluating a string-literal results in a string literal object with static storage duration, initialized from the given characters as specified above. Whether all string literals are distinct (that is, are stored in nonoverlapping objects) and whether successive evaluations of a string-literal yield the same or a different object is unspecified. [ Note: The effect of attempting to modify a string literal is undefined. — end note ] I hadn't realized until now that the standard allows two evaluations of the same string literal to refer to distinct objects (and it's difficult to imagine an implementation choice that would make them distinct). -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for XCOM Labs void Void(void) { Void(); } /* The recursive call of the void */