Sujet : Re: So You Think You Can Const?
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 09. Jan 2025, 20:47:49
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20250109114258.473@kylheku.com>
References : 1 2 3 4
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2025-01-09, Keith Thompson <Keith.S.Thompson+
u@gmail.com> wrote:
Julio Di Egidio <julio@diegidio.name> writes:
On 08/01/2025 17:48, Andrey Tarasevich wrote:
[...]
It is perfectly safe. One can even argue that standard declaration
if `free` as `void free(void *)` is defective. It should have been
`void free(const void *)` from the very beginning.
>
I do not understand that: `free` is changing the pointed data, so how
can `const void *` even be "correct"?
>
No, `free` doesn't (necessarily) change the pointed-to data.
Any attempt to access the allocated data after free() has undefined
behavior, so it might be modified, but all free() needs to do is
make it available for further allocation. It might do so without
touching the data itself.
It doesn't matter because if free were to change the pointed-to
data, that would be only wrong if the effective type were const.
The only way an allocated block acquires an effective type is
when its value is stored; it then inherits the type of lvalue
expression.
An expression of const type isn't a modifiable lvalue that could
be used to store to the object. (If an implementation allows the
modification, in spite of emitting the required diagnostic at
translation time, the behavior is then no longer defined.)
Therefore, an effective type for an allocated block cannot ever be
const-qualified (in a program that has not already run into undefined
behavior so far prior to the call to impending call to free).
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca