[neomutt-devel] why casting pointer in mutt_mem_free
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)
> void **p = (void **) ptr;
> if (*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...
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 890 bytes
Desc: not available
More information about the neomutt-devel