Sujet : Re: So You Think You Can Const?
De : andreytarasevich (at) *nospam* hotmail.com (Andrey Tarasevich)
Groupes : comp.lang.cDate : 11. Jan 2025, 17:38:40
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vlu6qh$miie$1@dont-email.me>
References : 1 2 3 4 5
User-Agent : Mozilla Thunderbird
On 01/10/25 10:37 AM, Kaz Kylheku wrote:
Destruction by malloc is modifying in any system that recycles the
memory for another allocation.
From such a radically "physical" point of view, nothing is and nothing will ever be `const`... Sorry, this is not even close to what "constness" in C is about.
In C and C++ (as well in virtually all higher level languages) "constness" is not a physical concept. It is a purely high-level logic-level concept, designed, implemented and enforced entirely by the author of the code (of the public interface of the module) in accordance with their intent.
It has absolutely no relation to any physical modifications that might occur anywhere in the execution environment. Nobody cares whether something somewhere gets "modified". It is always a question of whether _I_ want to recognize such modifications as part of the public interface designed by me. I'm the one who says whether the operation is "constant" or not, based purely on my idea of "logical constness".
That's the reason `const` exists in C (and C++).
However (returning to the more narrowly focused matter at hand), two things - creation and deletion of objects - will always indisputably stand apart as operations that transcend/defeat/ignore the idea of "constness" with relation to the object itself. Creation/deletion might logically be seen as "non-constant" wrt to the surrounding environment (e.g. memory manager), but wrt to the object itself they shall not (and, obviously, cannot) care about its "constness" at all.
An object begins being `const` only after the process of its creation (construction) is complete. An object stops being `const` the moment the process of its destruction begins. That's how it works in C and C++. (Again, using C++ as an example, you all know that `const` objects are not seen as `const` inside their constructors and destructors.) In C we don't have constructors and destructors, but we still naturally follow the same ideas in hand-written code. It would've been nice to have `free` to play along with this.
-- Best regards,Andrey