stdlib.h中&GT;&之间的LT差分;和&lt;&malloc.h所GT; [英] 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]
然后我就可以包括&LT; malloc.h所&GT;
或&LT;文件stdlib.h&GT;
来摆脱在警告
虽然工作没有它。
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
)
推荐答案
的&LT; malloc.h所&GT;
头去precated(并且相当Linux特有的,其所定义的非标的功能一样 mallinfo (3))。使用&LT;&stdlib.h中GT;
如果您只是需要的的malloc(3)和相关的标准功能(如免费
,释放calloc
,的realloc
....)。请注意,&LT;&stdlib.h中GT;
被定义为 C89 (或更高版本)的标准,而不是&LT;&malloc.h所GT;
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的了解链接器和放大器的书;装载机了解。
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
是编译器正确的(生产组装code),为
汇编, 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中&GT;&之间的LT差分;和&lt;&malloc.h所GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!