free(3) const void *

Alejandro Colomar alx@kernel.org
Fri Jan 26 23:25:05 GMT 2024


Hi DJ, Russ,

On Fri, Jan 26, 2024 at 04:47:38PM -0500, DJ Delorie wrote:
> Andreas Schwab <schwab@linux-m68k.org> writes:
> > Also, objects in the heap are never read-only.
> 
> I thought of that, but using "const" in a program can mean something
> other than "the object is read-only".  It can also act as a promise to
> the user and/or optimizer that an object is not written to, even if
> doing such is possible.
> 
> So the original problem of "having to keep a non-const pointer around
> just to free it" is valid.  My post was just to put me in the "too bad,
> deal with it" category ;-)

After some fresh air, I've been thinking about what Russ said, and it
makes a lot of sense.  While the attribute [[gnu::malloc()]] can tell
where some object is definitely terminated, there's no way to tell via
attributes where an object is definitely not terminated; you can only
guess that an object may or may not be terminated when a function
doesn't use the attribute.  So having `const` imply that an object is
not terminated is a feature.

If that means that we need to live with the fact that we need a writable
pointer to be able to free(3) an object, I guess the answer is indeed
"too bad, deal with it".  :-)

Maybe some day, attributes will give us full certainty about object
lifetime, but that day is not today.

Have a lovely night!
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/20240127/1462f10a/attachment-0001.sig>


More information about the Libc-alpha mailing list