This is a simple custom memory allocator implemented in C. It provides functions similar to malloc and free for dynamic memory allocation and deallocation.
- Include the malloc_imp.h header file in your C code.
- Call malloc_imp(size_t size) to allocate memory dynamically. This function returns a pointer to the allocated memory block.
- Call free_imp(void *ptr) to deallocate memory previously allocated with malloc_imp. Make sure to pass the pointer returned by malloc_imp.
#include <stdio.h>
#include "malloc_imp.h"
int main() {
// Allocate memory
char *str = (char *)malloc_imp(10);
if (str == NULL) {
printf("Memory allocation failed\n");
return 1;
}
strcpy(str, "Hello");
printf("%s\n", str);
// Deallocate memory
free_imp(str);
return 0;
}
gcc -o allocator main.c malloc_imp.c
- Implement calloc and realloc later in future maybe.