Deutsch   English   Français   Italiano  
<vltjqc$j8n0$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

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: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: So You Think You Can Const?
Date: Sat, 11 Jan 2025 12:14:20 +0100
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <vltjqc$j8n0$1@dont-email.me>
References: <vljvh3$27msl$1@dont-email.me> <vlma9m$2s5e5$1@dont-email.me>
 <vlolsf$3cnll$4@dont-email.me> <vlqd9p$3s4ai$2@dont-email.me>
 <vlqstb$3uk5j$1@dont-email.me> <87v7umpkfv.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 11 Jan 2025 12:14:20 +0100 (CET)
Injection-Info: dont-email.me; posting-host="e188b3ebf2ec4ccd5ef86f7d32308ebf";
	logging-data="631520"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/Yksi+xqzaryLNB/8zB+rkGYv/AIw+dzI="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:gm+HwcnGs6PuR2DjmTep205iU+0=
Content-Language: en-GB
In-Reply-To: <87v7umpkfv.fsf@nosuchdomain.example.com>
Bytes: 2743

On 10/01/2025 19:56, Keith Thompson wrote:
> David Brown <david.brown@hesbynett.no> writes:
> [...]
>> If you want a better signature for "free", then I would suggest "void
>> free(void ** p)" - that (to me) more naturally shows that the function
>> is freeing the pointer, while also greatly reducing the "use after
>> free" errors in C code by turning them into "dereferencing a null
>> pointer" errors which are more easily caught by many OS's.
> 
> I'm not sure that would work.  A void** argument means you need to pass
> a pointer to a void* object.  If you've assigned the converted result of
> malloc() to, say, an int* object, you don't have a void* object.  (int*
> and void* might not even have the same representation).
> 

Yes, you are right - while "free(void ** p)" might often be feasible in 
practice (since on most implementations, pointers are the same size and 
representation) it would at a minimum rely on compilers being somewhat 
lax about accepting these conversions.  Certainly it is not something 
that could be part of the standard.

The idea was to place the emphasis on "free" changing the pointer, 
rather than the data pointed to.  But it could not be done as simply as 
I had suggested.

> Some kind of generic function that takes a pointer to an object of any
> object pointer type could work, but the language doesn't support that.
> (C++ addressed this by making `new` and `delete` built-in operators
> rather than library functions.)
>