c - 即使包含 <stdlib.h> 也会出现警告 : implicit declaration of function ‘malloc’ ,

标签 c list malloc

这是一段代码的摘录,我在其中用数组的元素填充了一个列表。

#include <stdlib.h>
#include <stdio.h>
#include "../../lib/kernel/list.h"
#include "./listpop.h"

struct item {
    struct list_elem elem;
    int value;
    int priority;
};

void populate(struct list * l, int * a, int n);

void populate(struct list * l, int * a, int n)
{
  int i = 0;
  while(i != n) {
    struct item * newitem = malloc(sizeof(struct item));
    newitem->value = a[i];
    list_push_back(l,newitem);
    i++;
  }
}

void test_assignment_1()
{   struct list our_list;
    list_init(&our_list);
    populate(&our_list, ITEMARRAY, ITEMCOUNT);
}

list.h 中的代码:

/* List element. */
struct list_elem 
{
  struct list_elem *prev;     /* Previous list element. */
  struct list_elem *next;     /* Next list element. */
};

/* List. */
struct list 
{
  struct list_elem head;      /* List head. */
  struct list_elem tail;      /* List tail. */
};

void list_init (struct list *);

list.c 中的代码:

/* Initializes LIST as an empty list. */
void
list_init (struct list *list)
{
  ASSERT (list != NULL);
  list->head.prev = NULL;
  list->head.next = &list->tail;
  list->tail.prev = &list->head;
  list->tail.next = NULL;
}

最后,listpop.h 中的代码:

#define ITEMCOUNT 10
int ITEMARRAY[ITEMCOUNT] = {3,1,4,2,7,6,9,5,8,3};

这是我收到的警告:

警告:函数“malloc”的隐式声明

警告:内置函数“malloc”的隐式声明不兼容

到目前为止,我所读到的有关这些警告的所有信息都是添加 stdlib.h,但正如您从我的代码中看到的那样,我已经完成了,并且代码仍然给我这些警告。我已经多次重新启动代码,所以错误位于代码中的某处。

有人知道什么在这里不起作用吗?

最佳答案

您可能正在使用不符合标准的编译器和/或 C 库在过时的系统上进行编译。尝试包括 <malloc.h>除了<stdlib.h>并始终首先包含标准 header 。

关于c - 即使包含 <stdlib.h> 也会出现警告 : implicit declaration of function ‘malloc’ ,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61826531/

相关文章:

c - 如何 malloc 预处理器指令

c - 为什么我要向堆栈指针添加一个地址?

用于 C 的代码重构工具,可用于 GNU/Linux?最好是源码

c++ - typedef 结构语法

列表中的 C++ 迭代器

使用预定义值列表作为 SQL 表的 SQL 语句

malloc - 为二进制程序启用 mtrace (MALLOC_TRACE)

c - 对于给定的均值和方差,如何在 c 中生成高斯伪随机数?

python - 缺少 "from typing import List"- 脚本错误,但函数中没有

我们可以使用单个 malloc() block 来表示两种不同的类型吗?