hey,
Just having trouble with creating a 2d array in the heap memory.
my code goes something like this:
twoDarray = new char[iSize][iSize];
where iSize is passed into the method.
cheers,
ActionMan
Printable View
hey,
Just having trouble with creating a 2d array in the heap memory.
my code goes something like this:
twoDarray = new char[iSize][iSize];
where iSize is passed into the method.
cheers,
ActionMan
sorry forgot to say that twoDArray is declared as follows..
char ** twoDarray;
You cannot do it like that. A char** is a pointer to a pointer!
I believe this is what you're looking for...
Code:
char **NewMatrix(int rows, int columns)
{
char **matrix = new char*[rows];
while(rows)
{
matrix[--rows] = new char[columns];
memset(matrix[rows], 0, columns);
}
return matrix;
}
//...You MUST call this to free up the memory...
void KillMatrix(char** matrix, int rows)
{
while(rows)
delete [] matrix[--rows];
}
int main(int argc, char *argv[])
{
int i, rows = 20, cols = 50;
char **strings = NewMatrix(rows, cols);
for(i = 0; i < rows; i++)
sprintf(strings[i], "\nString #%i", i+1);
for(i = 0; i < rows; i++)
{
printf(strings[i]);
getch();
}
printf("\n\n\nDeallocating Array!");
getch();
KillMatrix(strings, rows);
return 0;
}
hey,
thanks.. that the way I origionally had it. I was hoping new was a little more advanced.
thanks for the help!!
cheers,
ActionMan