malloc error: a value of type "void *" cannot be used to initialize an entity of type "int **"
11,308
If you use C++ compiler, you may need to cast the result of malloc:
int ** th_args = (int**)malloc(24)
or simply use operator new
.
If you use a C
compiler, then... I am not sure of why this error is thrown
Related videos on Youtube
Author by
CottonCandy
Updated on June 04, 2022Comments
-
CottonCandy about 2 years
I have tried:
int** thread_args = malloc(24);
and
int** thread_args = malloc(sizeof(int*) * 3);
but I keep getting the error message.
I would really appreciate your help!
-
tkausl almost 6 yearsAre you compiling with a C++ compiler?
-
Gaurav Pathak almost 6 yearsPossible duplicate of invalid conversion from *void to *int [-fpermissive] using malloc(sizeof())
-
CottonCandy almost 6 years@tkausl yes. I am new to C++, and I never used to get this error in C99
-
Gaurav almost 6 years@Isaac
malloc
isC
specific. usenew
in CPP. -
tkausl almost 6 yearsWell you should tag C++ then, not C. This is valid in C but not in C++. You have to decide which language you want to program in.
-
melpomene almost 6 yearsPossible duplicate of Why does C++ require a cast for malloc() but C doesn't?
-
HostileFork says dont trust SE almost 6 years@Isaac See Wikipedia article Compatibility of C and C++
-
M.M almost 6 yearsYou should not use malloc in C++
-
molbdnilo almost 6 yearsYou forgot to try
int** thread_args = new int*[3];
, which is the C++ way. Next stop on the C++ train:std::vector<std::vector<int>> thread_args(3);
.
-
-
melpomene almost 6 yearsI'm pretty sure you're supposed to use
static_cast<>
in C++ (or an existing container type, really).