1

I am trying to compile a code written in C (ndpiReader.c program that comes with nDPI library, hosted here). I'm using Qt Creator and GCC compiler.

After doing some research here and here, I notice that compiling C code with C++ compiler is not the best idea. But I didn't get the answer of how to do this conversion and make this code C++ compatible.

When I try to run the code in Qt Creator I get the error bellow:

error: invalid conversion from 'void*' to 'ndpi_flow_struct*' [-fpermissive] if((newflow->ndpi_flow = malloc_wrapper(size_flow_struct)) == NULL) { ^

If more info is needed to solve the problem please leave a comment. I'm new to C++ so detailed answers with links are so much appreciated.

Edit: here is 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);
}
Community
  • 1
  • 1
ehsan0x
  • 660
  • 5
  • 10
  • 24

2 Answers2

3

You're seeing this error because in c++, types should have an exact match.

As we can see, the malloc_wrapper() function returns a void * and your newflow->ndpi_flow is of type ndpi_flow_struct*. So while compiling using c++ compiler, you've to add the cast, like

if((newflow->ndpi_flow=(ndpi_flow_struct*)malloc_wrapper(size_flow_struct)) == NULL) { . . . 

to force the compiler in believing that the return value of malloc_wrapper() is of type (ndpi_flow_struct*).

or even better, the static cast<> (keeping in mind the 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.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 5
    I think the (equivalent) static_cast<> is more appropriate and more c++ – Elemental Apr 01 '15 at 09:14
  • @Elemental Well, I'm not an expert in `c++`, thanks for the suggestion. Can you please review to make sure the edit looks ok? – Sourav Ghosh Apr 01 '15 at 09:20
  • @Nobody - keep in mind C++ won't allow certain `static_cast`s which will otherwise work just fine with the less verbose good old C style cast. – dtech Apr 01 '15 at 11:26
1

Usually, we just write

if((newflow->ndpi_flow = (ndpi_flow_struct*)malloc_wrapper(size_flow_struct)) == NULL) { 
Michael
  • 57,169
  • 9
  • 80
  • 125
Gerald
  • 690
  • 4
  • 13
  • thanks for your answer, it works fine, I accepted Sourav's answer because it was more detailed. – ehsan0x Apr 01 '15 at 09:06
  • c-style casts are discouraged. The accepted way to do this is to use the least permissive of static_cast, reinterpret_cast and const_cast that will get the conversion done. – Richard Hodges Apr 01 '15 at 09:32
  • 1
    @RichardHodges C-style casts are discouraged _in C++_. However, the question is about compiling C code (with a C++ compiler). To maintain compilability in C, it would be necessary to use a C-style cast. – davmac Apr 01 '15 at 09:38