memdel - AugustinLopez/libft GitHub Wiki

Introduction

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)

Implementation

#include <stdlib.h>

void	memdel(void **ap)
{
	if (!*ap)
		return ;
	free(*ap);
	*ap = NULL;
}

Note

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);

Further Reading

⚠️ **GitHub.com Fallback** ⚠️