How to convert void* to foo* to comply with C++? -
i trying compile code written in c (ndpireader.c program comes ndpi library, hosted here). i'm using qt creator , gcc compiler.
after doing research here , here, notice compiling c code c++ compiler not best idea. didn't answer of how conversion , make code c++ compatible.
when try run code in qt creator error bellow:
error: invalid conversion 'void*' 'ndpi_flow_struct*' [-fpermissive] if((newflow->ndpi_flow = malloc_wrapper(size_flow_struct)) == null) { ^
if more info needed solve problem please leave comment. i'm new c++ detailed answers links appreciated.
edit: here malloc_wrapper() function's code
static void *malloc_wrapper(unsigned long size) { current_ndpi_memory += size; if(current_ndpi_memory > max_ndpi_memory) max_ndpi_memory = current_ndpi_memory; return malloc(size); }
you're seeing error because in c++, types should have exact match.
as can see, malloc_wrapper() function returns void * , newflow->ndpi_flow of type ndpi_flow_struct*. while compiling using c++ compiler, you've add cast, like
if((newflow->ndpi_flow=(ndpi_flow_struct*)malloc_wrapper(size_flow_struct)) == null) { . . . to force compiler in believing return value of malloc_wrapper() of type (ndpi_flow_struct*).
or better, static cast<> (keeping in mind c++ aspect), like
if(( newflow->ndpi_flow = static_cast<ndpi_flow_struct*>malloc_wrapper(size_flow_struct)) == null) { . . . related reading: a detailed answer on c++ casting.
Comments
Post a Comment