Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #89259
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Order of addresses of string-literals inside a translation unit |
| Date | 2023-02-23 13:52 -0800 |
| Organization | None to speak of |
| Message-ID | <87lekooxri.fsf@nosuchdomain.example.com> (permalink) |
| References | <tt55du$1h8fk$1@dont-email.me> <6pzJL.248610$PXw7.31730@fx45.iad> <tt6rto$1pkqc$3@dont-email.me> |
James Kuyper <jameskuyper@alumni.caltech.edu> 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 */
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Order of addresses of string-literals inside a translation unit Bonita Montero <Bonita.Montero@gmail.com> - 2023-02-22 14:31 +0100
Re: Order of addresses of string-literals inside a translation unit Richard Damon <Richard@Damon-Family.org> - 2023-02-22 20:34 -0500
Re: Order of addresses of string-literals inside a translation unit James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-02-23 00:00 -0500
Re: Order of addresses of string-literals inside a translation unit Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-23 13:52 -0800
Re: Order of addresses of string-literals inside a translation unit Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-02-24 09:03 -0800
Re: Order of addresses of string-literals inside a translation unit Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-02-24 08:55 -0800
Re: Order of addresses of string-literals inside a translation unit "james...@alumni.caltech.edu" <jameskuyper@alumni.caltech.edu> - 2023-02-24 09:43 -0800
Re: Order of addresses of string-literals inside a translation unit Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-03-14 08:48 -0700
Re: Order of addresses of string-literals inside a translation unit Öö Tiib <ootiib@hot.ee> - 2023-02-22 23:59 -0800
Re: Order of addresses of string-literals inside a translation unit scott@slp53.sl.home (Scott Lurndal) - 2023-02-23 16:18 +0000
csiph-web