Alex Rivera | Logout

Why do I get a warning every time I use malloc?

Asked 2009-08-04T23:15:41.560
115

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’
Edit
Report

2 Answers

30

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

answered 2009-08-04T23:18:34.067
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.

answered 2009-08-04T23:23:27.613

Your Answer