This project is deprecated in profit of funcheck which is more easy to use and more powerful.
![Logo](/froz42/ft_mallocator/raw/master/ressources/logo.png)
Test your allocs protections and leaks ! Report Bug · Request Feature
Table of Contents
This tool allows you to test if every allocs in your project are protected. It also checks if an alloc fail: if you free every allocation.
It also checks leaks !
Here, you will see how to setup this tool in an example project.
This is working out of the box on 42's dump.
You must compile using clang
At home, you will need those installed on your system :
sudo apt install make clang
- Clone the repo inside your project
git clone https://github.com/tmatis/ft_mallocator.git
- Cd in directory
cd ./ft_mallocator
- Execute script
bash test.sh
- Follow script's instructions...
record.mp4
Here we have an example of not well protected code:
char *malloc_function(void)
{
return (malloc(1000));
}
int main(void)
{
void *ptr = malloc_function();
if (ptr == NULL)
return (1);
free(ptr);
void *array[10];
for (int i = 0; i < 10; i++)
{
array[i] = malloc(1000);
if (array[i] == NULL)
{
printf("it fail %i\n", i);
return (0);
}
memset(array[i], 0, 1000);
}
for (int i = 0; i < 10; i++)
free(array[i]);
ptr = malloc_function();
memset(ptr, 0, 1000);
free(ptr);
}
This example have many problems, let's run mallocator on it :
The first allocation is well protected, nothing to say. The second allocation is protected but if a malloc fails, not everything is freed (the program returns without freeing the previous allocations). The third allocation is not protected at all, there is no null check.
Let's see a well protected example:
char *malloc_function(void)
{
return (malloc(1000));
}
int main(void)
{
void *ptr = malloc_function();
if (ptr == NULL)
return (1);
free(ptr);
void *array[10];
int i = 0;
for (i = 0; i < 10; i++)
{
array[i] = malloc(1000);
if (array[i] == NULL)
{
printf("it fail %i\n", i);
break ;
}
memset(array[i], 0, 1000);
}
for (int j = 0; j < i; j++)
free(array[j]);
ptr = malloc_function();
if (!ptr)
return (1);
memset(ptr, 0, 1000);
free(ptr);
}
Then we run mallocator on this code :
This code is protected.
You can see how it is working behind the scene here.
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
- Théo Matis [email protected]