Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to compile my version of Malloc in C?

I’m currently coding my implementation of the malloc() function, so I’m compiling with the following flags: -m64 -fPIC -pedantic -Wall -Wextra -Werror -nostdlib -ggdb3.

For the time being, I’m compiling as an executable and not as a shared library.

For my implementation I’m using the header files stddef.h stdint.h unistd.h, for the moment I need the syscall sbrk(). The problem is that on compilation I get the following error: undefined reference to sbrk.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I assume that sbrk() and more generally unistd.h are disabled if I compile with -nostdlib but how can I compile without the glibc but with the system calls?

Thanks in advance

P.S I really need to use sbrk() I can’t use mmap(), don’t encourage me to do it.

>Solution :

In this case, don’t use -nostdlib. glibc has special support for replacing malloc()

This should enable sbrk() to work. Don’t start multiple threads or you will have a headache.

If you can’t find sbrk(), call mmap() instead. sbrk() is obsolete and doesn’t interact very well with modern system facilities.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading