I am practicing how to create a 2D array and shrink it.
This is my function:
void Resize(int rownums,int columnnums)
{
MyMatrix newM(rownums,columnnums);
for(int i=0;i<rownums;i++)
for(int j=0;j<columnnums;j++)
newM.table[i][j]=table[i][j];
for(int i=0;i<rows;i++)
for(int j=0;j<columns;j++)
delete []table[j];
delete[]table;
rows=newM.rows;
columns=newM.columns;
table=new string*[rows];
for(int i=0;i<rows;i++)
table[i]=new string[columns];
for(int i=0;i<rows;i++)
for(int j=0;j<columns;j++)
table[i][j]=newM.table[i][j];
}
The problem is that it gives me a debug assertion failed.
I don’t understand what I did wrong or how I can fix it!!!
Anything you have in mind can help me!!
thank you
This is incorrect:
You don’t need the outer loop as it causes deletion of already deleted arrays, leading to undefined behavior.