为什么每次使用malloc时都会收到警告? [英] Why do I get a warning every time I use malloc?

查看:182
本文介绍了为什么每次使用malloc时都会收到警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我从 gcc


new.c:7:warning:隐式声明函数'malloc'

new.c:7:warning :不兼容的隐式声明内置函数'malloc'


解决方案

您需要添加:

  #include< stdlib.h> 

该文件包含内置函数 malloc 。如果你不这样做,编译器认为你想定义自己的函数名为 malloc ,它会提醒你,因为:


  1. 您没有明确地声明它

  2. 已经有一个内置函数,与隐式声明不同的签名(当函数被隐式声明时,其返回值和参数类型假定为 int ,这与内置函数不兼容-in malloc ,它需要一个 size_t 并返回一个 void * )。


If I use malloc in my code:

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

I get this warning from 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. If you don't do that, the compiler thinks you want to define your own function named malloc and it warns you because:

  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*).

这篇关于为什么每次使用malloc时都会收到警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