malloc 和 free 的代码
在哪里可以找到 gcc 编译器目前使用的 malloc 代码?我实际上想编写自己的 malloc 函数,该函数与原始函数略有不同。我知道我可以使用钩子等,但我想看看真正的代码。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
malloc 的 POSIX 接口在这里定义。
如果您想了解 GNU/Linux (glibc) 中的 C 库如何实现
malloc
,请从 http://ftp.gnu.org/gnu/glibc/ 或浏览 git 存储库 并查看malloc/malloc.c
文件。还有 Doug Lea 的内存分配器 的基本文档,描述了m(emory)alloc(ator) 的理论(仔细阅读本文,然后决定是否真的需要实现自己的 malloc)。
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/ or browse the git repository and look at themalloc/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).
在旧版本站点1查找相应版本的 glibc或此处。例如,如果您使用的是 glib 2.9,则它位于此存档。查找文件
malloc/malloc.c
。您会发现它不是一段简单的库代码。
1 看起来他们在glibc-2.9之后改变了目录结构。
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.
malloc()
应该在 glibc 中。malloc()
should be in glibc.