In an earlier question, I asked about typecasting pointers, but was directed to the better solution of using the C++ allocation system instead of mallocs. (I am converting some C code to C++)
However, I still have an issue with a similar function:
I changed:
tmp = malloc(sizeof(char*) * mtmp); --> tmp = new char*[mtmp];
and
free(tmp) --> delete [] tmp;
However, what do I do with realloc in the following function:
char* space_getRndPlanet (void)
{
int i,j;
char **tmp;
int ntmp;
int mtmp;
char *res;
ntmp = 0;
mtmp = CHUNK_SIZE;
//tmp = malloc(sizeof(char*) * mtmp); <-- replaced with line below
tmp = new char*[mtmp];
for (i=0; i<systems_nstack; i++)
for (j=0; j<systems_stack[i].nplanets; j++) {
if(systems_stack[i].planets[j]->real == ASSET_REAL) {
ntmp++;
if (ntmp > mtmp) { /* need more space */
mtmp += CHUNK_SIZE;
tmp = realloc(tmp, sizeof(char*) * mtmp); <--- Realloc
}
tmp[ntmp-1] = systems_stack[i].planets[j]->name;
I am getting the following error:
error: invalid conversion from 'void*' to 'char**'|
EDIT 2:
Okay, the consensus I am getting is that I should ditch my current solution (which I am open to doing).
Just to make sure that I am understanding correctly, do you guys mean that, instead of an array of pointers to objects, I should just have a vector containing the objects themselves?