How to define and use large dynamic two dimension arrays

A

Atemporal

hi, all, i got some problem when define and use large dynamic two
dimension arrays.
At first, i use vector<vector<double>> p1, i have three such arrays
and each is around 10000*10000, the program compiles ok, however, when
it gets following error when running

Unhandled exception at 0x7c812aeb in Value.exe: Microsoft C++
exception: std::bad_alloc at memory location 0x0013f988..

Then I turn to use the "new" method like the follows,

double **Profit1;
double **Profit2;
double **qq;

Profit1 = new double*[n1+1];
Profit2 = new double*[n1+1];
qq = new double*[n1+1];

for(int i=0;i<=n1;i++)
{
std::cout << i << std::endl;
Profit1 = new double[n2+1];
Profit2 = new double[n2+1];
qq = new double[n2+1];
}

I still get the similar error.
What shall I do?
As I'm a newbie, please answer my question in a little more details,
thanks a lot.
 
L

Lionel B

hi, all, i got some problem when define and use large dynamic two
dimension arrays.
At first, i use vector<vector<double>> p1,

i have three such arrays and
each is around 10000*10000, the program compiles ok, however, when it
gets following error when running

Unhandled exception at 0x7c812aeb in Value.exe: Microsoft C++ exception:
std::bad_alloc at memory location 0x0013f988..

Blimey, I'm not surprised. 10000 x 10000 doubles is ... how many Gb of
memory? Your memory allocation failed.
What shall I do?

Request less memory. We can't tell you how to do that, since we don't
know what problem you're trying to solve (and it would probably be OT
here anyway). If you figure out how to reduce the memory requirements of
your program and have any implementation queries by all means ask again
here.

Regards,
 
J

James Kanze

Atemporal wrote:
There is a template class that might help - see this:

I don't see anything there that would solve his problem: a
bad_alloc exception.
You're allocating 400MB, make sure that's really what you want to do.

He's allocating 800MB. Which still shouldn't be a problem on
most modern, general purpose machines. And he's doing it three
times, which starts becoming a problem on many of the smaller
machines. For starters, he should get a machine with something
like 4GB main memory. (I can allocate two such vector on my
old Sparc, with only 512MB, but it ends up thrashing so much
that the machine becomes unusable.)

Alternatively, it's not too difficult to design a matrix class
which uses disk storage. But making each access a disk access
isn't going to speed things up; most likely, if he cannot get
the more powerful machine, then he'll probably have to carefully
organize the process so he only works on part of the data at a
time, to avoid constantly reading and writing to disk.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top