C语言 <stdlib.h> 和 <malloc.h> 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12973311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
difference between <stdlib.h> and <malloc.h>
提问by none
When I use mallocin a C program, I get a warning:
当我malloc在 C 程序中使用时,我收到一条警告:
warning: incompatible implicit declaration of built-in function 'malloc' [enabled by default]
I can then include <malloc.h>or <stdlib.h>to get rid of the warningalthough it works without it as well.
然后我可以包含<malloc.h>或<stdlib.h>摆脱warning它,尽管它也可以在没有它的情况下工作。
So I was wondering, what's the difference between these headers and which one does gcclinks when I don't include anything?
所以我想知道,gcc当我不包含任何内容时,这些标题和哪个链接有什么区别?
(I'm using ubuntu 12.04 64-bitwith gcc 4.6.3)
(我使用ubuntu 12.04 64-bit同gcc 4.6.3)
回答by Basile Starynkevitch
The <malloc.h>header is deprecated (and quite Linux specific, on which it defines non-standardfunctions 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>
该<malloc.h>标头已被弃用(和相当的Linux具体而言,在其上它定义非标准类似功能mallinfo(3) )。<stdlib.h>如果您只需要malloc(3)和相关的标准函数(例如free, calloc, realloc....),请改用。请注意,<stdlib.h>由C89(及更高版本)标准定义,但不是<malloc.h>
Look into /usr/include/malloc.hyou'll find there some non-standard functions (e.g. malloc_stats(3), etc...) - in addition of malloc....
查看/usr/include/malloc.h您会发现有一些非标准函数(例如malloc_stats(3)等...) - 除了malloc....
And gccdon't link header files, but libraries. Read Levine's book about linkers & loadersfor more.
并且gcc不要链接头文件,而是链接库。阅读 Levine 关于链接器和加载器的书了解更多信息。
If you don't include any headers (and dont explicitly declare mallocyourself, which would be a bad idea), mallocis implicitly declared as returning some intvalue (which is wrong). I do invite you to pass at least the -Wallflag to gccwhen using it.
如果您不包含任何标头(并且不明确声明malloc自己,这将是一个坏主意),malloc则隐式声明为返回某个int值(这是错误的)。我确实邀请您在使用它时至少将-Wall标志传递给gcc。
You might also pass -vto gccto understand the actual programs involved: cc1is the compiler proper (producing assembly code), asthe assembler, ldthe linker, and collect2an internal utility which invokes the linker.
您可能还传递-v到gcc理解所涉及的实际方案:cc1在编译器正确(产生汇编代码),as汇编器,ld链接器和collect2内部工具,它调用链接。
回答by nos
stdlib.his a standard C header that declares among other things the malloc(), calloc(), free()functions. This is the header you should include.
stdlib.h是一个标准的 C 头文件,其中声明了malloc(), calloc(),free()函数。这是您应该包含的标题。
malloc.his a non-standard header, found on many systems where it often defines additional functions specific to the malloc implementation used by that platform.
malloc.h是一个非标准头文件,可在许多系统中找到,它通常定义特定于该平台使用的 malloc 实现的附加功能。
If you do not include any of these, there's no default, however if you call malloc()without a prior declaration of the malloc function, C will assume the function prototype is int malloc();, which is often wrong. In addition to the headers, C compilers typically link to a standard library, e.g. glibc on Linux, where the implementation of malloc resides.
如果您不包含其中任何一个,则没有默认值,但是如果您在malloc()没有事先声明 malloc 函数的情况下调用,C 将假定函数原型为int malloc();,这通常是错误的。除了头文件之外,C 编译器通常会链接到标准库,例如 Linux 上的 glibc,malloc 的实现驻留在其中。
Note that there's a difference between header files and libraries. Header files declare things, like structs and function prototypes. Libraries contain the implementation, the compiled code. You link to library, and you #includeheader files.
请注意,头文件和库之间存在差异。头文件声明了一些东西,比如结构和函数原型。库包含实现,编译后的代码。您链接到库和#include头文件。
回答by Steve Jessop
The headers declare different sets of functions, but both forward-declare malloc.
头文件声明了不同的函数集,但都是 forward-declare malloc。
If you don't include either of them then you don't have a prototype for malloc, hence the warning. But you link against the same function regardless, because there is only one mallocfunction. It's just forward-declared in two places. The forward-declarations aren't there to help link against the mallocfunction, they're there so that the compiler can emit the correct code around the call, to specify the arguments and read the return value.
如果您不包含其中任何一个,那么您就没有 的原型malloc,因此会发出警告。但是无论如何你都链接到同一个函数,因为只有一个malloc函数。它只是在两个地方预先声明。前向声明不是为了帮助链接malloc函数,它们在那里是为了让编译器可以围绕调用发出正确的代码,以指定参数并读取返回值。
Note that <malloc.h>is not a standard include. I don't think stdlib.hever includes malloc.hon GCC, but you can imagine that it might since that's one way to provide the necessary declaration.
请注意,这<malloc.h>不是标准包含。我认为GCC 中stdlib.h从来没有包含malloc.h过,但是您可以想象它可能会包含在内,因为这是提供必要声明的一种方式。
回答by David C.
Others have already discussed the differences between <malloc.h> and <stdlib.h>
其他人已经讨论过 <malloc.h> 和 <stdlib.h> 之间的区别
As for the warning when neither is included, that is the definition of how C functions work. A function without a prototype (which is what you have when you don't declare your own and don't include a header with one) is treated as a function with an intreturn type and an unspecifiedargument list.
至于两者都不包含时的警告,那就是 C 函数如何工作的定义。没有原型的函数(当您不声明自己的并且不包含带有一个的标头时所拥有的原型)被视为具有int返回类型和未指定参数列表的函数。
The compiler will perform default promotions (e.g. float to double and others) and the function is called. If the number of arguments used by the function is different from the number passed, or if the argument types after default promotions are not compatible with the function's implementation, it is undefined behavior.
编译器将执行默认提升(例如 float 到 double 等)并调用该函数。如果函数使用的参数数量与传递的数量不同,或者默认提升后的参数类型与函数的实现不兼容,则为未定义行为。
See ISO 9899:1999 (C99) §6.5.2.2, ¶ 6:
参见 ISO 9899:1999 (C99) §6.5.2.2, ¶ 6:
If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type
floatare promoted todouble. These are called the default argument promotions. If the number of arguments does not equal the number of parameters, the behavior is undefined. If the function is defined with a type that includes a prototype, and either the prototype ends with an ellipsis (, ...) or the types of the arguments after promotion are not compatible with the types of the parameters, the behavior is undefined. If the function is defined with a type that does not include a prototype, and the types of the arguments after promotion are not compatible with those of the parameters after promotion, the behavior is undefined, except for the following cases:
- one promoted type is a signed integer type, the other promoted type is the corresponding unsigned integer type, and the value is representable in both types;
- both types are pointers to qualified or unqualified versions of a character type or
void.
如果表示被调用函数的表达式的类型不包含原型,则对每个参数执行整数提升,并将具有类型的参数
float提升为double。这些被称为默认参数促销. 如果参数数量不等于参数数量,则行为未定义。如果函数定义的类型包含原型,并且原型以省略号 (, ...) 结尾,或者提升后的参数类型与参数类型不兼容,则行为未定义。如果函数定义的类型不包含原型,并且提升后的参数类型与提升后的参数类型不兼容,则行为未定义,但以下情况除外:
- 一个提升类型是有符号整数类型,另一个提升类型是对应的无符号整数类型,值在两种类型中都可以表示;
- 这两种类型都是指向字符类型或的限定或非限定版本的指针
void。
In the case of calling malloc()without a prototype, this has the potential to be very bad. malloc()accepts a size_targument and returns a void *pointer. If the result of default promotion of your integer argument produces an integer different in size from size_t, you will have undefined behavior. And if intis a different size from void *(e.g. on 64-bit systems, where intis often 32-bits and void *will be 64-bits,) the returned pointer will be messed up.
在malloc()没有原型的情况下调用,这可能会非常糟糕。 malloc()接受一个size_t参数并返回一个void *指针。如果您的整数参数的默认提升结果产生一个大小与 不同的整数size_t,您将有未定义的行为。如果int大小与void *(例如在 64 位系统上,int通常是 32 位并且void *将是 64 位)不同,则返回的指针将被弄乱。
回答by aib
<malloc.h>is not a standard header and is thus not portable. The standard puts malloc()et al. in <stdlib.h>.
<malloc.h>不是标准头,因此不可移植。标准看跌期权malloc()等。在<stdlib.h>。
回答by bmargulies
To learn the difference, you should read their contents for yourself.
要了解差异,您应该自己阅读它们的内容。
By default, gcc reads neither.
默认情况下,gcc 不会读取。
When you read them, you will see that they declare mallocdifferently.
当您阅读它们时,您会发现它们的声明malloc有所不同。

