2
Problem:
I maintain a 2D multidimensional array that has a dynamic size, is always growing, as I am not allowed to use vector
I need to develop a strategy to increase the size of this array at runtime.
Current strategy:
- Create new array
- Pass data from old array to new array
- Delete old array and replace with new
Currently I’m having trouble in the last phase, returns me strange errors like this:
malloc: Incorrect checksum for Freed Object 0x108a0b5d8: probably modified after being Freed.
Question:
What is the problem here and how to solve?
Code developed:
void person::increaseDataSize(const int newSize, int oldSize)
{
// Allocate new cells
personInformation * newData[newSize][newSize];
for(int i = 0; i < newSize; i++)
{
for(int j = 0; j < newSize; j++)
{
newData[i][j] = new personInformation();
}
}
// Fill new cells with old cells data
for(int i = 0; i < oldSize; i++)
{
for(int j = 0; j < oldSize; j++)
{
newData[i][j]->data0 = this->data[i][j]->data0;
}
}
// Delete old cells items
for(int i = 0; i < oldSize; i++)
{
for(int j = 0; j < oldSize; j++)
{
delete [] this->data[i][j];
}
}
// Fill old cells with new cells
for(int i = 0; i < newSize; i++)
{
for(int j = 0; j < newSize; j++)
{
this->data[i][j] = newData[i][j];
}
}
};
Thank you! But I believe that this is just one of the problems, as I keep receiving the error message mentioned.
– Vinícius Lara
I edited the answer and presented all the problems I found in your code.
– Douglas Oliveira
I understood perfectly what you said, but I’m having difficulties in relocation, what is the correct way to relocate? I can do oldData = date?
– Vinícius Lara
Aloque
newData
, copy the data to it, move itthis->data
and makethis->data
point tonewData
. All dynamically as I described above.– Douglas Oliveira