This module does not comply with standard 42 because it uses a global variable.
It is intended solely as a development tool and should not be included in your final renderings.
If you have any questions I'm available on slack under the username aderison (intra 42)
ft_malloc is a module designed to help École 42 students detect and understand memory leaks in their C projects. It offers a simple, educational alternative to more complex tools like Valgrind, while remaining accessible to beginners.
The main objective of ft_leaks is to :
- Trace all memory allocations in your program.
- Provide information on each allocation (size, name, ft_malloc line number).
- Allow controlled release of memory.
- Help identify potential memory leaks.
ft_malloc()
: Replacesmalloc()
to allocate memory while tracking it.ft_free()
: Replacesfree()
to free memory while updating tracking.ft_putalloc()
Displays all unreleased memory allocations otherwise displays➜ No malloc
.
- Each call to
ft_malloc()
allocates memory and stores the details in a linked list. - Information stored includes: pointer, size, name (given by user), and line of code.
ft_free()
frees memory and removes the corresponding entry from the tracking list.- At the end of the program, any allocation not freed is considered a potential leak.
- make
- Include “ft_leaks.h” in your source files.
- Replace all calls to
malloc()
withft_malloc(size, “name”, __LINE__)
. - Replace all calls to
free()
withft_free()
. - Compile your program with
gcc -o prog src/main.c -L. -lft_leaks -I./include
. With this architecture, you can modify the compilation command to suit your needs.
├── Makefile
├── include
│ └── ft_leaks.h
└── src
├── ft_free.c
├── ft_leaks.c
├── ft_malloc.c
├── ft_putalloc.c
├── main.c
└── utils
├── ft_putstr.c
└── ft_strlen.c
Exemple :
#include "ft_leaks.h"
int main()
{
char *str;
str = ft_malloc(sizeof(char) * (9 + 1), "ma_chaine", __LINE__);
//init de str
ft_free(str);
//Display allocations not released
ft_putalloc();
return 0;
}
- Not compliant with standard 42: Use of a global variable.
- Slight performance overload due to allocation tracking.
- Requires source code modification to use.
- Impossible to release all pointers at once, as the project is not up to standard and therefore not usable.
- Use descriptive names for your allocations to facilitate debugging.
- Regularly check the list of unreleased allocations during development.
- Don't forget to replace ft_malloc and ft_free with malloc and free in your final code before submitting it.
This project is open to improvement. Feel free to suggest modifications to make it more useful or effective, while keeping in mind that it must remain simple and educational.
Remember: ft_malloc is a learning tool. For real projects or production environments, use more robust and proven memory leak detection tools.