Sujet : Re: So You Think You Can Const?
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 12. Jan 2025, 05:03:12
Autres entêtes
Organisation : None to speak of
Message-ID : <87ldvgk7b3.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6 7
User-Agent : Gnus/5.13 (Gnus v5.13)
Kaz Kylheku <
643-408-1753@kylheku.com> writes:
On 2025-01-11, Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
[...]
In C and C++ (as well in virtually all higher level languages)
"constness" is not a physical concept.
>
It is for defined objects. A const object of static duration can be
put into ROM, or virtual memory configured read-only. Either of these
can generate a trap if written.
True, but a conforming implementation could put all objects into
read/write memory. Attempting to modify a const-qualified object
still has undefined behavior, but is likely to "work", unless the
compiler performs optimizations based on the assumption that such
an object won't be modified.
For that matter, a compiler can put a non-const object into ROM if
it can determine that the program never attempts to modify it.
(As far as I know that's not a common optimization.)
All the standard really says about "const" is that directly modifying
a const-qualified object is a constraint violation, and indirectly
modifying it has undefined behavior. This gives compilers some
additional freedom in how to treat such objects (storing them in
ROM, for example) *and* provides some information that can be used
in optimization (e.g., not reloading a value from memory because
it can't have changed).
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */