Reputation: 772
Okay, I am trying to integrate some C code into a C++ project, and have run into a few problems. I will detail the first one here.
I keep running into this error:
error: cannot convert 'char*' to 'char**' in assignment|
here is the offending code (with the breakpoint marked):
char** space_getFactionPlanet( int *nplanets, int *factions, int nfactions )
{
int i,j,k;
Planet* planet;
char **tmp;
int ntmp;
int mtmp;
ntmp = 0;
mtmp = CHUNK_SIZE;
tmp = malloc(sizeof(char*) * mtmp); <--- Breakpt
The malloc function is derived from a C header. Here is the declaration:
_CRTIMP void* __cdecl __MINGW_NOTHROW malloc (size_t) __MINGW_ATTRIB_MALLOC;
I am using codeblocks, which is set to use MinGW. The above syntax is totally foreign to me.
I am totally stumped, since this code works fine in the C program I took it from.
Any Ideas?
EDIT 1:
Oops, just realized that the declaration is from stdlib.h.
EDIT 2:
I tried:
tmp = static_cast<char **>(malloc(sizeof(char*) * mtmp));
As suggested, but not I get error: invalid static_cast from type 'char*' to type 'char**'.
EDIT 3:
Okay, reinterpret_cast works, but the solution to replace mallocs seems much more elegantly simple, so I am going with that.
However, there is no free(tmp) at the end of the function. Is this a problem if I don't put in a delete tmp[]?
EDIT 4: I should add that tmp is returned by the function, so it is neccesary to delete tmp, or is this automatic?
Okay I am marking this solved. Thanks for your help.
Upvotes: 1
Views: 902
Reputation: 121
Change tmp = malloc(sizeof(char*) * mtmp);
to:
tmp = (char**)malloc(sizeof(char*) * mtmp);
And don't go around changing malloc's to new's, as some have wrongly suggested, because of two reasons:
1) its not broken, so don't fix it (!)
2) you could introduce subtle or flat out horrible bugs that will eat away hours of your life later on.
Do spend your time fixing broken code, not code that works...
Upvotes: 1
Reputation: 103751
If you're converting to C++, then let's do away with the malloc shall we?
tmp = new char*[mtmp];
But later on, you will probably find something like this:
free(tmp);
Which you need to change to this:
delete [] tmp;
If tmp is returned from the function, do not delete it. You need to trace the pointer. Somewhere, perhaps in multiple locations, free is going to be called. You need to replace that with delete if you are going with this solution. However, an even better solution would be to ditch the pointers all together, and replace it with a
vector<string>
Upvotes: 4
Reputation: 84239
malloc(3)
returns void*
, which could be assigned to any other pointer type in C, but not in C++. This is one of the places where C++ is not backward-compatible with C. You can either do what other posters said - cast it to char**
, or go all the way to proper C++ free store usage with operators new[]
and delete[]
:
char** tmp = new char*[mtmp]; // was malloc(sizeof(char*) * mtmp);
// ... use tmp
delete [] tmp; // was free( tmp );
Upvotes: 2
Reputation: 994529
C++ is not so free with pointer type conversions. You will have to do something like this:
tmp = static_cast<char **>(malloc(sizeof(char *) * mtmp));
That will work if your malloc()
returns a void*
. However, the errors you're getting indicate that your malloc()
is declared to return a char*
, and in this case you'd have to use reinterpret_cast
instead:
tmp = reinterpret_cast<char **>(malloc(sizeof(char *) * mtmp));
This casts the return type of malloc()
to a type suitable for assignment into tmp
. You can read more about the different types of casts in C++ at Type Casting.
Note that if this code must still compile in C, you can use a C-style cast instead:
tmp = (char **)malloc(sizeof(char *) * mtmp);
Upvotes: 5
Reputation: 12824
Perhaps a cast is in order:
tmp = static_cast<char **>(malloc(sizeof(char*) * mtmp));
Upvotes: 1