#define MALLOC(p,s)\
if(!(p=malloc(s)))\
printf("Not enough memory");
#define MALLOC(ptr,s)\
if(!(ptr=malloc(s)))\
std::cout<<"Not enough memory";
This runs perfectly in c but in cpp it throws error int* cannot be converted to void*
#define MALLOC(p,s)\
if(!(p=malloc(s)))\
printf("Not enough memory");
#define MALLOC(ptr,s)\
if(!(ptr=malloc(s)))\
std::cout<<"Not enough memory";
This runs perfectly in c but in cpp it throws error int* cannot be converted to void*
It is the other way around: void*
cannot be converted to int*
in C++
- unlike in C
.
malloc
returns void*
, and apparently you invoke this macro with int* ptr
. You can make the 2nd macro compiled in C++
in this particular case by adding a cast:
#define MALLOC(ptr,s)\
if(!(ptr=static_cast<int*>(malloc(s))))\
std::cout<<"Not enough memory";
Or more general (starting from C++11
):
#define MALLOC(ptr,s)\
if(!(ptr=static_cast<decltype(ptr)>(malloc(s))))\
std::cout<<"Not enough memory";
Of course, idiomatic C++
does not use malloc()
at all.