Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

Kiss your keyboard goodbye!


devel / comp.lang.c / Re: "not-const" qualifier for C

SubjectAuthor
* "not-const" qualifier for CThiago Adams
+* Re: "not-const" qualifier for CKaz Kylheku
|`* Re: "not-const" qualifier for CThiago Adams
| `- Re: "not-const" qualifier for CKaz Kylheku
`- Re: "not-const" qualifier for CBlue-Maned_Hawk

1
"not-const" qualifier for C

<v0bma4$2g9n9$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=52843&group=comp.lang.c#52843

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.adams@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: "not-const" qualifier for C
Date: Wed, 24 Apr 2024 16:24:52 -0300
Organization: A noiseless patient Spider
Lines: 65
Message-ID: <v0bma4$2g9n9$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 24 Apr 2024 21:24:53 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="fbf65ac619c5165ee777e0c9ede4f805";
logging-data="2631401"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19zS63nfTtUJtEHUFEzkG3CDF4zvwiuIcA="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:oxVZz4xLdvK/b0rdu8sBaNZf8RQ=
Content-Language: en-US
 by: Thiago Adams - Wed, 24 Apr 2024 19:24 UTC

Motivation sample:

struct X {
const char* const type;
};

struct X * make_x(){
struct X * p = malloc(sizeof *p);
if (p)
{

p->type = strdup("X"); // *** error, type is const ***

if (p->type == NULL)
{
free(p);
p = NULL;
}
}
return p; //ok
}

void print(struct X * p){
prinf("%s", p->type);
}

void x_destroy(struct X * p)
{ free(p->type); // *** warning const to non-const ***
}

Now consider we have the keyword "mutable" that removes const from
struct members.

struct X {
const char* const type;
};

struct X * make_x(){
mutable struct X * p = malloc(sizeof *p);
if (p)
{
p->type = strdup("X"); //OK
if (p->type == NULL)
{
free(p);
p = NULL;
}
}
return p; //ok
}

void print(struct X * p){
prinf("%s", p->type);
}

void x_destroy(mutable struct X * p)
{ free(p->type); //no warning
}

Re: "not-const" qualifier for C

<20240424133951.155@kylheku.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=52845&group=comp.lang.c#52845

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 643-408-1753@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: "not-const" qualifier for C
Date: Wed, 24 Apr 2024 23:13:07 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 73
Message-ID: <20240424133951.155@kylheku.com>
References: <v0bma4$2g9n9$1@dont-email.me>
Injection-Date: Thu, 25 Apr 2024 01:13:07 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="beacde28bb5b90842ad087fd24932ac8";
logging-data="2730837"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18GZ4v1zBSOdWuj71K2531iQ5Ne6DDpgak="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:fasQsPW0c6PyPAo6K7DgQPuNOH0=
 by: Kaz Kylheku - Wed, 24 Apr 2024 23:13 UTC

On 2024-04-24, Thiago Adams <thiago.adams@gmail.com> wrote:
> Motivation sample:
>
> struct X {
> const char* const type;
> };
>
> struct X * make_x(){
> struct X * p = malloc(sizeof *p);
> if (p)
> {
>
> p->type = strdup("X"); // *** error, type is const ***
>
> if (p->type == NULL)
> {
> free(p);
> p = NULL;
> }
> }
> return p; //ok
> }

Different idea: allow all conversions without a cast which only
add qualifiers anywhere in the type:

struct X {
const char* const type;
};

struct mutable_X {
char* type;
};

struct X * make_x()
{
struct mutable_X * p = malloc(sizeof *p);
if (p)
{
p->type = strdup("X");
if (p->type == NULL)
{
free(p);
p = NULL;
}
}
return p; // ok: X differs from mutable_X only in having more qualifiers
}

Regarding freeing, we fix that with a different freeing interface:

extern void cvfree(const volatile void *p);

void x_destroy(struct X * p)
{
cvfree(p->type); //no warning: conversion only adds qualifiers
}

Freeing is not mutation; it makes sense to free via a qualified
pointer. Const objects can die, e.g:

{
const int x = 42;
}

The free function taking a void * is a misfeature in the C library.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: "not-const" qualifier for C

<29954376-ec3c-41ca-b1c7-27b4faab6625@gmail.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=52846&group=comp.lang.c#52846

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.adams@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: "not-const" qualifier for C
Date: Wed, 24 Apr 2024 22:36:07 -0300
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <29954376-ec3c-41ca-b1c7-27b4faab6625@gmail.com>
References: <v0bma4$2g9n9$1@dont-email.me> <20240424133951.155@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 25 Apr 2024 03:36:07 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="e2f190ba4164bfaf720bf8d82a3ddbc4";
logging-data="2782053"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+WtC9Vzi09Gpm/MsBdbJ0c+nM3+oz2JQY="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:dSoEqQJUXOVFeCreKeK86xsq5oY=
Content-Language: en-GB
In-Reply-To: <20240424133951.155@kylheku.com>
 by: Thiago Adams - Thu, 25 Apr 2024 01:36 UTC

Em 4/24/2024 8:13 PM, Kaz Kylheku escreveu:
> On 2024-04-24, Thiago Adams <thiago.adams@gmail.com> wrote:
>> Motivation sample:
>>
>> struct X {
>> const char* const type;
>> };
>>
>> struct X * make_x(){
>> struct X * p = malloc(sizeof *p);
>> if (p)
>> {
>>
>> p->type = strdup("X"); // *** error, type is const ***
>>
>> if (p->type == NULL)
>> {
>> free(p);
>> p = NULL;
>> }
>> }
>> return p; //ok
>> }
>
> Different idea: allow all conversions without a cast which only
> add qualifiers anywhere in the type:
>
> struct X {
> const char* const type;
> };
>
> struct mutable_X {
> char* type;
> };

In this case the types struct X and struct mutable_X are not
convertible. They are not the same type.

I forgot to put a sample but

void f(struct X x);
mutable struct X x;
f(x); //ok from mutable to const is fine

Re: "not-const" qualifier for C

<20240424183818.814@kylheku.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=52847&group=comp.lang.c#52847

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 643-408-1753@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: "not-const" qualifier for C
Date: Thu, 25 Apr 2024 01:47:45 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 61
Message-ID: <20240424183818.814@kylheku.com>
References: <v0bma4$2g9n9$1@dont-email.me> <20240424133951.155@kylheku.com>
<29954376-ec3c-41ca-b1c7-27b4faab6625@gmail.com>
Injection-Date: Thu, 25 Apr 2024 03:47:45 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="beacde28bb5b90842ad087fd24932ac8";
logging-data="2784043"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/mw7FzhduDvd/DGhi8XhGDyjINGMCMmAQ="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:ILTacgnIVMWio49qluAOfhUNuC4=
 by: Kaz Kylheku - Thu, 25 Apr 2024 01:47 UTC

On 2024-04-25, Thiago Adams <thiago.adams@gmail.com> wrote:
>
>
> Em 4/24/2024 8:13 PM, Kaz Kylheku escreveu:
>> On 2024-04-24, Thiago Adams <thiago.adams@gmail.com> wrote:
>>> Motivation sample:
>>>
>>> struct X {
>>> const char* const type;
>>> };
>>>
>>> struct X * make_x(){
>>> struct X * p = malloc(sizeof *p);
>>> if (p)
>>> {
>>>
>>> p->type = strdup("X"); // *** error, type is const ***
>>>
>>> if (p->type == NULL)
>>> {
>>> free(p);
>>> p = NULL;
>>> }
>>> }
>>> return p; //ok
>>> }
>>
>> Different idea: allow all conversions without a cast which only
>> add qualifiers anywhere in the type:
>>
>> struct X {
>> const char* const type;
>> };
>>
>> struct mutable_X {
>> char* type;
>> };
>
> In this case the types struct X and struct mutable_X are not
> convertible. They are not the same type.

And your mutable keyword is a syntax error in the current C language!

The idea is that we allow "pointer to struct mutable_X" to convert to
"pointer to struct X" without a cast, because the two types are
structurally equivalent, and every element of the destination type at
least as qualified as its counterpart in the source type.

I don't entirely like the idea because it entails structural
equivalence. That has various problems. Ideological: we are used
to "struct foo" and "struct bar" (in the same translation unit)
being different types. Structural equivalence requires complete
types. We could end up with the situation where "foo *" cannot
convert to "bar *" without a cast in one scope in the program
where foo is an incomplete type, but in another scope, foo is
completed in a way that the assignment is compatible.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: "not-const" qualifier for C

<pan$d437b$d3f83528$9e2423e3$90528bc4@invalid.invalid>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=52848&group=comp.lang.c#52848

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!bluemanedhawk.eternal-september.org!.POSTED!not-for-mail
From: bluemanedhawk@invalid.invalid (Blue-Maned_Hawk)
Newsgroups: comp.lang.c
Subject: Re: "not-const" qualifier for C
Date: Thu, 25 Apr 2024 03:47:19 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <pan$d437b$d3f83528$9e2423e3$90528bc4@invalid.invalid>
References: <v0bma4$2g9n9$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 25 Apr 2024 05:47:19 +0200 (CEST)
Injection-Info: bluemanedhawk.eternal-september.org; posting-host="53130bda7eb8b4cb86366e3b7f75e7f3";
logging-data="2948172"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18X90wjIGqC2lDkElz8HPk34oy86zr0J4Y="
User-Agent: Pan/0.154 (Izium; 517acf4)
Cancel-Lock: sha1:jpsHujM3LWiugkQF+VaR/CYQPlI=
X-Face: Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronuku
pokaiwhenuakitanatahu
Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAIAAADYYG7QAAACh0lEQVRYw71Z21bD
MAzzevbfkr4cHjrSXJyL044+MDa6WLEl2SkvkrZ1AbAvXO+bUGSCPYnsuIVGMpm
ZLnjX718GhAKNsp8lON2F9VrhELwIgJlBepkZjA78rVK+FkmNhEJK76UsJlz8+E
rJsjrpYouhLo/SC6qPHgakFOR8wV9+8rCfO/I/oVnmUZUp42/LW2XkLj9TCFNM9
jp5g2EmHZgpYZjCOkYU7sXVogRylJqpdggoFLG1g09Flah/7kErCxzR9HgXPYsq
0glb9cxjIz2Vsk9AmAoCSxECpD713joMKjQqLAtmMqJmXjdVvlMnMQCVITotJd1
z+fh1f1NNo+vuc1KnhWUmY7t03vydTud9BbXCtN3L2PL3bK7JCNG0GHzuZxafyB
fxevCxpm1vrwZltqw6SILCcdoCE6PGQC8wZWDA9Or7Qp5s3lAZezys0nDazs9S9
R0TjwEiksRxLkNPC1NMMWPs1bj0Ei0Yuo+JVtFLuzP1NRJ16qXWN8DhhtmS4PDg
O6mqRxs4bEJrYt087mSIow/1VzW2oFlMQuiuIy/KsUagvhdw6hSjJGlIavbLF8x
j3X47bccLcUSi0dkWh1nUZNhANT1tHKUXrNxNLbd9KPb9wDDVrKwmPQMOPQ1oy6
k5I1DwzDeRJd3jVIhDAUxq3ngzJG4CCkNXZxZVMcjefoK2J0gUY2S3rxz/RuTFx
2zHd9U+obimJXMG4edsk/2j5pTU5G1MmzbRLxkfq5EiT1GGsidvMGzi+1goGb2l
GCrN+nGnV8xj3q3JLRDVPL96vUc7Z4aJ3TN1mVqWAMJMfG+Jxh6TQqP+92iZkCU
xtglds1AB6r0aiSHKcnFck+p/c/0CbacFLQcajGcAAAAASUVORK5CYII=
 by: Blue-Maned_Hawk - Thu, 25 Apr 2024 03:47 UTC

If you wish to have certain properties of a struct be changeäble only by
specific subroutines, not the programmer, then you can use an opaque
struct.

--
Blue-Maned_Hawk│shortens to Hawk│/blu.mɛin.dʰak/│he/him/his/himself/Mr.
blue-maned_hawk.srht.site
You can also reconsider what it is that you're doing.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor