简体   繁体   English

为什么在cmd行(-D)中重新定义malloc()时,gcc为什么不检查函数的隐式声明?

[英]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.

相关问题 我可以重新定义一个函数或检查它是否存在? - Can I re-define a function or check if it exists? 为什么gcc会发出警告:隐式声明函数qsort_r? - Why gcc gives warning: implicit declaration of function qsort_r? 为什么gcc报告“隐含的函数声明&#39;轮&#39;&#39;? - Why does gcc report “implicit declaration of function ‘round’”? 内置 function 'malloc' 的不兼容隐式声明 - Incompatible implicit declaration of built-in function ‘malloc’ 函数的隐式声明在 gcc 中编译但不在 g++ 中编译 - implicit declaration of a function compiles in gcc but not in g++ 在C中定义函数之前使用隐式声明时,为什么编译器无法弄清楚? - Implicit declaration when using a function before it is defined in C, why can't the compiler figure this out? GCC |警告:隐式声明函数&#39;_stricmp&#39;[-Wimplicit-function-declaration] | - GCC |warning: implicit declaration of function '_stricmp' [-Wimplicit-function-declaration]| 为什么在编译如何解决函数的隐式声明时会出现这个错误? - why this error coming when compiling how to solve implicit declaration of function? 警告:function 'malloc' 的隐式声明,即使<stdlib.h>已经包括了</stdlib.h> - Warning: implicit declaration of function ‘malloc’, even if <stdlib.h> is included 双指针不会在函数中获得malloc - Double pointer doesn't get malloc'd in function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM