free(3) const void *

Alejandro Colomar alx@kernel.org
Fri Jan 26 15:33:49 GMT 2024


Hi Andreas,

On Fri, Jan 26, 2024 at 04:13:55PM +0100, Andreas Schwab wrote:
> On Jan 26 2024, Alejandro Colomar wrote:
> 
> > It is sometimes (often?) useful to allocate some object, write
> > immediately to it, and then use it read-only.  The standard definition
> > of free() forces the programmer to keep a non-const pointer around just
> > for the sake of freeing, which is unnecessarily dangerous.
> 
> It is even more dangerous to pretend that the contents are unmodified,
> when in fact they are no longer accessible (the memory can be reused any
> time in any way).

But that's why [[gnu::malloc(deallocator)]] exists, to helps determine
the lifetime of an object (although it's imperfect, since it doesn't
take into account reallocations).

Have a lovely day,
Alex

-- 
<https://www.alejandro-colomar.es/>
Looking for a remote C programming job at the moment.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20240126/4dda4097/attachment.sig>


More information about the Libc-alpha mailing list