| Deutsch English Français Italiano |
|
<87ldvgk7b3.fsf@nosuchdomain.example.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: So You Think You Can Const?
Date: Sat, 11 Jan 2025 20:03:12 -0800
Organization: None to speak of
Lines: 31
Message-ID: <87ldvgk7b3.fsf@nosuchdomain.example.com>
References: <vljvh3$27msl$1@dont-email.me> <vlma9m$2s5e5$1@dont-email.me>
<vlo0cm$2dkpd$9@dont-email.me> <vlqd4j$3s4ai$1@dont-email.me>
<20250110103252.718@kylheku.com> <vlu6qh$miie$1@dont-email.me>
<20250111182556.466@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Sun, 12 Jan 2025 05:03:17 +0100 (CET)
Injection-Info: dont-email.me; posting-host="e09f1c48d05ea16eba68b16586b9febd";
logging-data="1076649"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19MU1y8xXph5qYl4xsgsvOp"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:vqrzPVzU3k/DmUp4ztIWkUkcq4Y=
sha1:0KSI9+8scSN2hIxZHx0IeP2/WZE=
Bytes: 2578
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.com
void Void(void) { Void(); } /* The recursive call of the void */