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

Share:
11,308

Related videos on Youtube

CottonCandy
Author by

CottonCandy

Updated on June 04, 2022

Comments

  • CottonCandy
    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
      tkausl almost 6 years
      Are you compiling with a C++ compiler?
    • Gaurav Pathak
      Gaurav Pathak almost 6 years
    • CottonCandy
      CottonCandy almost 6 years
      @tkausl yes. I am new to C++, and I never used to get this error in C99
    • Gaurav
      Gaurav almost 6 years
      @Isaacmalloc is C specific. use new in CPP.
    • tkausl
      tkausl almost 6 years
      Well 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
      melpomene almost 6 years
    • HostileFork says dont trust SE
      HostileFork says dont trust SE almost 6 years
      @Isaac See Wikipedia article Compatibility of C and C++
    • M.M
      M.M almost 6 years
      You should not use malloc in C++
    • molbdnilo
      molbdnilo almost 6 years
      You 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
    melpomene almost 6 years
    I'm pretty sure you're supposed to use static_cast<> in C++ (or an existing container type, really).