161

I'm getting this error:

warning: incompatible implicit declaration of built-in function ‘malloc’

I am trying to do this:

fileinfo_list* tempList = malloc(sizeof(fileinfo_list));

Just for the reference the struct used at hand is:

typedef struct {
    fileinfo** filedata;
    size_t nFiles;
    size_t size;
    size_t fileblock;
} fileinfo_list;

I don't see anything wrong with what I've done. I'm just creating a tempList with the size of 1 x fileinfo_list.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
SGE
  • 2,087
  • 3
  • 16
  • 16
  • 3
    possible duplicate of [Why do I get a warning everytime I use malloc?](http://stackoverflow.com/questions/1230386/why-do-i-get-a-warning-everytime-i-use-malloc) – Oded Nov 01 '11 at 14:14

5 Answers5

357

You likely forgot to #include <stdlib.h>

jasonleonhard
  • 6,357
  • 53
  • 49
cnicutar
  • 164,886
  • 23
  • 329
  • 361
46

You need to #include <stdlib.h>. Otherwise it's defined as int malloc() which is incompatible with the built-in type void *malloc(size_t).

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Omri Barel
  • 8,186
  • 3
  • 27
  • 22
  • when it is defined as `int malloc()`, does it replicate `void *malloc(size_t)`? – user1343318 Mar 12 '14 at 22:19
  • @user1343318 Not necessarily, which is precisely the reason for the warning. Ex: a x64 platform with 64-bit data pointers and 32-bit `int` values will puke goat feces, while a x86 32bit-data-pointer/32bit-`int` can seemingly work correctly. *Neither* are correct, as in neither case is the compiler aware of what `malloc` actually returns, and assumes `int` in response. – WhozCraig May 13 '15 at 01:54
15

You're missing #include <stdlib.h>.

Antti
  • 10,806
  • 2
  • 21
  • 29
4

The stdlib.h file contains the header information or prototype of the malloc, calloc, realloc and free functions.

So to avoid this warning in ANSI C, you should include the stdlib header file.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
-4

The only solution for such warnings is to include stdlib.h in the program.