St9bad_alloc exception in declaring double pointer
Hi,
I am working on a C++ program where I need to work with manipulation
of large matrices. I am getting St9bad_alloc exception thrown when I
try to allocate large matrices (typically >5000x5000). However, it
works fine with smaller matrices. Is that expected or there is
anything that I can do to get rid of this? Or, if it is a memory
issue, are there better memory-saving ways to handle 2D arrays? Here
is the piece of code that's throwing error:
double *phit, *dq, **Pq, **Aq, **AqT, **dAq, **AqAqT,**IAqAqT;
try
{
phit= new double[3*M];
dq= new double[3*M];
Pq= new double* [3*M];
for(i=0; i<3*M; i++)
Pq[i]=new double[3*M];
Aq= new double*[3];
for(i=0; i<3; i++)
Aq[i]=new double[3*M];
dAq= new double*[3];
for(i=0; i<3; i++)
dAq[i]=new double[3*M];
AqT=new double*[3*M];
for(i=0; i<3*M; i++)
AqT[i]=new double[3];
AqAqT=new double*[3];
for(i=0; i<3; i++)
AqAqT[i]=new double[3];
IAqAqT=new double*[3];
for(i=0; i<3; i++)
IAqAqT[i]=new double[3];
}
catch (exception& e)
{
cout << "Standard exception in 3-D pointer allocation: " << e.what()
<< endl;
exit(1);
}
I am deallocating pointers in the standard way:
try
{
for(int i=0; i<3*M; i++)
delete[] AqT[i],Pq[i];
for(int i=0; i<3; i++)
delete[] Aq[i], dAq[i], AqAqT[i],IAqAqT[i];
delete[] Aq,AqT,AqAqT,IAqAqT,Pq,phit,dq;
}
catch (exception& e)
{
cout << "Standard exception in deAllocation: " << e.what() << endl;
exit(1);
}
Thanks,
Vijay
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]