bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Troubles with dispose function on oset


From: Bruno Haible
Subject: Re: Troubles with dispose function on oset
Date: Tue, 27 Nov 2007 13:50:03 +0100
User-agent: KMail/1.9.1

Hi,

> I have written my dispose
> function, I get as parameter a void pointer,
> which it seems I must free too. However its prototype is like:
> 
>     typedef void (*gl_setelement_dispose_fn) (const void *elt);
> 
> Then, if I declare my function with a non-const type as first
> argument, it will warn me by conflict at prototype.
> On the other hand, I declare it as const. It warn me if I try to do
> free() on it.

The elements have a type of 'const void *' because the ordered-set
implementation does not need write access to the memory pointed to by the
pointers.

But here, in the dispose function, you need write access. So you need to
cast from 'const void *' to 'void *' inside your dispose function. The
cast is valid because you guarantee that you have allocated each element
with malloc(), realloc(), or calloc().

(In C++, one would have used a template with a type parameter that would be
either 'const foo *' or 'foo *'. But this here is C, not C++.)

Bruno




reply via email to

[Prev in Thread] Current Thread [Next in Thread]