The goal of this project was to implement a pretty basic memory allocator to better understand the memory allocation by writting our own malloc()
, calloc()
, realloc()
and free()
.
By implementing these functions on our own we will be able to understand the intuition behind the working of these functions.
A step-by-step beginner friendly guide can be found here for anyone who wants to implement or try it for yourself.
Source code for this project can be found here.
We’ll be compiling our memory allocator and then run a utility like ls using our memory allocator.
First compile it as a library file.
$ gcc -o memAlloc.so -fPIC -shared memAlloc.c
Set the environment variable LD_PRELOAD
to the path of a shared object, this way our file will be loaded before any other library.
$ export LD_PRELOAD=$PWD/memAlloc.so
Now to test,
$ ls
memAlloc.c memAlloc.so
And now our memory allocator is serving ls
!.