Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Experiments must be reproducible; they should all fail in the same way.


devel / comp.lang.c / Re: Do you insist on const-correctness?

SubjectAuthor
* Do you insist on const-correctness?Anton Shepelev
+- Re: Do you insist on const-correctness?Anton Shepelev
+* Re: Do you insist on const-correctness?Ben Bacarisse
|`* Re: Do you insist on const-correctness?Anton Shepelev
| +* Re: Do you insist on const-correctness?David Brown
| |`* Re: Do you insist on const-correctness?David Brown
| | `* Re: Do you insist on const-correctness?comp.lang.c
| |  `* Re: Do you insist on const-correctness?David Brown
| |   +* Re: Do you insist on const-correctness?Keith Thompson
| |   |`- Re: Do you insist on const-correctness?Chris M. Thomasson
| |   +- Re: Do you insist on const-correctness?Scott Lurndal
| |   +* Re: Do you insist on const-correctness?David Brown
| |   |`- Re: Do you insist on const-correctness?Chris M. Thomasson
| |   `- Re: Do you insist on const-correctness?Keith Thompson
| `* Re: Do you insist on const-correctness?Ben Bacarisse
|  `- Re: Do you insist on const-correctness?Anton Shepelev
+* Re: Do you insist on const-correctness?Tim Rentsch
|`* Re: Do you insist on const-correctness?Anton Shepelev
| +* Re: Do you insist on const-correctness?David Brown
| |+* Re: Do you insist on const-correctness?Scott Lurndal
| ||+* Re: Do you insist on const-correctness?Kaz Kylheku
| |||`* Re: Do you insist on const-correctness?Bart
| ||| +* Re: Do you insist on const-correctness?Kaz Kylheku
| ||| |`* Re: Do you insist on const-correctness?Bart
| ||| | +- Re: Do you insist on const-correctness?Chris M. Thomasson
| ||| | +- Re: Do you insist on const-correctness?Kaz Kylheku
| ||| | +- Re: Do you insist on const-correctness?Keith Thompson
| ||| | `- Re: Do you insist on const-correctness?David Brown
| ||| +* Re: Do you insist on const-correctness?David Brown
| ||| |`* Re: Do you insist on const-correctness?Bart
| ||| | +- Re: Do you insist on const-correctness?Ben Bacarisse
| ||| | +- Re: Do you insist on const-correctness?Keith Thompson
| ||| | +* Re: Do you insist on const-correctness?Kaz Kylheku
| ||| | |`* Re: Do you insist on const-correctness?Chris M. Thomasson
| ||| | | `* Re: Do you insist on const-correctness?Keith Thompson
| ||| | |  `* Re: Do you insist on const-correctness?Chris M. Thomasson
| ||| | |   `* Re: Do you insist on const-correctness?Keith Thompson
| ||| | |    `- Re: Do you insist on const-correctness?Chris M. Thomasson
| ||| | `* Re: Do you insist on const-correctness?David Brown
| ||| |  `* Re: Do you insist on const-correctness?Bart
| ||| |   `* Re: Do you insist on const-correctness?David Brown
| ||| |    `* Re: Do you insist on const-correctness?Bart
| ||| |     +- Re: Do you insist on const-correctness?Keith Thompson
| ||| |     +- Re: Do you insist on const-correctness?Kaz Kylheku
| ||| |     `- Re: Do you insist on const-correctness?David Brown
| ||| +- Re: Do you insist on const-correctness?Keith Thompson
| ||| `* Re: Do you insist on const-correctness?James Kuyper
| |||  `* Re: Do you insist on const-correctness?Bart
| |||   `* Re: Do you insist on const-correctness?David Brown
| |||    `* Re: Do you insist on const-correctness?Kaz Kylheku
| |||     `- Re: Do you insist on const-correctness?Chris M. Thomasson
| ||`* Re: Do you insist on const-correctness?David Brown
| || `* Re: Do you insist on const-correctness?Chris M. Thomasson
| ||  `* Re: Do you insist on const-correctness?Chris M. Thomasson
| ||   `* Re: Do you insist on const-correctness?Ben Bacarisse
| ||    `* Re: Do you insist on const-correctness?Chris M. Thomasson
| ||     `* Re: Do you insist on const-correctness?Ben Bacarisse
| ||      `- Re: Do you insist on const-correctness?Chris M. Thomasson
| |+* Re: Do you insist on const-correctness?Kaz Kylheku
| ||`* Re: Do you insist on const-correctness?David Brown
| || `* Re: Do you insist on const-correctness?Anton Shepelev
| ||  `* Re: Do you insist on const-correctness?David Brown
| ||   `* Re: Do you insist on const-correctness?Anton Shepelev
| ||    `- Re: Do you insist on const-correctness?David Brown
| |`* Re: Do you insist on const-correctness?Anton Shepelev
| | +- Re: Do you insist on const-correctness?Bart
| | +- Re: Do you insist on const-correctness?Richard Damon
| | +* Re: Do you insist on const-correctness?Scott Lurndal
| | |`- Re: Do you insist on const-correctness?Anton Shepelev
| | `* Re: Do you insist on const-correctness?David Brown
| |  +* Re: Do you insist on const-correctness?Bart
| |  |`* Re: Do you insist on const-correctness?David Brown
| |  | `* Re: Do you insist on const-correctness?Bart
| |  |  +* Re: Do you insist on const-correctness?Chris M. Thomasson
| |  |  |`* Re: Do you insist on const-correctness?Bart
| |  |  | +* Re: Do you insist on const-correctness?Chris M. Thomasson
| |  |  | |`- Re: Do you insist on const-correctness?Bart
| |  |  | +* Re: Do you insist on const-correctness?David Brown
| |  |  | |`* Re: Do you insist on const-correctness?Chris M. Thomasson
| |  |  | | +- Re: Do you insist on const-correctness?Kaz Kylheku
| |  |  | | `* Re: Do you insist on const-correctness?David Brown
| |  |  | |  `* Re: Do you insist on const-correctness?Chris M. Thomasson
| |  |  | |   `- Re: Do you insist on const-correctness?Chris M. Thomasson
| |  |  | `* Re: Do you insist on const-correctness?Anton Shepelev
| |  |  |  +* Re: Do you insist on const-correctness?Anton Shepelev
| |  |  |  |`- Re: Do you insist on const-correctness?David Brown
| |  |  |  +- Re: Do you insist on const-correctness?Bart
| |  |  |  +* Re: Do you insist on const-correctness?Dan Cross
| |  |  |  |`* Re: Do you insist on const-correctness?Anton Shepelev
| |  |  |  | `* Re: Do you insist on const-correctness?Dan Cross
| |  |  |  |  `* Re: Do you insist on const-correctness?Scott Lurndal
| |  |  |  |   `- Re: Do you insist on const-correctness?Dan Cross
| |  |  |  `* Re: Do you insist on const-correctness?David Brown
| |  |  |   +* Re: Do you insist on const-correctness?Bart
| |  |  |   |+* Re: Do you insist on const-correctness?Kaz Kylheku
| |  |  |   ||+* Re: Do you insist on const-correctness?Bart
| |  |  |   |||`* Re: Do you insist on const-correctness?David Brown
| |  |  |   ||| +- Re: Do you insist on const-correctness?Anton Shepelev
| |  |  |   ||| `* Re: Do you insist on const-correctness?Bart
| |  |  |   |||  `* Re: Do you insist on const-correctness?David Brown
| |  |  |   |||   `* Re: Do you insist on const-correctness?Ben Bacarisse
| |  |  |   ||`* Re: Do you insist on const-correctness?David Brown
| |  |  |   |`- Re: Do you insist on const-correctness?David Brown
| |  |  |   +* Re: Do you insist on const-correctness?Anton Shepelev
| |  |  |   +* Re: Do you insist on const-correctness?David Brown
| |  |  |   `- Re: Do you insist on const-correctness?Chris M. Thomasson
| |  |  `- Re: Do you insist on const-correctness?David Brown
| |  `* Re: Do you insist on const-correctness?Anton Shepelev
| `* Re: Do you insist on const-correctness?Tim Rentsch
+* Re: Do you insist on const-correctness?Kaz Kylheku
`- Re: Do you insist on const-correctness?Tim Rentsch

Pages:12345678
Re: Do you insist on const-correctness?

<ueae1i$1uk16$5@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29156&group=comp.lang.c#29156

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Mon, 18 Sep 2023 14:04:50 -0700
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <ueae1i$1uk16$5@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<uea52i$1stu4$2@dont-email.me> <ueadvt$1uk16$4@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 18 Sep 2023 21:04:50 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="dbd0edff1a203ab30b2e978110ba1d77";
logging-data="2052134"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19SrefTKbBYQA6AhWDEJyIY2eDIRTj6T0c="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:D1u683WxP6IA+xWzyAktlksQBGs=
In-Reply-To: <ueadvt$1uk16$4@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Mon, 18 Sep 2023 21:04 UTC

