Skip to content

Memory leak detection tool for 42 students. Tracks allocations, helps identify leaks. Simple and educational.

License

Notifications You must be signed in to change notification settings

arnaudderison/ft_leaks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ft_malloc - Memory leak detection tool for 42 students

⚠️ Important warning for students at école 42

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)

📚 Description

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.

🎯 Project goal

The main objective of ft_leaks is to :

  1. Trace all memory allocations in your program.
  2. Provide information on each allocation (size, name, ft_malloc line number).
  3. Allow controlled release of memory.
  4. Help identify potential memory leaks.

🛠 How it works

Main functions :

  1. ft_malloc(): Replaces malloc() to allocate memory while tracking it.
  2. ft_free(): Replaces free() to free memory while updating tracking.
  3. ft_putalloc() Displays all unreleased memory allocations otherwise displays ➜ No malloc.

Process :

  1. Each call to ft_malloc() allocates memory and stores the details in a linked list.
  2. Information stored includes: pointer, size, name (given by user), and line of code.
  3. ft_free() frees memory and removes the corresponding entry from the tracking list.
  4. At the end of the program, any allocation not freed is considered a potential leak.

📋 Use

  1. make
  2. Include “ft_leaks.h” in your source files.
  3. Replace all calls to malloc() with ft_malloc(size, “name”, __LINE__).
  4. Replace all calls to free() with ft_free().
  5. 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;
}

🚫 Limits

  1. Not compliant with standard 42: Use of a global variable.
  2. Slight performance overload due to allocation tracking.
  3. Requires source code modification to use.
  4. Impossible to release all pointers at once, as the project is not up to standard and therefore not usable.

💡 Tips

  • 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.

🤝 Contribution

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.

About

Memory leak detection tool for 42 students. Tracks allocations, helps identify leaks. Simple and educational.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published