memdel - AugustinLopez/libft GitHub Wiki
From the libft PDF subject:
| Name | ft_memdel |
|---|---|
| Prototype | void ft_memdel(void **ap); |
| Description | takes as a parameter the address of a memory area that needs to be freed with free(3), then puts the pointer to NULL. |
| Param. #1 | A pointer's address that needs its memory freed and set to NULL. |
| Return value | None. |
| Libc Functions | free(3) |
#include <stdlib.h>
void memdel(void **ap)
{
if (!*ap)
return ;
free(*ap);
*ap = NULL;
}
This implementation introduces one non-requested behavior:
- A NULL *ap pointer causes the function to end immediately. The pointer *ap might have been set to NULL by a previous call to memdel(). This behavior helps preventing double free by making subsequent call to memdel() safe.
In order to free an object pointed to by ptr with memdel(), it is recommended to use the following format. It ensures the intended behavior of the function without triggering compiler warning(s):
memdel((void **)&ptr);
- Dangling pointer
- Common Weakness - Use After Free : CWE - The MITRE Corporation