hg_git hg_git - 2 months ago 13
C++ Question

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

I'm trying to learn libuv by creating small programs with help of it's documentation. Note that I'll be using it via

c++
language and not
c
. Here's what I started out with -

#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;
}


and compiled it with -
g++ -std=c++14 -luv main.cpp
which should give
a.out
as output file, but it fails with an error -


invalid conversion from ‘void*’ to ‘uv_loop_t* {aka uv_loop_s*}


which is pointing to malloc usage. I confirmed it by replacing those lines with old libuv way to do this -

uv_loop_t* loop = uv_loop_new();
...
uv_loop_delete(loop);


which shouldn't work but surprisingly works and compiles fine here.

But I think instead of dodging around this problem, I should solve this with a good way in c++. So here I'm asking for a good alternative in c++ (probably without malloc/manual memory management) so I can proceed further.

Answer

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);
}