If I use malloc
in my code:
int *x = malloc(sizeof(int));
I get this warning from gcc
:
new.c:7: w
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
.
(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
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
.
The reasons why, are best explained in the answers to this question:
difference between