Invalid Conversion from 'Void*' to 'Char*' When Using Malloc

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

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

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

Malloc invalid conversion from 'void*'

The problem is that you're using a C++ compiler to compile C code.

In C, a void * can freely be converted to or from any other non-function pointer without a cast. In C++, a cast is required, which is why you're getting errors about converting to/from void *.

Use the C compiler to compile C code:

gcc scheduler.c -o out

C++ error: invalid conversion from 'void*' to 'char*'

The problem is that you're trying to compile C with a C++ compiler. As the error message says, this line:

Teclas = calloc(1024,sizeof(char));

tries to convert the untyped void* pointer returned by calloc into a typed char* pointer to assign to the variable of that type. Such a conversion is valid in C, but not C++.

The solution is to use a C compiler. It looks like you're using GCC, so just rename the source file to something.c, and build with gcc rather than g++.

If you really must use a compiler for the wrong language, and don't feel like rewriting this in idiomatic C++, then you'll need a cast to force it through the compiler:

Teclas = static_cast<char*>(calloc(1024,sizeof(char)));

or, if you want the code to remain valid C:

Teclas = (char*)calloc(1024,sizeof(char));

But don't do that: use the right compiler for the language. Unless this is the first stage in converting the program to C++; in which case, the next thing to do is get rid of these allocations and use std::string instead.

conversion from void* to char*

You could just caast the result of the realloc to (char*) like you do with the malloc.

retval = (char*)realloc(ptr, 5);

Ironically though what you have written is perfectly legal "C" code ... just not C++ where you require the cast. So you might also try setting your compiler to compile the code as "C" as you aren't doing any C++ and that would equally solve the issue. That said the above modification is also perfectly legal C ... so would compile under C++ and C.

Edit: As softy rightly points out you realloc ptr and store the pointer location into retval which means that ptr is, potentially, pointing at invalid memory and you should NOT free it. At best you free the same memory twice, at worst you call free on memory that has already been freed (by realloc). Either way it is bad (tm). As it is you are invoking some serious "undefined behaviour".

C++ malloc invalid conversion from `void*' to struct

In C++ there is no implicit conversion from type void * to pointer of other type. You have to specify explicit casting. For example

node = ( struct bstree * )malloc(sizeof (*node));

or

node = static_cast<struct bstree *>( malloc(sizeof (*node)) );

Also in C++ you should use operator new instead of the C function malloc.

Invalid converstion from void* to char**

Added: The fasted solution to Arron's actual problem is provided by sgm in a comment. The text below is all accurate, and hopefully helpful, but a second rate solution to the problem at hand.


Your compiler is being very stiffnecked about pointer casts (are you using a c++ compiler?), adding an explicit cast like

sequence=(char**)malloc(sizeof(char *)*seqNum);

should make the error go away. Alternately you might be able to convince the compiler to go easy on you with some kind of option like

$(CC) --lighten-up-baby code.c

which might be preferable if this is in some third party code that you don't really want to hack. Read your compiler documentation to find the option you want. Since all the gccs I have on hand (versions 4.0 and 4.2) are happy with that code, I'm not in a good place to offer advice on switches to turn this behavior off.

Error invalid conversion from ‘void*’ to ‘char*’

The issue is where you're calling realloc:

// new array allocate with large size and copy data to new array
nwData = realloc(strp->data, nbytes);

nwData is a char * type, but realloc returns a void *. See https://en.cppreference.com/w/c/memory/realloc for more information. You should instead cast to a char * as you do when setting name.data:

nwData = (char *)realloc(strp->data, nbytes);

I'm assuming you're compiling with g++? If you're writing a C program you should compile with gcc, which will compile according to C language semantics rather than C++.

As a side note, I see that you're manually setting the remainder of the array to \0 in a loop:

// filled with '\0' in remaining space of new array
for (int lp = lnth; lp < nbytes; lp++)
{
strp->data[lp] = '\0';
}

It is typically much faster (and better code style) to use the builtin memcpy function rather than using a loop:

memset(strp->data + lnth, nbytes - lnth, '\0');

error: invalid conversion from 'void*' to 'node_t*'

Replace malloc with new

Perhaps read https://www.geeksforgeeks.org/malloc-vs-new/

Note the line

    node_t *result = new node_t;

in the below code

#include <iostream>
using namespace std;

typedef struct node {
int value;
struct node *next;
} node_t;

void printlist(node_t *head){
node_t *temporary = head;
while(temporary != NULL){
cout << temporary -> value << endl;
temporary = temporary -> next;
}
cout << endl;
}
node_t *create_new_node(int value){
node_t *result = new node_t;
result->value = value;
result->next = NULL;
return result;
}

int main()
{
setlocale(LC_ALL, "portuguese");

node_t *head;
node_t *tmp;
tmp = create_new_node(32);
head = tmp;
tmp = create_new_node(8);
tmp->next = head;
head = tmp;
tmp = create_new_node(34);
tmp->next = head;
head = tmp;
printlist(head);

return 0;
}

A running version at godbolt

https://godbolt.org/z/7PvohY4b5

avoiding malloc in c++ : invalid conversion from ‘void*’ to ‘uv_loop_t*

In C a void*, the return type of malloc, converts implicitly to any other data pointer type.

In C++ it does not.

C also has implicit int, which means that best practice for use of malloc differs between the languages. In C the result should not be casted, because if one is missing an #include this could implicitly declare malloc with int result. In C++, however, the result must be casted if it's used as anything other than a void*.

Your code

#include <iostream>
#include <uv.h>

int main() {
uv_loop_t *loop = malloc(sizeof(uv_loop_t));
uv_loop_init(loop);

std::cout << "Running loop" << std::endl;
uv_run(loop, UV_RUN_DEFAULT);

uv_loop_close(loop);
free(loop);
return 0;
}

… is better expressed as

#include <iostream>
#include <uv.h>

int main() {
uv_loop_t loop;
uv_loop_init(&loop);

std::cout << "Running loop" << std::endl;
uv_run(&loop, UV_RUN_DEFAULT);

uv_loop_close(&loop);
}


Related Topics



Leave a reply



Submit