Increase multidimensional array size at runtime

Asked

Viewed 88 times

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];
        }
    }
};

1 answer

2


If you want to increase the size of the array (#rows and #columns), then the array itself needs to be dynamically allocated. You have not shown how this->data was allocated, but if it is equal to how allocated newData, then it wasn’t dynamically.

Dynamic allocation of a 2D array is done according to the following code.

personInformation*** data = new personInformation** [size]; // aloca as linhas
for(int i = 0; i < size; i++) {
    data[i] = new personInformation* [size]; // aloca as colunas por linha
    for(int j = 0; j < size; j++)
        data[i][j] = new personInformation(); // aloca o objeto
}

And the relocation should be as follows:

for(int i = 0; i < size; i++) {
    for(int j = 0; j < size; j++)
        delete data[i][j]; // libera o objeto
    delete [] data[i]; // libera array coluna
}
delete [] data; // libera array linha

Your allocation of newData presents a potential problem. To allocate an array statically, size should be known in compilation time. Function parameters are not necessarily known at compile time. If applicable, your program may display undefined behavior.

You use the delete [] to release a simple object. This way is to release dynamic arrays (allocated with new []). For simple objects use only delete data[i][j].

In the last loop you wear newSize to iterate on this->data smaller in size (oldSize). You need to deselect the entire array and relocate to the new size before making this loop.

  • Thank you! But I believe that this is just one of the problems, as I keep receiving the error message mentioned.

  • I edited the answer and presented all the problems I found in your code.

  • 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?

  • 1

    Aloque newData, copy the data to it, move it this->data and make this->data point to newData. All dynamically as I described above.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.