Small library for removing all unwanted memory leaks, caused by forgetting to deallocate memory.
Small library for removing all unwanted memory leaks, caused by memory allocators/deallocators.
- Download header and source file.
- Include header into your source file when you want to use it.
- Use!
First, you need to initialize listing. To do so, execute function and pass maximum count of pointers program will use.
init_listing(4); // Allocate memory for 4 pointers
You have two functions for allocating memory: lmalloc() and lcalloc(). All of them take exactly one argument - memory size, and return pointer. Don't forget to check if it's valid!
void* ptr1 = lmalloc(32); // Allocate raw memory block 32 bytes size
void* ptr2 = lcalloc(64); // Allocate zero'ed out memory block 64 bytes size
There is one function to free pointer: lfree(). Works only with pointers created using lmalloc() or lcalloc().
lfree(ptr1);
At the end of program (or function) you can put free_all() function to free all pointers created with library, and prevent memory leaks.
free_all();
This is an example.