简体   繁体   English

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

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

This is an extract of a code, where I populate a list with the elements of an array.这是一段代码的摘录,我在其中用数组的元素填充了一个列表。

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

Code inside list.h: 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 *);

Code inside list.c: 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;
}

And finally, the code inside listpop.h:最后,listpop.h 中的代码:

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

Here are the warnings I get:这是我收到的警告:

warning: implicit declaration of function 'malloc'

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

So far, all I've read about those warnings is to add stdlib.h, but as you can see from my code I've already done it, and the code still give me those warnings.到目前为止,我所读到的关于这些警告的所有内容都是添加 stdlib.h,但正如您从我的代码中看到的那样,我已经完成了,并且代码仍然给我这些警告。 I've restarted the code many times, so the error lays somewhere in the code.我已经多次重新启动代码,所以错误位于代码中的某处。

Anyone knows what is not working here?任何人都知道什么在这里不起作用?

You might be compiling on a obsolete system with a non conforming compiler and/or C library.您可能正在使用不符合标准的编译器和/或 C 库在过时的系统上进行编译。 Try including <malloc.h> in addition to <stdlib.h> and always include the standard headers first.除了<stdlib.h>之外,尝试包含<malloc.h>并且总是首先包含标准头文件。

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

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