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

Viewed 1070

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 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:

/* 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:

#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. I've restarted the code many times, so the error lays somewhere in the code.

Anyone knows what is not working here?

1 Answers

You might be compiling on a obsolete system with a non conforming compiler and/or C library. Try including <malloc.h> in addition to <stdlib.h> and always include the standard headers first.

Related