<stdlib.h> 之间的区别和 <malloc.h> [英] difference between <stdlib.h> and <malloc.h>
问题描述
当我在 C 程序中使用 malloc
时,收到警告:
When I use malloc
in a C program, I get a warning:
warning: incompatible implicit declaration of built-in function 'malloc' [enabled by default]
然后我可以包含
或
以摆脱 warning
尽管它有效也没有它.
I can then include <malloc.h>
or <stdlib.h>
to get rid of the warning
although it works without it as well.
所以我想知道,当我不包含任何内容时,这些标题与 gcc
链接的标题之间有什么区别?
So I was wondering, what's the difference between these headers and which one does gcc
links when I don't include anything?
(我使用 ubuntu 12.04 64 位
和 gcc 4.6.3
)
推荐答案
<malloc.h>
标头已弃用(并且非常特定于 Linux,它定义了非标准 函数类似于 mallinfo(3)).如果您只需要 malloc(3) 和相关的标准函数(例如 free
、calloc
、realloc
....).请注意,
由 C89 定义(和后来)标准,但不是
The <malloc.h>
header is deprecated (and quite Linux specific, on which it defines non-standard functions like mallinfo(3)). Use <stdlib.h>
instead if you simply need malloc(3) and related standard functions (e.g. free
, calloc
, realloc
....). Notice that <stdlib.h>
is defined by C89 (and later) standards, but not <malloc.h>
查看 /usr/include/malloc.h
你会发现那里有一些非标准函数(例如 malloc_stats(3) 等...) - 除了 malloc
....
Look into /usr/include/malloc.h
you'll find there some non-standard functions (e.g. malloc_stats(3), etc...) - in addition of malloc
....
和 gcc
不链接头文件,而是链接库.阅读 Levine 的关于 linkers & 的书加载器了解更多.
And gcc
don't link header files, but libraries. Read Levine's book about linkers & loaders for more.
如果您不包含任何标头(并且没有自己显式声明 malloc
,这将是一个坏主意),malloc
被隐式声明为返回一些 int
值(这是错误的).我确实邀请您在使用它时至少将 -Wall
标志传递给 gcc
.
If you don't include any headers (and dont explicitly declare malloc
yourself, which would be a bad idea), malloc
is implicitly declared as returning some int
value (which is wrong). I do invite you to pass at least the -Wall
flag to gcc
when using it.
您也可以将 -v
传递给 gcc
以了解所涉及的实际程序:cc1
是正确的编译器(生成汇编代码),as
汇编器,ld
链接器,以及 collect2 一个调用链接器的内部实用程序.
You might also pass -v
to gcc
to understand the actual programs involved: cc1
is the compiler proper (producing assembly code), as
the assembler, ld
the linker, and collect2 an internal utility which invokes the linker.
这篇关于<stdlib.h> 之间的区别和 <malloc.h>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!