Path: ...!weretis.net!feeder9.news.weretis.net!news.quux.org!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: Wed, 08 Jan 2025 12:24:47 -0800 Organization: A noiseless patient Spider Lines: 25 Message-ID: <86bjwh835c.fsf@linuxsc.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Wed, 08 Jan 2025 21:24:48 +0100 (CET) Injection-Info: dont-email.me; posting-host="6e19b66cb0c542b6b8c649f7413a0cd9"; logging-data="3071468"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+a4YWK746yhH8soULq11HHsHYRiIQC1X8=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:ffFolfqwG0e6gG0QwtU6a03tQLQ= sha1:UyRhGmrs5YnacC+jNWIg72g6dp8= Bytes: 2067 Andrey Tarasevich writes: > On 01/07/25 11:32 AM, Julio Di Egidio wrote: > >> Assuming, as said, that the data was originally >> allocated with malloc, is [calling free on a pointer >> to const something] safe or something can go wrong >> even in that case? > > 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 think declaring the parameter as 'void *' rather than 'const void *' is a better choice. There is a fair chance that calling free() on a pointer to const anything is a programming error, and it would be good to catch that. If it isn't an error, then fixing the diagnostic is trivial. If it's a common pattern one could even define an inline function static inline void cfree( const void *p ){ free( (void*)p ); } and call that instead of free(). (Obviously the 'inline' should be taken out if compiling as C90.)