How to define and use large dynamic two dimension arrays

Discussion in 'C++' started by Atemporal, Jun 9, 2008.

  1. Atemporal

    Atemporal Guest

    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.
     
    Atemporal, Jun 9, 2008
    #1
    1. Advertising

  2. Atemporal

    Lionel B Guest

    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
     
    Lionel B, Jun 9, 2008
    #2
    1. Advertising

  3. Gianni Mariani, Jun 9, 2008
    #3
  4. Atemporal

    James Kanze Guest

    On Jun 9, 1:52 pm, 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.comp.lang.learn.c-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:
    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
     
    James Kanze, Jun 9, 2008
    #4
  5. Atemporal

    Lionel B Guest

    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.comp.lang.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
     
    Lionel B, Jun 9, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    27
    Views:
    615
    Barry Schwarz
    Sep 7, 2006
  2. Replies:
    3
    Views:
    331
    Barry Schwarz
    Jun 10, 2007
  3. DiAvOl
    Replies:
    17
    Views:
    586
    John Bode
    Jan 7, 2008
  4. Luuk
    Replies:
    15
    Views:
    843
    Nobody
    Feb 11, 2010
  5. Kev Jackson
    Replies:
    2
    Views:
    132
Loading...

Share This Page