I'm trying to dynamically allocate a 2D array over 2 functions. I can allocate, but cleanup becomes messy.
CString **ppStrArray;
// Create a pointer
ppStrArray = new CString *[];
int rows, cols;
rows = cols = 0;
CreateArray( rows, cols, ppStrArray );
// Work with array
:
:
// Free memory
if( ppStrArray )
{
for( int i = 0; i < rows; i++ )
{
delete [] ppStrArray[i];
}
// It has a memory violation here
delete [] ppStrArray;
}
void CreateArray( int &row_count, int &col_count, CString **ppArray )
{
// For simplicity I set these, in reality these are determined
// elsewhere
row_count = 5;
col_count = 5;
if( ppArray )
{
for( int row = 0; row < ( int )numRows; row++ )
{
ppArray [row] = new CString[numCols];
}
}
return;
}
This code works if I change
ppStrArray = new CString *[];
to
ppStrArray = new CString *[5]; // 5 in this example
But I don't want to hard code the column count, the whole point of a dynamic array is because I don't know the number of columns. (I'm accessing a SQL server2000 database through stored procedures)
Can any one help?
Thanks,
Drewpee