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

后端 未结 5 736
情话喂你
情话喂你 2020-11-28 06:01

If I use malloc in my code:

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

I get this warning from gcc:

new.c:7: w         


        
5条回答
  •  粉色の甜心
    2020-11-28 06:33

    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 and

提交回复
热议问题