繁体   English   中英

警告:function 'malloc' 的隐式声明,即使<stdlib.h>已经包括了</stdlib.h>

[英]Warning: implicit declaration of function ‘malloc’, even if <stdlib.h> is included

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

#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};

这是我收到的警告:

warning: implicit declaration of function 'malloc'

warning: incompatible implicit declaration of built-in function 'malloc'

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

任何人都知道什么在这里不起作用?

您可能正在使用不符合标准的编译器和/或 C 库在过时的系统上进行编译。 除了<stdlib.h>之外,尝试包含<malloc.h>并且总是首先包含标准头文件。

暂无
暂无

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

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