Deutsch   English   Français   Italiano  
<20250110122353.00005377@yahoo.com>

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

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Michael S <already5chosen@yahoo.com>
Newsgroups: comp.lang.c
Subject: Re: So You Think You Can Const?
Date: Fri, 10 Jan 2025 12:23:53 +0200
Organization: A noiseless patient Spider
Lines: 59
Message-ID: <20250110122353.00005377@yahoo.com>
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>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 10 Jan 2025 11:23:59 +0100 (CET)
Injection-Info: dont-email.me; posting-host="94c17ecaa93f2792be113f3db83a13ee";
	logging-data="4165085"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+2aoSBnEjSSfVPtFJMPmrSjGg7/Q5Urfw="
Cancel-Lock: sha1:wD7nWzmZUppZ39TaWxHcXOuxbRs=
X-Newsreader: Claws Mail 4.1.1 (GTK 3.24.34; x86_64-w64-mingw32)
Bytes: 3529

On Thu, 09 Jan 2025 23:40:52 -0800
Keith Thompson <Keith.S.Thompson+u@gmail.com> 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 believe that the Standard really says that, but find the part about
value of ptr variable ridiculous. It breaks natural hierarchy by which
standard library is somewhat special, but it is not above rules of core
language. free() is defined as function rather than macro. By rules of
core language, a function call can not modify the value of local
variable at caller's scope, unless pointers to the variable was passed
to it explicitly.


> Having said that, it's likely that such an attempt will not be
> diagnosed, and that the values of ptr and *ptr will be *appear* to be
> the same before and after calling free().  (Though the memory
> management system might update *ptr, depending on the
> implementation.)  But this is outside the scope of what C defines,
> and there are no guarantees of *anything*.
> 
> > Every object in C object model has to be created (when its lifetime
> > begins) and has to be eventually destroyed (when its lifetime
> > ends). This applies to all objects, including `const` ones
> > (!). Lifetime of a `const` objects also ends eventually, which means
> > that `const` object has to be destroyable. No way around it.  
> 
> An object with static storage duration (either defined with the
> "static" keyword or defined at file scope) has a lifetime that ends
> when the program terminates.  In a typical implementation, the
> destruction of such an object doesn't do anything other than
> deallocating its memory.
> 
> [...]
>