Incompatible implicit declaration of built-in function ‘malloc’

CStructMalloc

C Problem Overview


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.

C Solutions


Solution 1 - C

You likely forgot to #include <stdlib.h>

Solution 2 - C

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).

Solution 3 - C

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

Solution 4 - C

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.

Solution 5 - C

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSGEView Question on Stackoverflow
Solution 1 - CcnicutarView Answer on Stackoverflow
Solution 2 - COmri BarelView Answer on Stackoverflow
Solution 3 - CAnttiView Answer on Stackoverflow
Solution 4 - Csantosh sahuView Answer on Stackoverflow
Solution 5 - Cuser3828152View Answer on Stackoverflow