-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lstdelone.c
56 lines (48 loc) · 2.24 KB
/
ft_lstdelone.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbrito-p <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/08 20:26:00 by mbrito-p #+# #+# */
/* Updated: 2023/05/08 20:26:00 by mbrito-p ### ########.fr */
/* */
/* ************************************************************************** */
// Takes as a parameter a node and frees the memory of
// the node’s content using the function ’del’ given
// as a parameter and free the node. The memory of
// ’next’ must not be freed.
// lst: The node to free.
// del: The address of the function used to delete
// the content.
#include "libft.h"
void ft_lstdelone(t_list *lst, void (*del)(void*))
{
// If the list node and the delete function exis
if(lst && del)
{
// Call the delete function on the content of the list node
del(lst->content);
// Free the memory allocated for the list node
free(lst);
// Set the pointer to the list node to NULL to prevent a dangling pointer
// A dangling pointer is a pointer that refers to a memory location that has been deallocated or freed.
// In other words, it is a pointer that points to an invalid or non-existent memory address.
lst = NULL;
}
}
// void delete_string(void *str)
// {
// free(str);
// }
// int main(void)
// {
// // Create a new node with a string "hello"
// t_list *node = ft_lstnew(strdup("hello"));
// // Call ft_lstdelone to delete the node
// ft_lstdelone(node, delete_string);
// // Print a message to indicate successful execution
// printf("Program completed successfully.\n");
// return 0;
// }