Deutsch   English   Français   Italiano  
<867c75829j.fsf@linuxsc.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder9.news.weretis.net!news.quux.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups: comp.lang.c
Subject: Re: So You Think You Can Const?
Date: Wed, 08 Jan 2025 12:43:52 -0800
Organization: A noiseless patient Spider
Lines: 64
Message-ID: <867c75829j.fsf@linuxsc.com>
References: <vljvh3$27msl$1@dont-email.me> <20250107130809.661@kylheku.com> <vlm0hf$2dkpd$1@dont-email.me> <87a5c15ob0.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Wed, 08 Jan 2025 21:43:52 +0100 (CET)
Injection-Info: dont-email.me; posting-host="6e19b66cb0c542b6b8c649f7413a0cd9";
	logging-data="3071468"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX197eHqzNzd83sMTpwh2igVf7m0GbhlCgfQ="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:FJGK2VI4cLLPDDy7FhurdNJV0GM=
	sha1:uOns+nwGaX9Ey1pypIT2iDB4Z3M=
Bytes: 3367

Ben Bacarisse <ben@bsb.me.uk> writes:

> Julio Di Egidio <julio@diegidio.name> writes:
>
>> On 07/01/2025 23:11, Kaz Kylheku wrote:
>>
>>> On 2025-01-07, Julio Di Egidio <julio@diegidio.name> wrote:
>>
>> <snipped>
>>
>>>> In particular, I am using C90, and compiling with
>>>> `gcc ... -ansi -pedantic -Wall -Wextra` (as I have
>>>> the requirement to ideally support any device).
>>>>
>>>> To the question, I was reading this, but I am not
>>>> sure what the quoted passage means:
>>>>
>>>> Matt Stancliff, "So You Think You Can Const?",
>>>> <https://matt.sh/sytycc>
>>>> << Your compiler, at its discretion, may also choose
>>>>      to place any const declarations in read-only storage,
>>>>      so if you attempt to hack around the const blocks,
>>>>      you could get undefined behavior. >>
>>>
>>> An object defined with a type that is const-qualified
>>> could be put into write-protected storage.
>>
>> What do you/we mean by "object" in this context?  (Sorry, I do have
>> forgotten, the glossary to begin with.)
>
> An object (in C) is a contiguous region of storage, the contents of
> which can represent values.
>
>> Overall, I am surmising this and only this might go write-protected:
>>
>>   MyStruct_t const T = {...};
>
> Yes, though you should extend your concern beyond what might be
> write-protected.  Modifying an object whose type is const qualified
> is undefined, even if the object is in writable storage.  A compiler
> may assume that such an object has not changed because in a program
> that has undefined behaviour, all bets are off.  [...]

We need to be careful about what is being asserted here.  There
are cases where a compiler may not assume that a const object
has not changed, despite the rule that assigning to a const
object is undefined behavior:

    #include <stdio.h>
    typedef union { const int foo; int bas; } Foobas;

    int
    main(){
     Foobas  fb     =  { 0 };

        printf( "  fb.foo is %d\n", fb.foo );
        fb.bas = 7;
        printf( "  fb.foo is %d\n", fb.foo );
        return  0;
    }

The object fb.foo is indeed a const object, but an access of
fb.foo must not assume that it retains its original value after
the assignment to fb.bas.