G
Gumby
I want to make a two-d array of unsigned ints that
I can change the size of as I need more memory. To
do this I put in the h file a simple pointer to the
memory and row/col variables to retain the current size.
private:
unsigned int* mergeKeyTable;
unsigned int rows;
unsigned int cols;
and then in the cc file, methods setup the amount of memory and
rows, cols record what the dimensions are:
Foo::Foo()
{
........
initializeTable(20,20); // start off with 400 u.ints
}
void Foo::initializeTable(unsigned therows, unsigned thecols)
{
rows = therows;
cols = thecols;
mergeKeyTable = new unsigned int[rows * cols];
memset(mergeKeyTable, 0, sizeof(unsigned int)*rows*cols);
}
void Foo::addTableValue(unsigned rowpos, unsigned colpos, unsigned value)
{
//the table is too small, make a bigger one
if (rowpos > rows || colpos > cols)
{
delete []mergeKeyTable; // problem here?, how can delete
// know the size?
initializeTable(rowpos, colpos); // make the bigger one..
}
// put the value number at the right position via pointer arithmetic
*(mergeKeyTable + (rows * rowpos) + colpos) = value;
}
the initialize..() func runs from the constructor and if the addTableValue()
is called with a dimension bigger than what would fit in the table it sets
the dimensions, then new's a bigger table. 'value' is then
put in the table at the right position.
The question is how can I delete the memory properly? Usually delete is
passed a true object like a class or a basic type or if you did
make an array via new, the size would be there in the [][] like this:
unsigned int* mergeKeyTable = new unsigned int[100][100];
......
delete [] mergeKeyTable; // OK delete 10000 Uints
then that would be ok, since the compiler can figure out what the
size of mergeKeyTable is because the numbers are right there in the
code. But if the new function is passed the rows and cols at run time and
creates the array does the delete[] do the right thing?
Mark
I can change the size of as I need more memory. To
do this I put in the h file a simple pointer to the
memory and row/col variables to retain the current size.
private:
unsigned int* mergeKeyTable;
unsigned int rows;
unsigned int cols;
and then in the cc file, methods setup the amount of memory and
rows, cols record what the dimensions are:
Foo::Foo()
{
........
initializeTable(20,20); // start off with 400 u.ints
}
void Foo::initializeTable(unsigned therows, unsigned thecols)
{
rows = therows;
cols = thecols;
mergeKeyTable = new unsigned int[rows * cols];
memset(mergeKeyTable, 0, sizeof(unsigned int)*rows*cols);
}
void Foo::addTableValue(unsigned rowpos, unsigned colpos, unsigned value)
{
//the table is too small, make a bigger one
if (rowpos > rows || colpos > cols)
{
delete []mergeKeyTable; // problem here?, how can delete
// know the size?
initializeTable(rowpos, colpos); // make the bigger one..
}
// put the value number at the right position via pointer arithmetic
*(mergeKeyTable + (rows * rowpos) + colpos) = value;
}
the initialize..() func runs from the constructor and if the addTableValue()
is called with a dimension bigger than what would fit in the table it sets
the dimensions, then new's a bigger table. 'value' is then
put in the table at the right position.
The question is how can I delete the memory properly? Usually delete is
passed a true object like a class or a basic type or if you did
make an array via new, the size would be there in the [][] like this:
unsigned int* mergeKeyTable = new unsigned int[100][100];
......
delete [] mergeKeyTable; // OK delete 10000 Uints
then that would be ok, since the compiler can figure out what the
size of mergeKeyTable is because the numbers are right there in the
code. But if the new function is passed the rows and cols at run time and
creates the array does the delete[] do the right thing?
Mark