Code for malloc and free

不羁的心 提交于 2019-12-17 15:58:14

问题


Where can I find the code for malloc my gcc compiler is using at the moment? I actually want to write my own malloc function which will be a little different from the original one. I know I can use hooks et all, but I want to see the real code.


回答1:


The POSIX interface of malloc is defined here.

If you want to find out how the C library in GNU/Linux (glibc) implements malloc, go and get the source code from http://ftp.gnu.org/gnu/glibc/ and look at the malloc/malloc.c file.

There is also the base documentation of the Memory Allocator by Doug Lea that describes the theory of a m(emory)alloc(ator) (read this carrefully, and then decide if you really need to implement your own malloc).




回答2:


Look in the appropriate release of glibc at the old release site1 or here. For example, if you are using glib 2.9, it is in this archive. Look for the file malloc/malloc.c.

You will see that it is not a trivial piece of library code.


1 It looks like they changed the directory structure after glibc-2.9.




回答3:


malloc() should be in glibc.

Further Reading
Multithreading
C++ allocation tricks [Video]




回答4:


I am able to browse the source online here.



来源:https://stackoverflow.com/questions/6485586/code-for-malloc-and-free

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!