A lightweight C library that automates memory cleanup. It ensures all allocated memory is properly freed in case of allocation fail or at program exit. In short it's simplifying memory management and minimizing memory leaks.
Clone repository
git clone https://github.com/Emsa001/ft_destructor
Compile to ft_alloc.a
cd ft_destructor
make
Add to your application
cc main.c ft_alloc.a -o my_program
- Init / Get the list with all allocations
t_allocs *ft_allocs(t_allocs *lst);
- Allocations
void *ft_malloc(size_t size);
void *ft_calloc(size_t count, size_t size)
- Destructors
void ft_free(void *ptr);
void ft_destructor(void);
#include "ft_alloc.h"
int main(void)
{
// init list for allocations
ft_alloc_init();
// allocations with ft_malloc
char *str = ft_malloc(455 * sizeof(char));
char *str2 = ft_malloc(455 * sizeof(char));
char *str3 = ft_malloc(455 * sizeof(char));
// manual free
ft_free(str);
ft_free(str2);
ft_free(str3);
int i = 0;
while (i < 10)
{
// allocation without ft_free
char *str4 = ft_malloc(455 * sizeof(char));
(void)str4;
i++;
}
// original malloc - won't be freed with destructor
char *str5 = malloc(455 * sizeof(char));
(void)str5;
// will free everything that was allocated with ft_malloc or ft_calloc
ft_destructor();
return (0);
}
If you have any feedback, please reach out to me at discord: emsa001