invalid conversion from `void*' to `char*' when using malloc?

110,435

Solution 1

In C++, you need to cast the return of malloc()

char *foo = (char*)malloc(1);

Solution 2

C++ is designed to be more type safe than C, therefore you cannot (automatically) convert from void* to another pointer type. Since your file is a .cpp, your compiler is expecting C++ code and, as previously mentioned, your call to malloc will not compile since your are assigning a char* to a void*.

If you change your file to a .c then it will expect C code. In C, you do not need to specify a cast between void* and another pointer type. If you change your file to a .c it will compile successfully.

Solution 3

I assume this is the line with malloc. Just cast the result then - char *foo = (char*)...

Solution 4

So, what was your intent? Are you trying to write a C program or C++ program?

If you need a C program, then don't compile it as C++, i.e. either don't give your file ".cpp" extension or explicitly ask the compiler to treat your file as C. In C language you should not cast the result of malloc. I assume that this is what you need since you tagged your question as [C].

If you need a C++ program that uses malloc, then you have no choice but to explicitly cast the return value of malloc to the proper type.

Share:
110,435
pandoragami
Author by

pandoragami

My name is Andrew Somorjai. I usually code with C++, x86 Assembler, Python, Java, JavaScript, HTML, CSS and Erlang.

Updated on March 21, 2020

Comments

  • pandoragami
    pandoragami over 4 years

    I'm having trouble with the code below with the error on line 5:

    error: invalid conversion from void* to char*

    I'm using g++ with codeblocks and I tried to compile this file as a cpp file. Does it matter?

    #include <openssl/crypto.h>
    int main()
    {
        char *foo = malloc(1);
        if (!foo) {
            printf("malloc()");
            exit(1);
        }
        OPENSSL_cleanse(foo, 1);
        printf("cleaned one byte\n");
        OPENSSL_cleanse(foo, 0);
        printf("cleaned zero bytes\n");
    }
    
  • karlphillip
    karlphillip over 13 years
    If you are writing C code (and you are), then use the .c extension for the filename and you'll be good.
  • visual_learner
    visual_learner over 13 years
    @lost_with_coding - If you're using C++, please don't tag it [c]. Thy're not the same, as this answer clearly indicates.
  • karlphillip
    karlphillip over 13 years
    People forget that the dude saved his filename as .cpp, therefore my answer is completely relevant.
  • Karl Bielefeldt
    Karl Bielefeldt over 13 years
    Just wanted to point out that many people prefer the cast to be made explicit even in C as a matter of style, especially workgroups with programmers that switch a lot between C and C++.
  • James Greenhalgh
    James Greenhalgh about 13 years
    @Karl Bielefeldt but it is the wrong thing to do in C if it is being done as a matter of style. Not including stdlib.h would cause malloc to be implicitly defined returning an int. This will show errors if the cast is implicit between int and some pointer, but will not show errors if there is an explicit cast to the correct type. It is surely better style to use the safer C form?
  • Karl Bielefeldt
    Karl Bielefeldt about 13 years
    @James, on what compiler is this safer? On gcc 4.5.2 I get the exact same warning when omitting stdlib.h either way. Implicit casts are the more notorious ones for the compiler "helping" you in unexpected ways, which is why C++ got rid of a lot of them and added the explicit keyword to let you disable others.
  • James Greenhalgh
    James Greenhalgh about 13 years
    @Karl, put a -fno-builtin flag on GCC to disable the GCC builtin malloc, (which does a bunch of extra internal checks). If you're unlucky you'll be on a system with ints a different width from pointers, in which case your code with the cast will still give a warning, but it will be an unexpected one. (I get test.c:2: warning: cast to pointer from integer of different size). If you are lucky the code with the explicit cast will compile fine, while the code without it will error.
  • Basile Starynkevitch
    Basile Starynkevitch over 9 years
    Actually, in C++ you should not use malloc but new