简体   繁体   English

为什么每次使用 malloc 时都会收到警告?

[英]Why do I get a warning every time I use malloc?

If I use malloc in my code:如果我在代码中使用malloc

int *x = malloc(sizeof(int));

I get this warning from gcc :我从gcc收到这个警告:

new.c:7: warning: implicit declaration of function ‘malloc’  
new.c:7: warning: incompatible implicit declaration of built-in function ‘malloc’

You need to add:您需要添加:

#include <stdlib.h>

This file includes the declaration for the built-in function malloc .该文件包括内置函数malloc的声明。 If you don't do that, the compiler thinks you want to define your own function named malloc and it warns you because:如果你不这样做,编译器会认为你想定义你自己的名为malloc的函数,它会警告你,因为:

  1. You don't explicitly declare it and您没有明确声明它并且
  2. There already is a built-in function by that name which has a different signature than the one that was implicitly declared (when a function is declared implicitly, its return and argument types are assumed to be int , which isn't compatible with the built-in malloc , which takes a size_t and returns a void* ).已经有一个同名的内置函数,它的签名与隐式声明的函数不同(当一个函数被隐式声明时,它的返回和参数类型被假定为int ,这与内置函数不兼容) -in malloc ,它需要一个size_t并返回一个void* )。

你还没有完成#include <stdlib.h>

You need to include the header file that declares the function, for example:您需要包含声明函数的头文件,例如:

#include <stdlib.h>

If you don't include this header file, the function is not known to the compiler.如果不包含此头文件,则编译器不知道该函数。 So it sees it as undeclared.所以它认为它是未申报的。

Make a habit of looking your functions up in help.养成在帮助中查找函数的习惯。

Most help for C is modelled on the unix manual pages.大多数 C 的帮助都是以 unix 手册页为模型的。

Using :使用 :

man malloc

gives pretty useful results.给出了非常有用的结果。

Googling man malloc will show you what I mean.谷歌搜索man malloc会告诉你我的意思。

In unix you also get apropos for things that are related.在 unix 中,您还可以获得相关事物的适当性。

Beside the other very good answers, I would like to do a little nitpick and cover something what is not discussed yet in the other answers.除了其他非常好的答案之外,我还想做一点吹毛求疵,并涵盖其他答案中尚未讨论的内容。


When you are at Linux, To use malloc() in your code,当你在 Linux 时,要在你的代码中使用malloc()

You don´t actually have to #include <stdlib.h> .您实际上不必#include <stdlib.h>

(Although the use of stdlib.h is very common and probably every non-toy-program should include it either way because it provides a wide range of useful C standard library functions and macros) (尽管stdlib.h的使用非常普遍,可能每个非玩具程序都应该以任何一种方式包含它,因为它提供了大量有用的 C 标准库函数和宏)

You could also #include <malloc.h> instead.你也可以#include <malloc.h>代替。

But please note that the use of malloc.h is deprecated and it makes your code non-portable.但请注意,不推荐使用malloc.h ,它使您的代码不可移植。 If you want to use malloc() you should always and ever (except for explicit reasons to do otherwise) #include <stdlib.h> .如果你想使用malloc()你应该永远(除非有明确的理由) #include <stdlib.h>

The reasons why , are best explained in the answers to this question:为什么,最好在回答这个问题解释的原因:

difference between <stdlib.h> and <malloc.h> <stdlib.h> 和 <malloc.h> 的区别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM