Re: Argument of free() should be const void*
- From: Stewart Brodie <stewart.brodie@xxxxxxxxxxxx>
- Date: Tue, 11 Oct 2005 09:29:52 GMT
des@xxxxxx (Dag-Erling Smørgrav) wrote:
> Rudolf <rthered@xxxxxxxxxxx> writes:
> > I don't follow how this happens. Doesn't malloc return void* ?
>
> It is perfectly allowable to pass void * to a function expecting const
> void *, so this is no argument against the proposed change.
Changing the prototype of free to take a const-qualified pointer changes the
type of the function. Whilst a void* can be implicitly qualified to const
void*, the existing type "void (*)(void *)" does not impicitly cast to the
proposed "void (*)(const void *)", requiring code changes to anything that
stores the address of free().
In my experience, it is quite common to wrap memory allocation calls with
thin veneers that monitor the dynamic memory allocations for the purposes of
recording them, debugging them, redirecting them to a different memory
manager.
> > If your program is changing the type of the pointer from malloc to
> > something incompatible with free, well, then, don't do that. Or,
> > understand that that you will have to undo that before you free the
> > memory.
>
> It's not always that simple in real life.
The project on which I am currently working actually has a free_const()
function that takes a const-qualified pointer, casts the const away and
passes the unqualified pointer on to the free() function. I'm not sure if I
like that all that much, but it gets the job done without requiring casts at
the point of use.
Another thing I do from time to time is to allocate some memory with
malloc() on application initialisation, initialise it to some value that has
to be computed dynamically but is otherwise unchanging, and then store the
address of that memory in a const-qualified manner to guarantee (in as much
as it can be guaranteed) that the data is not altered whilst the application
is running normally.
--
Stewart Brodie
.
- Follow-Ups:
- Re: Argument of free() should be const void*
- From: Keith Thompson
- Re: Argument of free() should be const void*
- References:
- Argument of free() should be const void*
- From: Chris Croughton
- Re: Argument of free() should be const void*
- From: Rudolf
- Re: Argument of free() should be const void*
- From: Dag-Erling Smørgrav
- Argument of free() should be const void*
- Prev by Date: Re: Argument of free() should be const void*
- Next by Date: Re: Argument of free() should be const void*
- Previous by thread: Re: Argument of free() should be const void*
- Next by thread: Re: Argument of free() should be const void*
- Index(es):
Relevant Pages
|