[英]Why gcc doesn't check implicit declaration of function when re-define malloc() in cmd line (-D)?
Here is my original function (test.c): 这是我的原始功能(test.c):
#include <stdlib.h>
int main()
{
void *p = malloc(1);
free(p);
return 0;
}
If I re-define malloc in gcc cmd line with -D, gcc compiles it fine. 如果我用-D在gcc cmd行中重新定义malloc,则gcc会很好地进行编译。 (why it doesn't complain at my_malloc()?)
(为什么它不抱怨my_malloc()?)
gcc -c -Wall -D malloc=my_malloc test.c
If I change the malloc name in my code to malloc2: 如果我将代码中的malloc名称更改为malloc2:
#include <stdlib.h>
int main()
{
void *p = malloc2(1);
free(p);
return 0;
}
And then re-define it, gcc now finally complains: 然后重新定义它,gcc现在终于抱怨:
$ gcc -c -Wall -D malloc2=my_malloc test.c
test.c: In function ‘main’:
test.c:5:5: warning: implicit declaration of function ‘my_malloc’ [-Wimplicit-function-declaration]
void *p = malloc2(1);
Could someone help me explain why this is happening? 有人可以帮我解释为什么会这样吗?
This has to do with how the preprocessor is working. 这与预处理器的工作方式有关。
Assuming you #include <stdlib.h>
at the top of your file, the first thing that happens is that this include file is basically imported into your source file. 假设您在文件顶部
#include <stdlib.h>
,首先发生的是该包含文件基本上已导入到您的源文件中。 Next, the define passed in to the command line gets processed. 接下来,处理传递到命令行的定义。
In the first case, this changes not only the call to malloc
in your code but also the declaration of malloc
imported from stdlib.h. 在第一种情况下,这不仅会更改代码中对
malloc
的调用,还会更改从stdlib.h导入的malloc
的声明 。 So no implicit declaration. 因此没有隐式声明。 In the second case, the
malloc2
call gets changed, but there's no corresponding declaration that gets swapped out as well. 在第二种情况下,
malloc2
调用被更改,但是也没有相应的声明也被换出。 So you get the warning. 因此,您会收到警告。
This is under gcc 4.1.2. 这是在gcc 4.1.2下的。
The warning you get in the third case is because you have used a function malloc2()
(or my_malloc()
in case you use the -Dmalloc2=my_malloc
define) without a prototype. 在第三种情况下收到的警告是因为您使用了没有原型的函数
malloc2()
(或在使用-Dmalloc2=my_malloc
定义的情况下使用了-Dmalloc2=my_malloc
my_malloc()
)。 In this case, the <stdlib.h>
defined prototype doesn't match the define you make in the command line, and you don't get a prototype for it anymore. 在这种情况下,
<stdlib.h>
定义的原型与您在命令行中所做的定义不匹配,并且您也不再为此获得原型。
As standard behaviour, if you don't include a prototype for a function before it's first use, it defaults to return int
and to use an undefined number of parameters. 作为标准行为,如果您在首次使用函数时未提供其原型,则默认情况下将返回
int
并使用未定义数量的参数。 This means that the compiler is assuming you have a int my_malloc();
这意味着编译器假设您有一个
int my_malloc();
function defined elsewhere and as you are trying to assign an integer value to a pointer variable, there's something wrong in your code. 在其他地方定义的函数,并且当您尝试将整数值分配给指针变量时,代码中出现了问题。
If you want the compiler assume your malloc implementation is like the standard library one, just define it with void *malloc(const size_t);
如果您希望编译器假定您的malloc实现类似于标准库之一,则只需使用
void *malloc(const size_t);
定义它即可void *malloc(const size_t);
before that first use. 在第一次使用之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.