On 9/18/2023 2:03 PM, Chris M. Thomasson wrote:
> On 9/18/2023 11:31 AM, David Brown wrote:
>> On 18/09/2023 17:54, Scott Lurndal wrote:
>>> David Brown <david.brown@hesbynett.no> writes:
>>>> On 18/09/2023 10:53, Anton Shepelev wrote:
>>>
>>>> * Variables can often be declared "const".  This leads to clearer code
>>>> that is easier to reason about - things don't change, and you only have
>>>> to look at one line to see the value that the variable has.  For
>>>> non-const variables, you have to look throughout their lifetimes - this
>>>> is maximally bad if they are defined at the top of the function.
>>>
>>> I would add that declaring variables const gives the optimization pass
>>> additional information and may result in more efficient register
>>> allocation and code generation.
>>>
>>>      const uint32_t regnum = bit::extract(instruction, 4, 0);
>>>
>>> also enhances readability.
>>
>> I agree that adding "const" improves readability, because it tells the
>> human reader that the thing never changes.
>>
>> But it doesn't help the compiler, since the compiler can read the rest
>> of the function and see that it does not change.  (So could a human
>> reader, but I care about saving the human reader the effort of having
>> to do that - I don't care about saving the compiler effort!)
>>
>>
>
> const as in:
>
> void
> ct_foo(
>   ct_foobar* const self
> ){
>    // self cannot be changed... So be it!
> }
>
> Kosher? ;^)

What about:

void
ct_foo(
ct_foobar const* const self
){
// self cannot be changed... So be it!
// the ct_foobar that self points to is const...
}

:^)

Re: Do you insist on const-correctness?

<87a5tjkx66.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29157&group=comp.lang.c#29157

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Mon, 18 Sep 2023 14:07:29 -0700
Organization: None to speak of
Lines: 53
Message-ID: <87a5tjkx66.fsf@nosuchdomain.example.com>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<20230918110915.139@kylheku.com> <uea5bd$1t2iu$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="429dd8307f13cb680980e2c637354176";
logging-data="2054018"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18oe+/NovY0Zse3ZFGsJR5X"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:i20FxB/Nd3/ACtzN6sfcxnANx0w=
sha1:6qbHQFB/JRzpwT9pG42y4Z0xcl0=
 by: Keith Thompson - Mon, 18 Sep 2023 21:07 UTC

Bart <bc@freeuk.com> writes:
> On 18/09/2023 19:10, Kaz Kylheku wrote:
>> On 2023-09-18, Scott Lurndal <scott@slp53.sl.home> wrote:
>>> David Brown <david.brown@hesbynett.no> writes:
>>>> On 18/09/2023 10:53, Anton Shepelev wrote:
>>>
>>>> * Variables can often be declared "const". This leads to clearer code
>>>> that is easier to reason about - things don't change, and you only have
>>>> to look at one line to see the value that the variable has. For
>>>> non-const variables, you have to look throughout their lifetimes - this
>>>> is maximally bad if they are defined at the top of the function.
>>>
>>> I would add that declaring variables const gives the optimization pass
>>> additional information
>> Declaring variables const provides no additional optimizing info
>> over
>> just not assigning to the variable and not taking its address.
>>
>
> If you take a program like this:
>
> typedef struct {int d,m,y;} date;
>
> void fred(date d) {}
> void bill(const date d) {}
>
> int main(void) {
> date dd;
>
> fred(dd);
> bill(dd);
>
> On a modern ABI where the struct is passed internally by reference (if
> not, make it bigger), a compiler may be obliged to copy the struct to
> a temporary location when calling fred(), but doesn't need to do so
> when calling bill(), if it trusts that 'const' qualifier.
[...]

Interesting point, but I doubt that any compilers do this.

The compiler can assume the parameter object is not modified
only if the *definition* is visible, as it is in your example.
A standalone declaration with "const" on a parameter gives no
information about whether the parameter is const in the definition.
And a compiler could inline the call to bill(), particularly if
there's only one call. Once it's inlined, the compiler can see
whether d is actually modified. (Multiple calls and/or external
visibility make inlining more difficult.)

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Re: Do you insist on const-correctness?

<87pm2fi3mf.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29158&group=comp.lang.c#29158

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Mon, 18 Sep 2023 22:16:24 +0100
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <87pm2fi3mf.fsf@bsb.me.uk>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<20230918110915.139@kylheku.com> <uea5bd$1t2iu$1@dont-email.me>
<ueaa7e$1tugo$1@dont-email.me> <ueadqt$1uhr4$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="4a1f049c3c0d7d3aa42e2e5e10f8a54b";
logging-data="2055444"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+c5e6/bMMpdUJUANliPKuTVmnfnBinplA="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:sVzsKWrDTtSJskL6flYoOtnPwLk=
sha1:SS2NWtFFo7WfrjsJPeurtdlVYwU=
X-BSB-Auth: 1.156056f0dbd7fbff1d87.20230918221624BST.87pm2fi3mf.fsf@bsb.me.uk
 by: Ben Bacarisse - Mon, 18 Sep 2023 21:16 UTC

Bart <bc@freeuk.com> writes:

> So, basically, 'const' can never be trusted?

No.

> If you have this:
>
> extern void F(void);
> const int abc=100;
>
> int main(void) {
>
> F();
> abc;
>
> Then this point, a compiler can never assume that 'abc' still has the value
> '100' at this point?

The object has a const-qualified type. It can not be altered except in
a way that results in undefined behaviour meaning that a compiler is
allowed to assume that it has not changed. (Of course, it also means
that a compiler can ignore the const if that makes sense for that
particular implementation.)

--
Ben.

Re: Do you insist on const-correctness?

<20230918142049.635@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29159&group=comp.lang.c#29159

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Mon, 18 Sep 2023 21:26:12 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 101
Message-ID: <20230918142049.635@kylheku.com>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<20230918110915.139@kylheku.com> <uea5bd$1t2iu$1@dont-email.me>
<20230918125058.653@kylheku.com> <ueadjl$1uhr4$1@dont-email.me>
Injection-Date: Mon, 18 Sep 2023 21:26:12 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6bfaa870470b0ebd2b50ab678df28ccf";
logging-data="2061383"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+McmO+T0wzhw5vSm64d7ag295rXBAId/Q="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:q9ewa6Dk6YxoPQ/wahSvmlbAr3g=
 by: Kaz Kylheku - Mon, 18 Sep 2023 21:26 UTC

On 2023-09-18, Bart <bc@freeuk.com> wrote:
> On 18/09/2023 20:55, Kaz Kylheku wrote:
>> On 2023-09-18, Bart <bc@freeuk.com> wrote:
>>> On 18/09/2023 19:10, Kaz Kylheku wrote:
>>>> On 2023-09-18, Scott Lurndal <scott@slp53.sl.home> wrote:
>>>>> David Brown <david.brown@hesbynett.no> writes:
>>>>>> On 18/09/2023 10:53, Anton Shepelev wrote:
>>>>>
>>>>>> * Variables can often be declared "const". This leads to clearer code
>>>>>> that is easier to reason about - things don't change, and you only have
>>>>>> to look at one line to see the value that the variable has. For
>>>>>> non-const variables, you have to look throughout their lifetimes - this
>>>>>> is maximally bad if they are defined at the top of the function.
>>>>>
>>>>> I would add that declaring variables const gives the optimization pass
>>>>> additional information
>>>>
>>>> Declaring variables const provides no additional optimizing info over
>>>> just not assigning to the variable and not taking its address.
>>>>
>>>
>>> If you take a program like this:
>>>
>>> typedef struct {int d,m,y;} date;
>>>
>>> void fred(date d) {}
>>> void bill(const date d) {}
>>
>> The const here makes no difference.
>
> Well, yeah, I changed those {} to ; precisely because someone would say
> that, but I forgot to repaste it.
>
> In any case, in such examples, you have to assume the the contents of {}
> could be anything, not literally empty bodies.
>
>> Since this is a definition, the compiler can inline the function, and
>> make it disappear, since it does nothing.
>>
>>> int main(void) {
>>> date dd;
>>>
>>> fred(dd);
>>> bill(dd);
>>>
>>> On a modern ABI where the struct is passed internally by reference (if
>>> not, make it bigger), a compiler may be obliged to copy the struct to a
>>> temporary location when calling fred(), but doesn't need to do so when
>>> calling bill(), if it trusts that 'const' qualifier.
>>
>> Suppose that bill is an external function, which is declared like
>> this:
>>
>> void bill(const date d);
>>
>> that const means nothing and can (really, should) be ignored.
>>
>> The function definition can look like:
>>
>> void bill(date d) { /*...*/ }
>>
>> or like:
>>
>> void bill(const date d) { /*...*/ }
>>
>> both of these are correct, and their ABI must be exactly the same.
>>
>>> Although this is nothing really to do with optimisation, just common sense.
>>
>> Common sense is no substitute for 1) documentation and 2) empirically
>> knowing how things are made by people who follow (1).
>
> So what would you do if generating code for:
>
> bill(dd);
>
> when the body of bill() is not visible; would you make a copy of the
> struct of not?

If I had make sure that it's compatible with another compiler or
platform ABI, then I would research into their rules.

If I could implement it from scratch, I would probably pass very small
structures as several register arguments, if registers are available
in light of other arguments. In other cases by address.

Since that address is never declared as a pointer argument, const
doesn't enter into the picture.

It has to look like pass-by-value, so the caller cannot modify the
object. The function definition is compiled such that if the structure
is modified, or its address is taken such that we cannot prove that it's
not modified elsewhere, then we generate code to make a copy. The stack
frame is extended by enough space to provide the storage for the copy,
etc.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Do you insist on const-correctness?

<87jzsni350.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29160&group=comp.lang.c#29160

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Mon, 18 Sep 2023 22:26:51 +0100
Organization: A noiseless patient Spider
Lines: 74
Message-ID: <87jzsni350.fsf@bsb.me.uk>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<uea52i$1stu4$2@dont-email.me> <ueadvt$1uk16$4@dont-email.me>
<ueae1i$1uk16$5@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="4a1f049c3c0d7d3aa42e2e5e10f8a54b";
logging-data="2055444"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/0npawZRe09zpbPap5xjT/kuO79i4yXRw="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:2Hc32OPpHNAvzlyYKwDsZeb3KKc=
sha1:Zr//HOPje/B/PcmoqucfVFtbs6M=
X-BSB-Auth: 1.1b99ae69b643a5595bfd.20230918222651BST.87jzsni350.fsf@bsb.me.uk
 by: Ben Bacarisse - Mon, 18 Sep 2023 21:26 UTC

"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:

> On 9/18/2023 2:03 PM, Chris M. Thomasson wrote:
>> On 9/18/2023 11:31 AM, David Brown wrote:
>>> On 18/09/2023 17:54, Scott Lurndal wrote:
>>>> David Brown <david.brown@hesbynett.no> writes:
>>>>> On 18/09/2023 10:53, Anton Shepelev wrote:
>>>>
>>>>> * Variables can often be declared "const".  This leads to clearer code
>>>>> that is easier to reason about - things don't change, and you only have
>>>>> to look at one line to see the value that the variable has.  For
>>>>> non-const variables, you have to look throughout their lifetimes - this
>>>>> is maximally bad if they are defined at the top of the function.
>>>>
>>>> I would add that declaring variables const gives the optimization pass
>>>> additional information and may result in more efficient register
>>>> allocation and code generation.
>>>>
>>>>      const uint32_t regnum = bit::extract(instruction, 4, 0);
>>>>
>>>> also enhances readability.
>>>
>>> I agree that adding "const" improves readability, because it tells the
>>> human reader that the thing never changes.
>>>
>>> But it doesn't help the compiler, since the compiler can read the rest
>>> of the function and see that it does not change.  (So could a human
>>> reader, but I care about saving the human reader the effort of having to
>>> do that - I don't care about saving the compiler effort!)
>>>
>>>
>> const as in:
>> void
>> ct_foo(
>>   ct_foobar* const self
>> ){
>>    // self cannot be changed... So be it!
>> }
>> Kosher? ;^)

....cannot be changed without undefined behaviour.

> What about:
>
> void
> ct_foo(
> ct_foobar const* const self
> ){
> // self cannot be changed... So be it!
> // the ct_foobar that self points to is const...
> }

Technically, no. The ct_foobar that self points to can't be changed
using the self pointer directly, but the ct_foobar object itself might
be const or it might not be, and it might even be able to be changed by
the function in question:

void ct_eg(const ct_foobar *self, ct_foobar *other_foobar)
{ ...
change_this_thing_beyond_recognition(other_foobar);
...
}

and later...

ct_foobar my_obj;
...
ct_eg(&my_obj, &my_obj);

This is (in part) what restrict-qualified pointers are for.

--
Ben.

Re: Do you insist on const-correctness?

<875y47ksdg.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29162&group=comp.lang.c#29162

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Mon, 18 Sep 2023 15:51:07 -0700
Organization: None to speak of
Lines: 33
Message-ID: <875y47ksdg.fsf@nosuchdomain.example.com>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<20230918110915.139@kylheku.com> <uea5bd$1t2iu$1@dont-email.me>
<ueaa7e$1tugo$1@dont-email.me> <ueadqt$1uhr4$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="db989aa286e51ccd0458447dc5251e4d";
logging-data="2089183"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+n68ZqmjjrBYugrNUWgOTE"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:0cXPszRUso9egl30nElQ8a+YkLs=
sha1:bqSR/8wkgdoXAAEgliiDuUHuNX4=
 by: Keith Thompson - Mon, 18 Sep 2023 22:51 UTC

Bart <bc@freeuk.com> writes:
> On 18/09/2023 20:59, David Brown wrote:
[...]
>>   It is legal
>> for program to cast a pointer-to-const to a pointer-to-non-const,
>> and use that to change the value, ***as long as the original object was
>> not defined as "const"***.

Emphasis added.

> So, basically, 'const' can never be trusted?

No.

> If you have this:
>
> extern void F(void);
> const int abc=100;
>
> int main(void) {
>
> F();
> abc;
>
> Then this point, a compiler can never assume that 'abc' still has the
> value '100' at this point?

No, see above. The original object was defined as "const".

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Re: Do you insist on const-correctness?

<871qevks5n.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29163&group=comp.lang.c#29163

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.hispagatos.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Mon, 18 Sep 2023 15:55:48 -0700
Organization: None to speak of
Lines: 37
Message-ID: <871qevks5n.fsf@nosuchdomain.example.com>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<20230918110915.139@kylheku.com> <uea5bd$1t2iu$1@dont-email.me>
<20230918125058.653@kylheku.com> <ueadjl$1uhr4$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="db989aa286e51ccd0458447dc5251e4d";
logging-data="2089183"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/KK54iu9fO6lQUJalevn0x"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:+oexPWS6dK2BQKIJH2F/EX7nfW8=
sha1:94Dy+p1mfU/SP+Lwo2d5i29Hr/4=
 by: Keith Thompson - Mon, 18 Sep 2023 22:55 UTC

Bart <bc@freeuk.com> writes:
> On 18/09/2023 20:55, Kaz Kylheku wrote:
>> On 2023-09-18, Bart <bc@freeuk.com> wrote:
[...]
>>> If you take a program like this:
>>>
>>> typedef struct {int d,m,y;} date;
>>>
>>> void fred(date d) {}
>>> void bill(const date d) {}
>> The const here makes no difference.
>
> Well, yeah, I changed those {} to ; precisely because someone would
> say that, but I forgot to repaste it.
>
> In any case, in such examples, you have to assume the the contents of
> {} could be anything, not literally empty bodies.
[...]

You posted valid code (with two empty function bodies), and it's
reasonable to assume that you were referring to that exact code.
If you want to indicate that the bodies might not be empty, I
suggest something like:
void fred(date d) {...}
void bill(const date d) {...}
or
void fred(date d) { /* ... */ }
void bill(const date d) { /* ... */ }

Of course if you had posted with ; rather than {} as you intended, that
would have been different -- but we can only see what you actually post,
not what you mean.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Re: Do you insist on const-correctness?

<20230918142634.194@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29164&group=comp.lang.c#29164

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Mon, 18 Sep 2023 22:59:02 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <20230918142634.194@kylheku.com>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<20230918110915.139@kylheku.com> <uea5bd$1t2iu$1@dont-email.me>
<ueaa7e$1tugo$1@dont-email.me> <ueadqt$1uhr4$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 18 Sep 2023 22:59:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="78a1cbf526ee5626f6ff8689cdc8787c";
logging-data="2097007"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+3O5Ai6qJsKzm0wLYWSUll3DJapCqJhNA="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:V6PVFXqaT5n5aA0pbnPvxiYffhk=
 by: Kaz Kylheku - Mon, 18 Sep 2023 22:59 UTC

On 2023-09-18, Bart <bc@freeuk.com> wrote:
> On 18/09/2023 20:59, David Brown wrote:
>> The compiler can't trust the "const" qualifier.  (Well, it can in this
>> case where it can see the definition of "bill" - but I assume you meant
>> when the function was defined externally.)  It is legal for program to
>> cast a pointer-to-const to a pointer-to-non-const, and use that to
>> change the value, as long as the original object was not defined as
>> "const".
>
> So, basically, 'const' can never be trusted?

A const qualifier on a function parameter (the parameter itself,
not what it points to) means nothing as far as declaring anything
to a caller.

It's active in definitions. In a function definition, parameters are
local variables, and that's how you make the parameter const.

It can be repeated in the declaration, but means nothing.

ISO C says something like thaat for the purposes of determining whether
two function types are compatible, qualifiers on the parameters are
ignored.

These two pointers have exactly the same type:

void (*p1)(const int);
void (*p2)(int);

You shouldn't put const qualifiers on parameters in declarations; since
they are ignored, it can only lead to confusion on the part of someone
who doesn't know about this quirk or has forgotten about it.

> If you have this:
>
> extern void F(void);
> const int abc=100;
>
> int main(void) {
>
> F();
> abc;
>
> Then this point, a compiler can never assume that 'abc' still has the
> value '100' at this point?

It can; abc isn't a parameter involved in determining function
compatibility, so that isn't a situation in which const is ignored.

Here the const means that assigning a value to abc is a constraint
violation, and modifying abc in some underhanded way like

*(int *)(&abc) = 0;

is undefined behavior. The compiler can assue that abc holds 100 on the
principle of assuming that no undefined behavior took place.

Re: Do you insist on const-correctness?

<ueakp8$1vvcp$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29165&group=comp.lang.c#29165

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Mon, 18 Sep 2023 15:59:51 -0700
Organization: A noiseless patient Spider
Lines: 90
Message-ID: <ueakp8$1vvcp$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<uea52i$1stu4$2@dont-email.me> <ueadvt$1uk16$4@dont-email.me>
<ueae1i$1uk16$5@dont-email.me> <87jzsni350.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 18 Sep 2023 22:59:52 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e823050642b27d6b26a63385ba2f7eca";
logging-data="2096537"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/xjfYh7IdDq+FtVAxkOb0jbEVrprdZZ2Q="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:u2J3dmz6/5QiltwJ1Qynd9hAOkQ=
In-Reply-To: <87jzsni350.fsf@bsb.me.uk>
Content-Language: en-US
 by: Chris M. Thomasson - Mon, 18 Sep 2023 22:59 UTC

On 9/18/2023 2:26 PM, Ben Bacarisse wrote:
> "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:
>
>> On 9/18/2023 2:03 PM, Chris M. Thomasson wrote:
>>> On 9/18/2023 11:31 AM, David Brown wrote:
>>>> On 18/09/2023 17:54, Scott Lurndal wrote:
>>>>> David Brown <david.brown@hesbynett.no> writes:
>>>>>> On 18/09/2023 10:53, Anton Shepelev wrote:
>>>>>
>>>>>> * Variables can often be declared "const".  This leads to clearer code
>>>>>> that is easier to reason about - things don't change, and you only have
>>>>>> to look at one line to see the value that the variable has.  For
>>>>>> non-const variables, you have to look throughout their lifetimes - this
>>>>>> is maximally bad if they are defined at the top of the function.
>>>>>
>>>>> I would add that declaring variables const gives the optimization pass
>>>>> additional information and may result in more efficient register
>>>>> allocation and code generation.
>>>>>
>>>>>      const uint32_t regnum = bit::extract(instruction, 4, 0);
>>>>>
>>>>> also enhances readability.
>>>>
>>>> I agree that adding "const" improves readability, because it tells the
>>>> human reader that the thing never changes.
>>>>
>>>> But it doesn't help the compiler, since the compiler can read the rest
>>>> of the function and see that it does not change.  (So could a human
>>>> reader, but I care about saving the human reader the effort of having to
>>>> do that - I don't care about saving the compiler effort!)
>>>>
>>>>
>>> const as in:
>>> void
>>> ct_foo(
>>>   ct_foobar* const self
>>> ){
>>>    // self cannot be changed... So be it!
>>> }
>>> Kosher? ;^)
>
> ...cannot be changed without undefined behaviour.
>
>> What about:
>>
>> void
>> ct_foo(
>> ct_foobar const* const self
>> ){
>> // self cannot be changed... So be it!
>> // the ct_foobar that self points to is const...
>> }
>
> Technically, no.

When I read right to left wrt:

ct_foobar const* const self

I see,

self is a:

const pointer to a

const ct_foobar

Did I get it wrong?

The ct_foobar that self points to can't be changed
> using the self pointer directly, but the ct_foobar object itself might
> be const or it might not be, and it might even be able to be changed by
> the function in question:
>
> void ct_eg(const ct_foobar *self, ct_foobar *other_foobar)
> {
> ...
> change_this_thing_beyond_recognition(other_foobar);
> ...
> }
>
> and later...
>
> ct_foobar my_obj;
> ...
> ct_eg(&my_obj, &my_obj);
>
> This is (in part) what restrict-qualified pointers are for.
>

Re: Do you insist on const-correctness?

<ueal14$1vvcp$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29166&group=comp.lang.c#29166

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Mon, 18 Sep 2023 16:04:04 -0700
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <ueal14$1vvcp$2@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<20230918110915.139@kylheku.com> <uea5bd$1t2iu$1@dont-email.me>
<ueaa7e$1tugo$1@dont-email.me> <ueadqt$1uhr4$2@dont-email.me>
<20230918142634.194@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 18 Sep 2023 23:04:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e823050642b27d6b26a63385ba2f7eca";
logging-data="2096537"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/7oAR7SPpYCip8vkvm7F/NUbjQeYQCXVw="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:FRPCkJFh9B7WbUvmHLk4cP3UMns=
Content-Language: en-US
In-Reply-To: <20230918142634.194@kylheku.com>
 by: Chris M. Thomasson - Mon, 18 Sep 2023 23:04 UTC

On 9/18/2023 3:59 PM, Kaz Kylheku wrote:
> On 2023-09-18, Bart <bc@freeuk.com> wrote:
>> On 18/09/2023 20:59, David Brown wrote:
>>> The compiler can't trust the "const" qualifier.  (Well, it can in this
>>> case where it can see the definition of "bill" - but I assume you meant
>>> when the function was defined externally.)  It is legal for program to
>>> cast a pointer-to-const to a pointer-to-non-const, and use that to
>>> change the value, as long as the original object was not defined as
>>> "const".
>>
>> So, basically, 'const' can never be trusted?
>
> A const qualifier on a function parameter (the parameter itself,
> not what it points to) means nothing as far as declaring anything
> to a caller.[...]

A const is just a hint to the programmer, in a sense?

ct_foobar const* const self;

seems to get the "intent" across?

Re: Do you insist on const-correctness?

<87wmwnjch6.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29167&group=comp.lang.c#29167

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Mon, 18 Sep 2023 16:19:49 -0700
Organization: None to speak of
Lines: 34
Message-ID: <87wmwnjch6.fsf@nosuchdomain.example.com>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<20230918110915.139@kylheku.com> <uea5bd$1t2iu$1@dont-email.me>
<ueaa7e$1tugo$1@dont-email.me> <ueadqt$1uhr4$2@dont-email.me>
<20230918142634.194@kylheku.com> <ueal14$1vvcp$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="db989aa286e51ccd0458447dc5251e4d";
logging-data="2100706"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18mUlPjFPJcAc2SJNF1l5jc"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:FjloCV0PPxQakb7M9Lu+DyUxl+M=
sha1:XzXJfxEgaz8tWPSpbzAefSnak9s=
 by: Keith Thompson - Mon, 18 Sep 2023 23:19 UTC

"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:
> On 9/18/2023 3:59 PM, Kaz Kylheku wrote:
[...]
>> A const qualifier on a function parameter (the parameter itself,
>> not what it points to) means nothing as far as declaring anything
>> to a caller.[...]
>
> A const is just a hint to the programmer, in a sense?

Only when it's applied to a function parameter in a function declaration
that is not part of a function definition.

> ct_foobar const* const self;
>
> seems to get the "intent" across?

That means that `self` is a const pointer to const `ct_foobar`, where
`ct_foobar` is a type. Both occurrences of `const` impose constraints
on code that refers to self. If that's the "intent", then yes, it gets
it across.

Note that this is not a parameter declaration (if it were, the semicolon
would be a syntax error), so it's not directly relevant to the current
discussion.

If it were a parameter declaration, then the second `const` (which makes
`self` itself a const object) would impose no constraints on callers,
but if the function declaration were part of a function definition, it
would impose a constraint on code inside the function.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Re: Do you insist on const-correctness?

<ueam6o$206cj$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29168&group=comp.lang.c#29168

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Mon, 18 Sep 2023 16:24:08 -0700
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <ueam6o$206cj$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<20230918110915.139@kylheku.com> <uea5bd$1t2iu$1@dont-email.me>
<ueaa7e$1tugo$1@dont-email.me> <ueadqt$1uhr4$2@dont-email.me>
<20230918142634.194@kylheku.com> <ueal14$1vvcp$2@dont-email.me>
<87wmwnjch6.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 18 Sep 2023 23:24:08 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e823050642b27d6b26a63385ba2f7eca";
logging-data="2103699"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX199F1xnnOWhl9mrTl1xrIKHegWsECom8Yg="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:l3R9X/F8lxNqnftezhFFN6lSsl0=
Content-Language: en-US
In-Reply-To: <87wmwnjch6.fsf@nosuchdomain.example.com>
 by: Chris M. Thomasson - Mon, 18 Sep 2023 23:24 UTC

On 9/18/2023 4:19 PM, Keith Thompson wrote:
> "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:
>> On 9/18/2023 3:59 PM, Kaz Kylheku wrote:
> [...]
>>> A const qualifier on a function parameter (the parameter itself,
>>> not what it points to) means nothing as far as declaring anything
>>> to a caller.[...]
>>
>> A const is just a hint to the programmer, in a sense?
>
> Only when it's applied to a function parameter in a function declaration
> that is not part of a function definition.
>
>> ct_foobar const* const self;
>>
>> seems to get the "intent" across?
>
> That means that `self` is a const pointer to const `ct_foobar`, where
> `ct_foobar` is a type. Both occurrences of `const` impose constraints
> on code that refers to self. If that's the "intent", then yes, it gets
> it across.
>
> Note that this is not a parameter declaration (if it were, the semicolon
> would be a syntax error), so it's not directly relevant to the current
> discussion.

void
ct_foo(
ct_foobar* const self
){
// self "cannot" be changed... So be it!
}

vs:

void
ct_foo(
ct_foobar const* const self
){
// self "cannot" be changed... So be it!
// the ct_foobar that self points to is const...
}

Any better?

>
> If it were a parameter declaration, then the second `const` (which makes
> `self` itself a const object) would impose no constraints on callers,
> but if the function declaration were part of a function definition, it
> would impose a constraint on code inside the function.
>

Re: Do you insist on const-correctness?

<87edivhx3z.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29169&group=comp.lang.c#29169

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Tue, 19 Sep 2023 00:37:04 +0100
Organization: A noiseless patient Spider
Lines: 99
Message-ID: <87edivhx3z.fsf@bsb.me.uk>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<uea52i$1stu4$2@dont-email.me> <ueadvt$1uk16$4@dont-email.me>
<ueae1i$1uk16$5@dont-email.me> <87jzsni350.fsf@bsb.me.uk>
<ueakp8$1vvcp$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="95e4e89f3e4548c156f5ca76b6d747da";
logging-data="2107736"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ygV6RonLOonB+SHmIol9V60AxBZwpao8="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:pvCgw4XfYzwBYDSWhzXZMJhsaYk=
sha1:selM/xdh6qKkrxsSPvAgMzDTf9c=
X-BSB-Auth: 1.12ad39db60109914589f.20230919003704BST.87edivhx3z.fsf@bsb.me.uk
 by: Ben Bacarisse - Mon, 18 Sep 2023 23:37 UTC

"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:

> On 9/18/2023 2:26 PM, Ben Bacarisse wrote:
>> "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:
>>
>>> On 9/18/2023 2:03 PM, Chris M. Thomasson wrote:
>>>> On 9/18/2023 11:31 AM, David Brown wrote:
>>>>> On 18/09/2023 17:54, Scott Lurndal wrote:
>>>>>> David Brown <david.brown@hesbynett.no> writes:
>>>>>>> On 18/09/2023 10:53, Anton Shepelev wrote:
>>>>>>
>>>>>>> * Variables can often be declared "const".  This leads to clearer code
>>>>>>> that is easier to reason about - things don't change, and you only have
>>>>>>> to look at one line to see the value that the variable has.  For
>>>>>>> non-const variables, you have to look throughout their lifetimes - this
>>>>>>> is maximally bad if they are defined at the top of the function.
>>>>>>
>>>>>> I would add that declaring variables const gives the optimization pass
>>>>>> additional information and may result in more efficient register
>>>>>> allocation and code generation.
>>>>>>
>>>>>>      const uint32_t regnum = bit::extract(instruction, 4, 0);
>>>>>>
>>>>>> also enhances readability.
>>>>>
>>>>> I agree that adding "const" improves readability, because it tells the
>>>>> human reader that the thing never changes.
>>>>>
>>>>> But it doesn't help the compiler, since the compiler can read the rest
>>>>> of the function and see that it does not change.  (So could a human
>>>>> reader, but I care about saving the human reader the effort of having to
>>>>> do that - I don't care about saving the compiler effort!)
>>>>>
>>>>>
>>>> const as in:
>>>> void
>>>> ct_foo(
>>>>   ct_foobar* const self
>>>> ){
>>>>    // self cannot be changed... So be it!
>>>> }
>>>> Kosher? ;^)
>> ...cannot be changed without undefined behaviour.
>>
>>> What about:
>>>
>>> void
>>> ct_foo(
>>> ct_foobar const* const self
>>> ){
>>> // self cannot be changed... So be it!
>>> // the ct_foobar that self points to is const...
>>> }
>> Technically, no.
>
> When I read right to left wrt:
>
> ct_foobar const* const self
>
> I see,
>
> self is a:
>
> const pointer to a
>
> const ct_foobar
>
> Did I get it wrong?

No, that's a reasonable way to read the type, but that type does not
conflict with anything I said. We say "points to a const ct_foobar" but
that's a shorthand. The thing pointed to may not be const. What is
const in the pointer target type, so the lvalue expression

*self

has a const qualified type. The object pointed to can't be altered
using that lvalue expression even though the object may not itself have
a const-qualified type.

>> The ct_foobar that self points to can't be changed
>> using the self pointer directly, but the ct_foobar object itself might
>> be const or it might not be, and it might even be able to be changed by
>> the function in question:
>> void ct_eg(const ct_foobar *self, ct_foobar *other_foobar)
>> {
>> ...
>> change_this_thing_beyond_recognition(other_foobar);
>> ...
>> }
>> and later...
>> ct_foobar my_obj;
>> ...
>> ct_eg(&my_obj, &my_obj);
>> This is (in part) what restrict-qualified pointers are for.

--
Ben.

Re: Do you insist on const-correctness?

<87sf7bjbcl.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29170&group=comp.lang.c#29170

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Mon, 18 Sep 2023 16:44:10 -0700
Organization: None to speak of
Lines: 59
Message-ID: <87sf7bjbcl.fsf@nosuchdomain.example.com>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<20230918110915.139@kylheku.com> <uea5bd$1t2iu$1@dont-email.me>
<ueaa7e$1tugo$1@dont-email.me> <ueadqt$1uhr4$2@dont-email.me>
<20230918142634.194@kylheku.com> <ueal14$1vvcp$2@dont-email.me>
<87wmwnjch6.fsf@nosuchdomain.example.com>
<ueam6o$206cj$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="db989aa286e51ccd0458447dc5251e4d";
logging-data="2109491"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1848qVfqjaFIGJMvLrsxMoW"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:1RkKeHCAO3ARjTRge08rOEG3vx8=
sha1:qZkuf9BdyfH4itnF4LfVWxYqx3Q=
 by: Keith Thompson - Mon, 18 Sep 2023 23:44 UTC

"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:
> On 9/18/2023 4:19 PM, Keith Thompson wrote:
>> "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:
>>> On 9/18/2023 3:59 PM, Kaz Kylheku wrote:
>> [...]
>>>> A const qualifier on a function parameter (the parameter itself,
>>>> not what it points to) means nothing as far as declaring anything
>>>> to a caller.[...]
>>>
>>> A const is just a hint to the programmer, in a sense?
>> Only when it's applied to a function parameter in a function
>> declaration
>> that is not part of a function definition.
>>
>>> ct_foobar const* const self;
>>>
>>> seems to get the "intent" across?
>> That means that `self` is a const pointer to const `ct_foobar`,
>> where `ct_foobar` is a type. Both occurrences of `const` impose
>> constraints on code that refers to self. If that's the "intent",
>> then yes, it gets it across.
>>
>> Note that this is not a parameter declaration (if it were, the
>> semicolon would be a syntax error), so it's not directly relevant to
>> the current discussion.
>
> void
> ct_foo(
> ct_foobar* const self
> ){
> // self "cannot" be changed... So be it!
> }

Attempting to modify self directly, for example `self = NULL;`, is a
constraint violation. Attempting to modify self indirectly, for example
`*(ct_foobar*)self = NULL;` has undefined behavior.

> vs:
>
> void
> ct_foo(
> ct_foobar const* const self
> ){
> // self "cannot" be changed... So be it!
> // the ct_foobar that self points to is const...
> }
>
> Any better?
[...]

And in both cases, the fact that the parameter `self` is const does not
affect callers.

Better for what purpose?

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Re: Do you insist on const-correctness?

<91bd7759-54ae-40c9-bd0b-0f352224cc53n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29171&group=comp.lang.c#29171

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:19a7:b0:40d:b839:b5bb with SMTP id u39-20020a05622a19a700b0040db839b5bbmr25385qtc.2.1695080869465;
Mon, 18 Sep 2023 16:47:49 -0700 (PDT)
X-Received: by 2002:a05:6830:160f:b0:6b9:6ef7:72f3 with SMTP id
g15-20020a056830160f00b006b96ef772f3mr3190378otr.0.1695080869223; Mon, 18 Sep
2023 16:47:49 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 18 Sep 2023 16:47:48 -0700 (PDT)
In-Reply-To: <ue9p8d$1qfvq$2@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=81.143.231.9; posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 81.143.231.9
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<878r94l6jj.fsf@bsb.me.uk> <20230918105557.34ccda4e0c85b711a58302ab@g{oogle}mail.com>
<ue9cv4$1ob5a$1@dont-email.me> <5bd9dd1f-93ca-434a-b9aa-65d7229f85ean@googlegroups.com>
<ue9p8d$1qfvq$2@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <91bd7759-54ae-40c9-bd0b-0f352224cc53n@googlegroups.com>
Subject: Re: Do you insist on const-correctness?
From: malcolm....@gmail.com (comp.lang.c)
Injection-Date: Mon, 18 Sep 2023 23:47:49 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2860
 by: comp.lang.c - Mon, 18 Sep 2023 23:47 UTC

On Monday, 18 September 2023 at 16:10:23 UTC+1, David Brown wrote:
> On 18/09/2023 15:02, Malcolm McLean wrote:
>
> > And it is far better to not use it at all than to have "semi-const" or
> > "logically const" concepts, where const is used but doesn't actually mean
> > what it conventionally means.
> Why is using "const" (telling the user "I won't change your data") when
> you mean "logically const" worse than using non-const (telling the user
> "Beware - I'm going to change your data") when you are /not/ going to
> change it? C won't let you say exactly what you mean either way.
>
Firstly, const obviously means "this won't change", but there's no "mutated"
keyword to mean "this will change" or this will change". Non-const items
are simply not declared const. You can assume that they may change if
and only if the project has a policy of "const correctness". And even then,
where const means "this won't change", non-const only means "this changes
on some execution path". Which may be excluded by the other parameters.
It doesn't mean that the object will change.
So it's not symmetrical.
If you have a policy of exclusing const then all the problems simply disappear.
You have to document which functions change objects passed by pointer,
if and only if they are transparent pointers and it's any business of callers.
But it's the rare function where the pointer is transparent, but any documentation
worthy of the name wouldn't say what state the object is left in after the function
returns.

Re: Do you insist on const-correctness?

<ueaqgb$20t4i$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29172&group=comp.lang.c#29172

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Mon, 18 Sep 2023 17:37:30 -0700
Organization: A noiseless patient Spider
Lines: 59
Message-ID: <ueaqgb$20t4i$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<20230918110915.139@kylheku.com> <uea5bd$1t2iu$1@dont-email.me>
<ueaa7e$1tugo$1@dont-email.me> <ueadqt$1uhr4$2@dont-email.me>
<20230918142634.194@kylheku.com> <ueal14$1vvcp$2@dont-email.me>
<87wmwnjch6.fsf@nosuchdomain.example.com> <ueam6o$206cj$1@dont-email.me>
<87sf7bjbcl.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 19 Sep 2023 00:37:31 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e823050642b27d6b26a63385ba2f7eca";
logging-data="2126994"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19pvXNvKPoJt63lr9oE59Cr3pNuX6lZVIA="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:noIw7VsPQj5yL+otIPmqvDX114k=
Content-Language: en-US
In-Reply-To: <87sf7bjbcl.fsf@nosuchdomain.example.com>
 by: Chris M. Thomasson - Tue, 19 Sep 2023 00:37 UTC

On 9/18/2023 4:44 PM, Keith Thompson wrote:
> "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:
>> On 9/18/2023 4:19 PM, Keith Thompson wrote:
>>> "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:
>>>> On 9/18/2023 3:59 PM, Kaz Kylheku wrote:
>>> [...]
>>>>> A const qualifier on a function parameter (the parameter itself,
>>>>> not what it points to) means nothing as far as declaring anything
>>>>> to a caller.[...]
>>>>
>>>> A const is just a hint to the programmer, in a sense?
>>> Only when it's applied to a function parameter in a function
>>> declaration
>>> that is not part of a function definition.
>>>
>>>> ct_foobar const* const self;
>>>>
>>>> seems to get the "intent" across?
>>> That means that `self` is a const pointer to const `ct_foobar`,
>>> where `ct_foobar` is a type. Both occurrences of `const` impose
>>> constraints on code that refers to self. If that's the "intent",
>>> then yes, it gets it across.
>>>
>>> Note that this is not a parameter declaration (if it were, the
>>> semicolon would be a syntax error), so it's not directly relevant to
>>> the current discussion.
>>
>> void
>> ct_foo(
>> ct_foobar* const self
>> ){
>> // self "cannot" be changed... So be it!
>> }
>
> Attempting to modify self directly, for example `self = NULL;`, is a
> constraint violation. Attempting to modify self indirectly, for example
> `*(ct_foobar*)self = NULL;` has undefined behavior.
>
>> vs:
>>
>> void
>> ct_foo(
>> ct_foobar const* const self
>> ){
>> // self "cannot" be changed... So be it!
>> // the ct_foobar that self points to is const...
>> }
>>
>> Any better?
> [...]
>
> And in both cases, the fact that the parameter `self` is const does not
> affect callers.
>
> Better for what purpose?
>

Better to try to convey to a reader that this intent is meant, or else
be there nasal demons... ?

Re: Do you insist on const-correctness?

<ueb75c$2667l$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29173&group=comp.lang.c#29173

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Tue, 19 Sep 2023 00:13:32 -0400
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <ueb75c$2667l$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<20230918110915.139@kylheku.com> <uea5bd$1t2iu$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 19 Sep 2023 04:13:32 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="168ce3afed49ec2f4e207ca0ec9fce70";
logging-data="2300149"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Tye7BDCEaPnApO30xi/WC3YwC6sVsyKo="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:Id0BJzA/ngM0C6a8zCHvmiYkwgY=
In-Reply-To: <uea5bd$1t2iu$1@dont-email.me>
Content-Language: en-US
 by: James Kuyper - Tue, 19 Sep 2023 04:13 UTC

On 18/09/2023 20:36, Bart wrote:
....
>   typedef struct {int d,m,y;} date;
>
>   void fred(date d) {}
>   void bill(const date d) {}

In a declaration that's not a definition:

void bill(const date d);

qualifiers such as "const" that are applied directly to parameters are
simply ignored. In the declaration which is part of a definition of the
function, such qualifiers matter only inside the function - they have no
effect on compatibility between the function's definition and it's other
declarations.

The only reason to declare such a parameter "const" is if it's not
supposed to be changed. In that regard, it's just like any other
variable which is local to the function. It's a good general rule that
you should declare any variable "const" if you know that it should never
be changed from the value it's initialized with. If that's the case, the
qualifier ensures that most code which puts the variable at risk of
being changed will make it mandatory for the compiler to issue a
diagnostic. When you see that diagnostic, you know that either you were
wrong about that variable never changing, or the code that triggered the
diagnostic needs to be corrected.

You don't need the "const" to enable any applicable optimizations - if
your code does not, in fact, change the variable, the compiler can
easily detect that fact and apply any optimizations enabled by that fact.

Re: Do you insist on const-correctness?

<uebid6$28ata$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29174&group=comp.lang.c#29174

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Tue, 19 Sep 2023 09:25:26 +0200
Organization: A noiseless patient Spider
Lines: 82
Message-ID: <uebid6$28ata$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<878r94l6jj.fsf@bsb.me.uk>
<20230918105557.34ccda4e0c85b711a58302ab@g{oogle}mail.com>
<ue9cv4$1ob5a$1@dont-email.me>
<5bd9dd1f-93ca-434a-b9aa-65d7229f85ean@googlegroups.com>
<ue9p8d$1qfvq$2@dont-email.me>
<91bd7759-54ae-40c9-bd0b-0f352224cc53n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 19 Sep 2023 07:25:26 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="21b889700009786c696fe134d8bff631";
logging-data="2370474"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19344CBuWlo8luXMVzMq3ImuVdcIKWDaq4="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:dIWXP0OjaNcVuOLWckaru+RzOfc=
Content-Language: en-GB
In-Reply-To: <91bd7759-54ae-40c9-bd0b-0f352224cc53n@googlegroups.com>
 by: David Brown - Tue, 19 Sep 2023 07:25 UTC

On 19/09/2023 01:47, comp.lang.c wrote:

(Malcolm, you've mixed up your Google Groups settings somewhere, and
given yourself the name "comp.lang.c". )

> On Monday, 18 September 2023 at 16:10:23 UTC+1, David Brown wrote:
>> On 18/09/2023 15:02, Malcolm McLean wrote:
>>
>>> And it is far better to not use it at all than to have "semi-const" or
>>> "logically const" concepts, where const is used but doesn't actually mean
>>> what it conventionally means.
>> Why is using "const" (telling the user "I won't change your data") when
>> you mean "logically const" worse than using non-const (telling the user
>> "Beware - I'm going to change your data") when you are /not/ going to
>> change it? C won't let you say exactly what you mean either way.
>>
> Firstly, const obviously means "this won't change", but there's no "mutated"
> keyword to mean "this will change" or this will change".

That's not what "const" means in C. But we can fully agree that there
is no "mutated" keyword or other nuanced "sort-of constant" marker in C.
(Perhaps one day there will be attributes added for this kind of thing.)

> Non-const items
> are simply not declared const. You can assume that they may change if
> and only if the project has a policy of "const correctness".

No, you must /always/ assume that they might change - unless there is
documentation guaranteeing that they don't.

Even with "const", it is possible for things to change, because you can
always cast it away in the implementation of the function. But it is a
strong indication to the user that you do not intend to change anything,
and you have to go out of your way (such as via a cast) to change things
that are marked "const".

> And even then,
> where const means "this won't change", non-const only means "this changes
> on some execution path". Which may be excluded by the other parameters.

As we all know, non-const means "may change", not "/will/ change".

And "const" means "this won't change unless I really want it to".

> It doesn't mean that the object will change.
> So it's not symmetrical.

It is more symmetric than you think. It is not as clear-cut as you
think. And if you have something that does not change in the
user-visible aspects (assuming the function user sticks to the defined
API), but may change in its internal aspects, there is no fixed rule
that says whether the pointer should be marked "const" or left
non-const. I would say it must be handled on a case-by-case basis -
whatever makes things clearer to the user and reduce their risk of
getting things wrong. (And of course the nuances must be documented,
since C can't express them.)

> If you have a policy of exclusing const then all the problems simply disappear.

Give up and run away? C's "const" keyword is not perfect in all
use-cases, so you think it is better to miss out entirely? That's a
very poor attitude. No tool is ever perfect in all circumstances -
that's no excuse for not doing the best you can with what you've got.

> You have to document which functions change objects passed by pointer,
> if and only if they are transparent pointers and it's any business of callers.

Documentation should supplement the code, not replace it. When things
can be written in the language, write it in the language - not in comments.

> But it's the rare function where the pointer is transparent, but any documentation
> worthy of the name wouldn't say what state the object is left in after the function
> returns.

I'm not sure what you are trying to say here - I'm not sure that /you/
are sure. After all, if a function takes a pointer to an object, the
documentation should be very clear about what state the object will be
in after the call.

Re: Do you insist on const-correctness?

<uebiso$28ata$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29175&group=comp.lang.c#29175

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Tue, 19 Sep 2023 09:33:44 +0200
Organization: A noiseless patient Spider
Lines: 104
Message-ID: <uebiso$28ata$2@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<20230918110915.139@kylheku.com> <uea5bd$1t2iu$1@dont-email.me>
<20230918125058.653@kylheku.com> <ueadjl$1uhr4$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 19 Sep 2023 07:33:44 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="21b889700009786c696fe134d8bff631";
logging-data="2370474"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+AOKlBbWNGjjMHD9T69KJ+JBbE7HwlBIM="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:WiIECc4WgqplT/4Vb9idTOsQBTE=
Content-Language: en-GB
In-Reply-To: <ueadjl$1uhr4$1@dont-email.me>
 by: David Brown - Tue, 19 Sep 2023 07:33 UTC

On 18/09/2023 22:57, Bart wrote:
> On 18/09/2023 20:55, Kaz Kylheku wrote:
>> On 2023-09-18, Bart <bc@freeuk.com> wrote:
>>> On 18/09/2023 19:10, Kaz Kylheku wrote:
>>>> On 2023-09-18, Scott Lurndal <scott@slp53.sl.home> wrote:
>>>>> David Brown <david.brown@hesbynett.no> writes:
>>>>>> On 18/09/2023 10:53, Anton Shepelev wrote:
>>>>>
>>>>>> * Variables can often be declared "const".  This leads to clearer
>>>>>> code
>>>>>> that is easier to reason about - things don't change, and you only
>>>>>> have
>>>>>> to look at one line to see the value that the variable has.  For
>>>>>> non-const variables, you have to look throughout their lifetimes -
>>>>>> this
>>>>>> is maximally bad if they are defined at the top of the function.
>>>>>
>>>>> I would add that declaring variables const gives the optimization pass
>>>>> additional information
>>>>
>>>> Declaring variables const provides no additional optimizing info over
>>>> just not assigning to the variable and not taking its address.
>>>>
>>>
>>> If you take a program like this:
>>>
>>>     typedef struct {int d,m,y;} date;
>>>
>>>     void fred(date d) {}
>>>     void bill(const date d) {}
>>
>> The const here makes no difference.
>
> Well, yeah, I changed those {} to ; precisely because someone would say
> that, but I forgot to repaste it.
>
> In any case, in such examples, you have to assume the the contents of {}
> could be anything, not literally empty bodies.
>
>> Since this is a definition, the compiler can inline the function, and
>> make it disappear, since it does nothing.
>>
>>>     int main(void) {
>>>         date dd;
>>>
>>>         fred(dd);
>>>         bill(dd);
>>>
>>> On a modern ABI where the struct is passed internally by reference (if
>>> not, make it bigger), a compiler may be obliged to copy the struct to a
>>> temporary location when calling fred(), but doesn't need to do so when
>>> calling bill(), if it trusts that 'const' qualifier.
>>
>> Suppose that bill is an external function, which is declared like
>> this:
>>
>>    void bill(const date d);
>>
>> that const means nothing and can (really, should) be ignored.
>>
>> The function definition can look like:
>>
>>    void bill(date d) { /*...*/ }
>>
>> or like:
>>
>>    void bill(const date d) { /*...*/ }
>>
>> both of these are correct, and their ABI must be exactly the same.
>>
>>> Although this is nothing really to do with optimisation,  just common
>>> sense.
>>
>> Common sense is no substitute for 1) documentation and 2) empirically
>> knowing how things are made by people who follow (1).
>
> So what would you do if generating code for:
>
>    bill(dd);
>
> when the body of bill() is not visible; would you make a copy of the
> struct of not? Bear in a mind that while this struct is only 12 bytes,
> another might be 0.5MB in size, and copying it on each of millions of
> calls would be a major overhead.
>

By "you", do you mean the compiler? Yes, the compiler needs to make a
copy if the original "dd" is needed later. And if it does not have
access to the definition of "bill" (it's in a separately compiled unit),
it needs to follow the ABI - typically for large structures, that means
copying it onto the stack. (For some targets, it may mean passing a
hidden pointer to a copy that does not have to be on the stack - that's
up to the target ABI.)

If the compiler has access to the definition of "bill" and is handling
the compilation of the two parts together, it's a different story. Then
it can merge code, inline code, use information about whether "bill"
actually changes "dd" or not, use any calling convention it wants, and
so on. Whole-program optimisation (or link-time-optimisation - there
are many names) gives the compiler a lot of freedom. But regardless, it
must always give the same end results as though "dd" were copied.

Re: Do you insist on const-correctness?

<uebjbs$28ata$3@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29176&group=comp.lang.c#29176

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Tue, 19 Sep 2023 09:41:48 +0200
Organization: A noiseless patient Spider
Lines: 101
Message-ID: <uebjbs$28ata$3@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<20230918110915.139@kylheku.com> <uea5bd$1t2iu$1@dont-email.me>
<ueaa7e$1tugo$1@dont-email.me> <ueadqt$1uhr4$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 19 Sep 2023 07:41:48 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="21b889700009786c696fe134d8bff631";
logging-data="2370474"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+l344luHFz9RyEaBkVBwYh9ESWlhsiIH0="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:rEOwqEIESj1V6m5sMIZcsm26g90=
Content-Language: en-GB
In-Reply-To: <ueadqt$1uhr4$2@dont-email.me>
 by: David Brown - Tue, 19 Sep 2023 07:41 UTC

On 18/09/2023 23:01, Bart wrote:
> On 18/09/2023 20:59, David Brown wrote:
>> On 18/09/2023 20:36, Bart wrote:
>>> On 18/09/2023 19:10, Kaz Kylheku wrote:
>>>> On 2023-09-18, Scott Lurndal <scott@slp53.sl.home> wrote:
>>>>> David Brown <david.brown@hesbynett.no> writes:
>>>>>> On 18/09/2023 10:53, Anton Shepelev wrote:
>>>>>
>>>>>> * Variables can often be declared "const".  This leads to clearer
>>>>>> code
>>>>>> that is easier to reason about - things don't change, and you only
>>>>>> have
>>>>>> to look at one line to see the value that the variable has.  For
>>>>>> non-const variables, you have to look throughout their lifetimes -
>>>>>> this
>>>>>> is maximally bad if they are defined at the top of the function.
>>>>>
>>>>> I would add that declaring variables const gives the optimization pass
>>>>> additional information
>>>>
>>>> Declaring variables const provides no additional optimizing info over
>>>> just not assigning to the variable and not taking its address.
>>>>
>>>
>>> If you take a program like this:
>>>
>>>    typedef struct {int d,m,y;} date;
>>>
>>>    void fred(date d) {}
>>>    void bill(const date d) {}
>>>
>>>    int main(void) {
>>>        date dd;
>>>
>>>        fred(dd);
>>>        bill(dd);
>>>
>>> On a modern ABI where the struct is passed internally by reference
>>> (if not, make it bigger), a compiler may be obliged to copy the
>>> struct to a temporary location when calling fred(), but doesn't need
>>> to do so when calling bill(), if it trusts that 'const' qualifier.
>>
>> The compiler can't trust the "const" qualifier.  (Well, it can in this
>> case where it can see the definition of "bill" - but I assume you
>> meant when the function was defined externally.)  It is legal for
>> program to cast a pointer-to-const to a pointer-to-non-const, and use
>> that to change the value, as long as the original object was not
>> defined as "const".
>
> So, basically, 'const' can never be trusted?

Not fully, no.

int foo(const int * p) {
int * q = (int *) p;
*q++;
return *q;
}

You have to go out of your way to remove the "const" qualifier, so
"const" is good at stopping accidents. And good programmers are not
going to break "const" without very good reason (and hopefully very good
documentation), but it certainly can be done.

The "const" qualifier in C is about as good as can be made for something
that was added to the language. To give a language a proper concept of
read-only access that can't be broken, I believe you really need to have
it as a fundamental from the start. (It is the norm for functional
programming languages, for examples.) "const" in C is far from perfect
in all cases, but useful in many situations nonetheless.

>
> If you have this:
>
>     extern void F(void);
>     const int abc=100;
>
>     int main(void) {
>
>         F();
>         abc;
>
> Then this point, a compiler can never assume that 'abc' still has the
> value '100' at this point?
>

Ah, that's a little different. If an object is /defined/ with "const",
then changing its value is undefined behaviour. And that means the
compiler /can/ assume it does not change.

This is true even if you have code :

int * p = (int *) &abc;
*p = 123;

The compiler can still assume "abc" is 100 after that code. Maybe the
program will crash on the line "*p = 123;", due to memory protection
trapping. Maybe it will run and crash later - its undefined behaviour,
so there are no guarantees.

Re: Do you insist on const-correctness?

<uebnp5$2985i$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29177&group=comp.lang.c#29177

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Tue, 19 Sep 2023 09:57:10 +0100
Organization: A noiseless patient Spider
Lines: 117
Message-ID: <uebnp5$2985i$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<20230918110915.139@kylheku.com> <uea5bd$1t2iu$1@dont-email.me>
<ueb75c$2667l$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 19 Sep 2023 08:57:09 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="83878dd757b56bd42626981bbcafaa18";
logging-data="2400434"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/zNxgdmYj8/3057KZoG0ouV+FKYWvI7Ec="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:fYBOK02a1bzVICyM/5+yXuEpnBs=
In-Reply-To: <ueb75c$2667l$1@dont-email.me>
 by: Bart - Tue, 19 Sep 2023 08:57 UTC

On 19/09/2023 05:13, James Kuyper wrote:
> On 18/09/2023 20:36, Bart wrote:
> ...
>>   typedef struct {int d,m,y;} date;
>>
>>   void fred(date d) {}
>>   void bill(const date d) {}
>
> In a declaration that's not a definition:
>
> void bill(const date d);
>
> qualifiers such as "const" that are applied directly to parameters are
> simply ignored. In the declaration which is part of a definition of the
> function, such qualifiers matter only inside the function - they have no
> effect on compatibility between the function's definition and it's other
> declarations.
>
> The only reason to declare such a parameter "const" is if it's not
> supposed to be changed.

Structs passed 'by-value' on a modern ABI can very possibly be passed
by-reference. It depends on the struct details and ABI.

This makes it quite different from other kinds of objects which really
are passed by value. (An array object is another which is implicitly
passed by reference, but that detail is exposed in the language and
people will aware of it.)

> In that regard, it's just like any other
> variable which is local to the function. It's a good general rule that
> you should declare any variable "const" if you know that it should never
> be changed from the value it's initialized with. If that's the case, the
> qualifier ensures that most code which puts the variable at risk of
> being changed will make it mandatory for the compiler to issue a
> diagnostic. When you see that diagnostic, you know that either you were
> wrong about that variable never changing, or the code that triggered the
> diagnostic needs to be corrected.
>
> You don't need the "const" to enable any applicable optimizations - if
> your code does not, in fact, change the variable, the compiler can
> easily detect that fact and apply any optimizations enabled by that fact.

With independent compilation, or using external libraries, in general
the bodies of any functions that are called will not be visible, only
the declarations.

So that sort of analysis cannot be applied.

Apparently, to guaranteed pass-by-value semantics even though 'const'
has been applied, it is necessary to create a copy of such structs and
pass that instead.

But look at this example function of a library (Raylib) which uses
by-value structs extensively:

RLAPI Vector2 GetWorldToScreen(Vector3 position, Camera camera);

extern Vector3 arg1;
extern Camera arg2;

int main(void) {
GetWorldToScreen(arg1, arg2);
}

(Full specs are given below.)

The Vector3 type is 12 bytes, and Camera is 44 bytes. 56 bytes that need
copying on every call. This API has 100s of such functions, and structs
are always passed by value.

Wouldn't it be better if that copy could be avoided? That is, without
rewriting the library to use explicit references, and having to use &
for each passed struct. (I think that would also rule out being able to
use compound literals.)

Avoiding the copy means less generated code, and faster execution,
although the overheads for this library are not critical.

----------------------------------
#define RLAPI

typedef struct Vector2 {
float x; // Vector x component
float y; // Vector y component
} Vector2;

typedef struct Vector3 {
float x; // Vector x component
float y; // Vector y component
float z; // Vector z component
} Vector3;

typedef struct Camera3D {
Vector3 position; // Camera position
Vector3 target; // Camera target it looks-at
Vector3 up; // Camera up vector (rotation over its axis)
float fovy; // Camera field-of-view aperture in Y
(degrees) in perspective, used as near plane width in orthographic
int projection; // Camera projection: CAMERA_PERSPECTIVE or
CAMERA_ORTHOGRAPHIC
} Camera3D;

typedef Camera3D Camera; // Camera type fallback, defaults to Camera3D

RLAPI Vector2 GetWorldToScreen(Vector3 position, Camera camera);

extern Vector3 arg1;
extern Camera arg2;

int main(void) {
GetWorldToScreen(arg1, arg2);
}

Re: Do you insist on const-correctness?

<uebnuo$296h8$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29178&group=comp.lang.c#29178

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Tue, 19 Sep 2023 11:00:08 +0200
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <uebnuo$296h8$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <20230918095305.817@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 19 Sep 2023 09:00:08 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="21b889700009786c696fe134d8bff631";
logging-data="2398760"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+SfEo6akZXw5sxjLlAH3FFErMgOEHB93Q="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:xv2r3NRb2uAvSqFt4ErQsRYiACc=
In-Reply-To: <20230918095305.817@kylheku.com>
Content-Language: en-GB
 by: David Brown - Tue, 19 Sep 2023 09:00 UTC

On 18/09/2023 19:15, Kaz Kylheku wrote:
> On 2023-09-18, David Brown <david.brown@hesbynett.no> wrote:

>>
>> I prefer not to declare any variable until I have a proper
>> initialisation for it.
>
> C90 syntax supports this by allowing nested compound statements to
> introduce declarations.
>
> {
> int yes = 1;
> setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof yes);
> }
>
> This has advantages. We control not only the start of the identifier's
> scope, but also the end.
>
That's all true, but is the OP using C90? I've assumed he is using more
modern C, and the layout defining locals at the start of the function is
an active choice.

I think it is reasonable to assume that people are using at least C99,
if not C11, unless they specifically say they are hobbled to C90.

Re: Do you insist on const-correctness?

<uebom4$29dfb$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29179&group=comp.lang.c#29179

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Tue, 19 Sep 2023 10:12:36 +0100
Organization: A noiseless patient Spider
Lines: 48
Message-ID: <uebom4$29dfb$1@dont-email.me>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<86a5tkmj1b.fsf@linuxsc.com>
<20230918115357.0c06b3c4225a6c33bb3244dc@g{oogle}mail.com>
<ue9ocg$1qfvq$1@dont-email.me> <4p_NM.30205$oX8.25901@fx46.iad>
<20230918110915.139@kylheku.com> <uea5bd$1t2iu$1@dont-email.me>
<ueaa7e$1tugo$1@dont-email.me> <ueadqt$1uhr4$2@dont-email.me>
<uebjbs$28ata$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 19 Sep 2023 09:12:36 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="83878dd757b56bd42626981bbcafaa18";
logging-data="2405867"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/FWvuk0HXVRZDSdnbqdrbfULrUw8EYFZ4="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:DY+XAbE7ZLjKo8VdCZrh42FbouM=
In-Reply-To: <uebjbs$28ata$3@dont-email.me>
 by: Bart - Tue, 19 Sep 2023 09:12 UTC

On 19/09/2023 08:41, David Brown wrote:
> On 18/09/2023 23:01, Bart wrote:

>> If you have this:
>>
>>      extern void F(void);
>>      const int abc=100;
>>
>>      int main(void) {
>>
>>          F();
>>          abc;
>>
>> Then this point, a compiler can never assume that 'abc' still has the
>> value '100' at this point?
>>
>
> Ah, that's a little different.  If an object is /defined/ with "const",
> then changing its value is undefined behaviour.  And that means the
> compiler /can/ assume it does not change.
>
> This is true even if you have code :
>
>     int * p = (int *) &abc;
>     *p = 123;
>
> The compiler can still assume "abc" is 100 after that code.  Maybe the
> program will crash on the line "*p = 123;", due to memory protection
> trapping.  Maybe it will run and crash later - its undefined behaviour,
> so there are no guarantees.

Making 'abc' static complicates things because some compilers will put
it into read-only memory. A test program where F was in a second module
that defined 'abc' without 'const', was able to modify 'abc' using
bcc/tcc, but it crashed on gcc.

A better example would have 'abc' as a const local which is somehow
accessible to an external function.

(I've played around with 'let' in my language, which works for simple
variables. You can't assign to them again, but also you can't apply &,
and they can't be passed to a function as a by-reference argument.

I didn't like it because it doesn't work for more elaborate types (where
applying & may be essential), and such variables have to be declared at
the first point of use. Currently 'let' variable are mainly used
internally for implicitly-declared for-loop indices.)

Re: Do you insist on const-correctness?

<87wmwmiiyt.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29180&group=comp.lang.c#29180

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Do you insist on const-correctness?
Date: Tue, 19 Sep 2023 02:57:14 -0700
Organization: None to speak of
Lines: 21
Message-ID: <87wmwmiiyt.fsf@nosuchdomain.example.com>
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc>
<878r94l6jj.fsf@bsb.me.uk>
<20230918105557.34ccda4e0c85b711a58302ab@g{oogle}mail.com>
<ue9cv4$1ob5a$1@dont-email.me>
<5bd9dd1f-93ca-434a-b9aa-65d7229f85ean@googlegroups.com>
<ue9p8d$1qfvq$2@dont-email.me>
<91bd7759-54ae-40c9-bd0b-0f352224cc53n@googlegroups.com>
<uebid6$28ata$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="db989aa286e51ccd0458447dc5251e4d";
logging-data="2414398"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1989JLVlLZiR8l6h02lJKB0"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:xi9jYPpOpng4PFwnCixHvqx3j6w=
sha1:I8hsJekUbIsOVTA0iD6cdKVXxeE=
 by: Keith Thompson - Tue, 19 Sep 2023 09:57 UTC

David Brown <david.brown@hesbynett.no> writes:
> On 19/09/2023 01:47, comp.lang.c wrote:
> (Malcolm, you've mixed up your Google Groups settings somewhere, and
> given yourself the name "comp.lang.c". )
[...]

It's likely something going wrong with Google Groups itself. I've seen
a handful or articles in other newsgroups with headers like

From: "newsgroup.name" <email@address>

(which caused some spam to bypass my filters). I made a test post on GG
that didn't exhibit the issue, so it's probably sporadic.

I'm sure Google's devotion to a smooth integration between Google Groups
and Usenet will lead to a quick solution -- in the fullness of time.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Re: Do you insist on const-correctness?

<eAhOM.7294$H0Ge.6208@fx05.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=29184&group=comp.lang.c#29184

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!feeder1.feed.usenet.farm!feed.usenet.farm!peer03.ams4!peer.am4.highwinds-media.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx05.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Do you insist on const-correctness?
Newsgroups: comp.lang.c
References: <20230918012105.b9fb2c36e93542dddb654b1f@gmail.moc> <878r94l6jj.fsf@bsb.me.uk> <20230918105557.34ccda4e0c85b711a58302ab@g{oogle}mail.com> <ue9cv4$1ob5a$1@dont-email.me> <5bd9dd1f-93ca-434a-b9aa-65d7229f85ean@googlegroups.com> <ue9p8d$1qfvq$2@dont-email.me> <91bd7759-54ae-40c9-bd0b-0f352224cc53n@googlegroups.com> <uebid6$28ata$1@dont-email.me>
Lines: 9
Message-ID: <eAhOM.7294$H0Ge.6208@fx05.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Tue, 19 Sep 2023 13:43:38 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Tue, 19 Sep 2023 13:43:38 GMT
X-Received-Bytes: 1279
 by: Scott Lurndal - Tue, 19 Sep 2023 13:43 UTC

David Brown <david.brown@hesbynett.no> writes:
>On 19/09/2023 01:47, comp.lang.c wrote:
>
>(Malcolm, you've mixed up your Google Groups settings somewhere, and
>given yourself the name "comp.lang.c". )

I've seen posts in other groups that imply this is a new "feature" of
the google groups interface.


devel / comp.lang.c / Re: Do you insist on const-correctness?

Pages:12345678
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor