Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: So You Think You Can Const? Date: Thu, 09 Jan 2025 17:48:08 -0800 Organization: A noiseless patient Spider Lines: 31 Message-ID: <86y0zj782v.fsf@linuxsc.com> References: <878qrkqll8.fsf@nosuchdomain.example.com> <20250109114258.473@kylheku.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Fri, 10 Jan 2025 02:48:09 +0100 (CET) Injection-Info: dont-email.me; posting-host="0681eefa40b39ffce3ec62410cd69ade"; logging-data="3813076"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18BWq5qIe6bGW4ZivtR83Brnnm+6rZvFOQ=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:/RlgTvINIBg16FIlDgXW6Qykfhs= sha1:1Pfu885uq8yDhZKXii+2OHX6k4Q= Bytes: 2344 Kaz Kylheku <643-408-1753@kylheku.com> writes: > On 2025-01-09, Keith Thompson wrote: > >> Julio Di Egidio 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. > [...] Effective type is irrelevant. This particular undefined behavior occurs only when an object _defined_ with a const-qualified type is changed. There are no objects defined in a block of memory allocated by malloc().