Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
|
Header file that contains macros used to replace malloc/calloc and free. More...
Go to the source code of this file.
Macros | |
#define | malloc(bytes) malloc_dbg(bytes, __LINE__, __FILE__, __FUNCTION__) |
This macro replace the standard malloc function with malloc_dbg. | |
#define | calloc(elemCount, elemSize) calloc_dbg(elemCount, elemSize, __LINE__, __FILE__, __FUNCTION__) |
This macro replace the standard calloc function with calloc_dbg. | |
#define | free(ptr) free_dbg(ptr) |
This macro replace the standard free function with free_dbg. | |
Functions | |
void * | malloc_dbg (size_t bytes, int line, const char *filename, const char *functionName) |
malloc_dbg function is a wrapper around the malloc function. | |
void * | calloc_dbg (size_t elementCount, size_t elementSize, int line, const char *filename, const char *functionName) |
calloc_dbg function is a wrapper around the calloc function. | |
void | free_dbg (void *ptrToFree) |
free_dbg function is used to free the memory allocated to a pointer. | |
void | printLeaks (void) |
printLeaks function is used to print all the memory leaks. | |
Header file that contains macros used to replace malloc/calloc and free.
Macros malloc, calloc and free respectively calls malloc_dbg, calloc_dbg and free_dbg. malloc_dbg and calloc_dbg allocates memory using the "real" malloc and calloc and store the pointer returned (with additional informations) in a linked list. Thanks to this linked list, it is possible to check memory leaks.
void * calloc_dbg | ( | size_t | elementCount, |
size_t | elementSize, | ||
int | line, | ||
const char * | filename, | ||
const char * | functionName | ||
) |
calloc_dbg function is a wrapper around the calloc function.
This function calls calloc and allocates the number of bytes passed in the parameters. If the allocation succeeds then it add the pointer returned by malloc in the mem_info list.
elementCount | number of element to allocate |
elementSize | Size of each element |
line | Line number in the caller file |
filename | Caller file |
functionName | Caller function |
void free_dbg | ( | void * | ptrToFree | ) |
free_dbg function is used to free the memory allocated to a pointer.
This function free the memory pointed by the pointer passed in parameter. To free this pointer, we loop through the mem_info list and check if we find the pointer. Once it's found, the pointer is freed and the element is deleted from the list.
ptrToFree | Pointer that must be freed |
void * malloc_dbg | ( | size_t | bytes, |
int | line, | ||
const char * | filename, | ||
const char * | functionName | ||
) |
malloc_dbg function is a wrapper around the malloc function.
This function calls malloc and allocates the number of bytes passed in the parameters. If the allocation succeeds then it add the pointer returned by malloc in the mem_info list.
bytes | Size of the allocation in bytes |
filename | Caller file |
functionName | Caller function |
void printLeaks | ( | void | ) |
printLeaks function is used to print all the memory leaks.
This function is called when the program exits. It loop through the mem_info list and if it's not empty, it prints the memory leaks.