I'm running a structural analysis program in VS2010 which iterates through a loop up to 4000 times. The program keeps breaking on me around the 960th loop and gives the following error
Unhandled exception at 0x0040cd1d in Structural Analysis Program.exe: 0xC000005: Access violation writing location 0x00000000.
The error seems to come back to the following function each time
Matrix2D getTranspose(Matrix2D a)
{
Matrix2D transpose = Matrix2D(a.getNumColumns(), a.getNumRows());
for(int i=1; i<=a.getNumRows(); i++)
{
for(int j=1; j<=a.getNumColumns(); j++)
{
transpose.setElement(j, i, a.getElement(i, j));
}
}
return transpose;
}
This is defined as a function in Matrix2D.cpp which also contains the function definitions for the class Matrix2D (the function declarations are contained in Matrix2D.h following the class declaration), where Matrix2D is an array defined by the following constructor.
Matrix2D::Matrix2D(int n_rows, int n_columns)
{
r = n_rows;
c = n_columns;
elements = (double**) malloc((r+1)*sizeof(double));
for (int i=0; i<=(r-1); i++)
{
elements[i] = (double*) malloc((c+1)*sizeof(double));
}
for(int i=0; i<=(r-1); i++)
{
for(int j=0; j<=(c-1); j++)
{
elements [i][j] = 0.0;
}
}
}
I hope there's enough information here for somebody to help me. I've very limited experience programming and can't get my head around this.
Thanks in advance.