Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to define and use large dynamic two dimension arrays

Reply
Thread Tools

How to define and use large dynamic two dimension arrays

 
 
Atemporal
Guest
Posts: n/a
 
      06-09-2008
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[i] = new double[n2+1];
Profit2[i] = new double[n2+1];
qq[i] = 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.
 
Reply With Quote
 
 
 
 
Lionel B
Guest
Posts: n/a
 
      06-09-2008
On Mon, 09 Jun 2008 00:23:38 -0700, Atemporal wrote:

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


Sounds reasonable (it has to be `vector<vector<double> >', but I guess
you know that already).

> 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,

--
Lionel B
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      06-09-2008
Atemporal wrote:
....
> What shall I do?
> As I'm a newbie, please answer my question in a little more details,
> thanks a lot.


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

http://groups.google.com/group/alt.c...n&dmode=source

You're allocating 400MB, make sure that's really what you want to do.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-09-2008
On Jun 9, 1:52 pm, Gianni Mariani <(E-Mail Removed)> wrote:
> Atemporal wrote:


> ...


> > What shall I do?
> > As I'm a newbie, please answer my question in a little more details,
> > thanks a lot.


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


> http://groups.google.com/group/alt.c...+/msg/08904bff...


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.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      06-09-2008
On Mon, 09 Jun 2008 11:52:52 +0000, Gianni Mariani wrote:

> Atemporal wrote:
> ...
>> What shall I do?
>> As I'm a newbie, please answer my question in a little more details,
>> thanks a lot.

>
> There is a template class that might help - see this:
>
> http://groups.google.com/group/alt.c...g.learn.c-c++/

msg/08904bffef6f4ac1?hl=en&dmode=source
>
> You're allocating 400MB, make sure that's really what you want to do.


More than that, surely? If a double is 8 bytes then memory = (approx, at
least) 3 x 10000 x 10000 x 8 bytes = 2400 MB = 2.4 GB.

--
Lionel B
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
a question about 1-dimension and 2-dimension array Luuk C Programming 15 02-11-2010 02:45 AM
Does this use dynamic two-dimension array correctly? lovecreatesbea...@gmail.com C Programming 3 06-10-2007 12:30 PM
two arrays problem (although different from the other two arrays) Kev Jackson Ruby 2 03-29-2006 03:58 PM
newbie question : picture dimension vs print dimension Rene Wong Digital Photography 9 09-30-2003 01:46 AM



Advertisments