Sujet : Re: So You Think You Can Const?
De : andreytarasevich (at) *nospam* hotmail.com (Andrey Tarasevich)
Groupes : comp.lang.cDate : 10. Jan 2025, 07:01:53
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vlqd4j$3s4ai$1@dont-email.me>
References : 1 2 3
User-Agent : Mozilla Thunderbird
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.
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.
So, destruction is not really a "modifying" operation. Destruction of an object is a special case, it's a meta-operaton, which needs special treatment. We have to be able to destroy `const` objects as well, regardless of how they were created/allocated. And in C destruction of dynamically created objects is essentially embodied in `free`. So, `free` is a meta-operation, which has to be able to destroy `const` objects.
This, for example, is mirrored in C++ as well, where `delete` is immediately applicable to pointers to `const` objects. No reason to make `free` behave differently in C.
-- Best regards,Andrey