Deutsch   English   Français   Italiano  
<vcfcmv$2mgp$1@dont-email.me>

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

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Thiago Adams <thiago.adams@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: When Immutability and Nullability Meet: Exploring Mutable Objects
 in C
Date: Wed, 18 Sep 2024 17:20:46 -0300
Organization: A noiseless patient Spider
Lines: 59
Message-ID: <vcfcmv$2mgp$1@dont-email.me>
References: <vccs7e$3mu1n$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 18 Sep 2024 22:20:47 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="554aff38f275d912145131404b9bdc0b";
	logging-data="88601"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1//rofWOcfRMhHwSaRReQ1+sWnceGBWjr0="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:VSdZucig705i6Kr3b86qb3wzrAk=
Content-Language: en-US
In-Reply-To: <vccs7e$3mu1n$1@dont-email.me>
Bytes: 2204

On 17/09/2024 18:27, Thiago Adams wrote:
> 
> I found some use cases where we may want disable const and _not_null (if 
> C had this qualifier) at same time. Kind of "anti-qualifier".
I think



I think my post might not have any replies because it's too long. So 
here's a shorter version.

Consider this sample: It has a error and a warning

#include <stdlib.h>
#include <string.h>

struct person {
   const char* const name;
};

struct person * make(const char* name)
{
   struct person * p = malloc(sizeof *p);
   if (p == 0) return 0;
   p->name = strdup(name);  //error read-only
   return p;
}

void person_delete(struct person* p) {
     free(p->name); //warning
}

int main()
{
   const struct person * p = make("a");
   if (p){
      person_delete(p); //warning
   }
   return 0;
}

My suggestion is remove the error and warning by adding a new qualifier 
mutable

mutable struct person * p = malloc(sizeof *p);

That allows p->name to be changed and person_delete to be called with no 
warnings.

void person_delete(mutable struct person* p) {
     free(p->name); //ok
}

https://godbolt.org/z/5Yo63ab1Y