0

I'm trying to compile on Microsoft visual studio 2013 on C++ a program written for linux ( is a mix of C and C++ (#include .h) and I'm going to convert all in C++ to not be more confused !)

the statement:

ret->data = _malloc(ret->size + 8);

return the error:

IntelliSense: a value of type "void *" cannot be assigned to an entity of type "unsigned char *"

please help

Anton Savin
  • 40,838
  • 8
  • 54
  • 90
writetome
  • 13
  • 3

1 Answers1

2

In c++ you need to explicitly cast void * to the target poitner type, so to fix your code

ret->data = static_cast<unsigned char *>(_malloc(ret->size + 8));

or, use new/delete[]

ret->data = new unsigned char[ret->size + 8];

Some notes,

  1. You can't use new/delete[] if you will be using realloc() later.

  2. You probably need to change your compiler to a c compiler, because that is clearly c code, not c++.

Perhaps the problem is the file extension, if you give the file a .cpp or .cxx extension it will be compiled as c++ code, so change it to .c and it should work.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • @BaummitAugen sorry I really don't know the difference, I really do c coding mostly. – Iharob Al Asimi Jun 28 '15 at 21:30
  • http://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used if you want to learn more. :) – Baum mit Augen Jun 28 '15 at 21:34
  • Peculiar? GCC also has that: `-x c`. Otherwise just use the `.c` file extension to use the MS C compiler (again, just like GCC). – cremno Jun 28 '15 at 21:36
  • @cremno I didn't know that, I'll fix the answer. I just have been using linux for about 8yrs and didn't get to write many programs with Visual Studio, so I was based on what I've read online. – Iharob Al Asimi Jun 29 '15 at 12:21