114

If I use malloc in my code:

int *x = malloc(sizeof(int));

I get this warning from gcc:

new.c:7: warning: implicit declaration of function ‘malloc’  
new.c:7: warning: incompatible implicit declaration of built-in function ‘malloc’
Antti Haapala
  • 117,318
  • 21
  • 243
  • 279
Kredns
  • 34,183
  • 49
  • 147
  • 200

5 Answers5

227

You need to add:

#include <stdlib.h>

This file includes the declaration for the built-in function malloc. If you don't do that, the compiler thinks you want to define your own function named malloc and it warns you because:

  1. You don't explicitly declare it and
  2. There already is a built-in function by that name which has a different signature than the one that was implicitly declared (when a function is declared implicitly, its return and argument types are assumed to be int, which isn't compatible with the built-in malloc, which takes a size_t and returns a void*).
sepp2k
  • 341,501
  • 49
  • 643
  • 658
  • 5
    Terminology nit: There's no such thing as a built-in function in C. malloc is simply a function from the Standard C Library. – Jens Oct 13 '12 at 14:55
  • 5
    @Jens I don't see a problem with referring to standard library functions as built-ins - and neither do the gcc people apparently since the error message used the word "built-in", too (which is why I did). – sepp2k Oct 13 '12 at 15:17
  • 7
    @sepp2k Well, I *do* see a problem. The usage is non-standard. *Compilers* can have built-in functions, and gcc may have malloc built-in. But as I wrote, *in C*, there are no built-in functions. – Jens Oct 13 '12 at 15:21
30

You haven't done #include <stdlib.h>.

chaos
  • 115,791
  • 31
  • 292
  • 308
16

You need to include the header file that declares the function, for example:

#include <stdlib.h>

If you don't include this header file, the function is not known to the compiler. So it sees it as undeclared.

nalply
  • 20,652
  • 12
  • 75
  • 93
andri
  • 10,945
  • 2
  • 36
  • 49
10

Make a habit of looking your functions up in help.

Most help for C is modelled on the unix manual pages.

Using :

man malloc

gives pretty useful results.

Googling man malloc will show you what I mean.

In unix you also get apropos for things that are related.

NAND
  • 645
  • 7
  • 21
Tim Williscroft
  • 3,647
  • 21
  • 36
0

Beside the other very good answers, I would like to do a little nitpick and cover something what is not discussed yet in the other answers.


When you are at Linux, To use malloc() in your code,

You don´t actually have to #include <stdlib.h>.

(Although the use of stdlib.h is very common and probably every non-toy-program should include it either way because it provides a wide range of useful C standard library functions and macros)

You could also #include <malloc.h> instead.

But please note that the use of malloc.h is deprecated and it makes your code non-portable. If you want to use malloc() you should always and ever (except for explicit reasons to do otherwise) #include <stdlib.h>.

The reasons why, are best explained in the answers to this question:

difference between <stdlib.h> and <malloc.h>