Deutsch   English   Français   Italiano  
<vlv93l$sa38$1@dont-email.me>

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

Path: ...!weretis.net!feeder9.news.weretis.net!news.quux.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: So You Think You Can Const?
Date: Sat, 11 Jan 2025 18:23:48 -0800
Organization: A noiseless patient Spider
Lines: 99
Message-ID: <vlv93l$sa38$1@dont-email.me>
References: <vljvh3$27msl$1@dont-email.me> <vlma9m$2s5e5$1@dont-email.me>
 <vlo0cm$2dkpd$9@dont-email.me> <vlqd4j$3s4ai$1@dont-email.me>
 <874j27qfp7.fsf@nosuchdomain.example.com> <vlstnv$foh8$1@dont-email.me>
 <8ee6d6882fb3170c140eca7f8c70cc9799de25f7@i2pn2.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 12 Jan 2025 03:23:50 +0100 (CET)
Injection-Info: dont-email.me; posting-host="5328dbe39382283b57a1e72b814449ac";
	logging-data="927848"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19SWVfTtly0DYuQ1VOB9yw0/hG9vEGkOS0="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:kmWDnXQjWFc/P3sR6bg9Kn3hE0A=
Content-Language: en-US
In-Reply-To: <8ee6d6882fb3170c140eca7f8c70cc9799de25f7@i2pn2.org>
Bytes: 4225

On 1/11/2025 6:16 AM, Richard Damon wrote:
> On 1/10/25 11:57 PM, Chris M. Thomasson wrote:
>> On 1/9/2025 11:40 PM, Keith Thompson wrote:
>>> Andrey Tarasevich <andreytarasevich@hotmail.com> writes:
>>>> On 01/09/25 12:12 AM, Julio Di Egidio wrote:
>>>>> I do not understand that: `free` is changing the pointed data, so
>>>>> how can `const void *` even be "correct"?
>>>>
>>>> `free` is destroying the pointed data.
>>>
>>> Right.  In other words, it causes the pointed-to data to reach the end
>>> of its lifetime.  "Changing" the data generally means modifying its
>>> value (that's what "const" forbids).
>>>
>>> Given:
>>>
>>>      int *ptr = malloc(sizeof *ptr);
>>>      *ptr = 42;
>>>      printf("*ptr = %d\n", *ptr);
>>>      free(ptr);
>>>
>>> After the call to free(), the int object logically no longer exists.
>>> Also, the value of the pointer object ptr becomes indeterminate.
>>> Attempting to refer to the value of either ptr or *ptr has undefined
>>> behavior.
>>
>> I must be missing something here. Humm... I thought is was okay to do 
>> something like this:
>> _____________________________
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> int main() {
>>      int* a = malloc(sizeof(*a));
>>
>>      if (a)
>>      {
>>          *a = 42;
>>
>>          printf("a = %p\n", (void*)a);
>>          printf("*a = %d\n", *a);
>>
>>          free(a);
>>
>>          printf("a = %p was just freed! do not deref\n", (void*)a);
>>      }
>>
>>      return 0;
>> }
>> _____________________________
>>
>> Is that okay?
>>
>> [...]
> 
> No, because the value of a has become indeterminate, and operating on 
> it, even to just look at its value, can trap.

Argh! Shit. Thanks.


> you could save a representation of it either in a char array or as a 
> uintptr_t value, and work with that (but not try to recreate a pointer 
> with it, as that pointer "value" has become indeterminate).
> 
> This issue CAN occur if the implementation is using segment_tag + offset 
> pointers, and free invalidates the segment_tag of that the pointer used, 
> and the implementation will perhaps validate the segment_tag when 
> looking at the pointer value. (perhaps pointers are loaded into 
> registers that automatically validate the segment_tag in them).

Any better?

________________________________
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>

int main() {
     int* a = malloc(sizeof(*a));

     if (a)
     {
         *a = 42;

         printf("a = %p\n", (void*)a);
         printf("*a = %d\n", *a);

         uintptr_t x = (uintptr_t)a;

         free(a);

         printf("x = %" PRIxPTR " was just freed! do not deref\n", x);
     }

     return 0;
}
________________________________