[neomutt-devel] why casting pointer in mutt_mem_free

Pietro Cerutti gahr at gahr.ch
Tue Mar 27 09:35:47 CEST 2018

On Mar 27 18 15:04, Yubin Ruan wrote:
>In mutt/memory.c, what is point of casting ptr from (void *) to (void **)? Any
>ancient C technique here?
>void mutt_mem_free(void *ptr)
>  if (!ptr)
>    return;
>  void **p = (void **) ptr;
>  if (*p)
>  {
>    free(*p);
>    *p = 0;
>  }

Usage would be:

char *c = mutt_mem_malloc(10);
assert(c == NULL);

That is, calling mutt_mem_free sets the pointer object pointed-to by the 
argument to NULL. For doing this, mutt_mem_free needs to get passed a 
pointer to the object, which is itself a pointer variable. Hence the &c 
in the invokation.

Now, any pointer is implicitely convertible to void* as per the C 
standard, but not to void**. This is why the explicit cast inside 
mutt_mem_free is needed to get back the original pointer-to-pointer from 
the expected pointer object.

Hope this helps... 

Pietro Cerutti
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 890 bytes
Desc: not available
URL: <http://mailman.neomutt.org/pipermail/neomutt-devel-neomutt.org/attachments/20180327/51295a77/attachment.sig>

More information about the neomutt-devel mailing list