Re: St9bad_alloc exception in declaring double pointer
vijaykaus@gmail.com wrote:
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];
You don't say what M is, but this statement potentially allocates a
lot of memory, like 3 * M * 3 * M * sizeof(double). For a sufficently
large M, a std::bad_alloc would be expected on a 32 bit machine.
Bo Persson
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);
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]