stack overflow just because of a double array[500][500]?

Discussion in 'C++' started by James, Nov 3, 2004.

  1. James

    James Guest

    Hi,

    I am using Visual C++ 6.0. I got a "stack overflow" error message when
    running the program because of a "double array[500][500]". I have a
    computer with 1GB memory. Can I extend the memory for the program
    running? How?

    Looking forward to hearing from you!

    Regards,

    James
    James, Nov 3, 2004
    #1
    1. Advertising

  2. "James" <> wrote in message
    news:...
    > Hi,
    >
    > I am using Visual C++ 6.0. I got a "stack overflow" error message when
    > running the program because of a "double array[500][500]". I have a
    > computer with 1GB memory. Can I extend the memory for the program
    > running? How?


    Possibly but how big the stack is something that your compiler specifies not
    the C++ language. Ask in a compiler specific group for an ansewr to this
    question.

    But really you should use dynamic allocation, any of the methods I suggested
    in response to your last post would be prefereable to trying to declare such
    a huge array on the stack.

    For instance

    #include <vector>

    std::vector<std::vector<double> > array(500);
    for (int i = 0; i < 500; ++i)
    array.resize(500);

    is a simple way to give you a 500 by 500 2D array without blowing your
    stack.

    john
    John Harrison, Nov 3, 2004
    #2
    1. Advertising

  3. James wrote:
    > Hi,
    >
    > I am using Visual C++ 6.0. I got a "stack overflow" error message when
    > running the program because of a "double array[500][500]". I have a
    > computer with 1GB memory. Can I extend the memory for the program
    > running? How?


    It's not the "memory for the program running" it's the stack size
    that is too small. <OT> You may adjust it via some compiler options,
    I believe. </OT>.

    Better than that, don't have this 4MB array allocated on stack
    (which happens, for instance, if it's a local variable inside a
    function) -- try going for dynamical allocation, using 'new' for
    example. See your book's index for "new", "dynamical allocation",
    and also the C++ FAQ-lite for how to handle two-dimensional
    arrays using 'new'.

    You might also consider using a
    std::vector< std::vector <double> >
    for storing your doubles.

    HTH,
    - J.
    Jacek Dziedzic, Nov 3, 2004
    #3
    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. Sydex
    Replies:
    12
    Views:
    6,453
    Victor Bazarov
    Feb 17, 2005
  2. Replies:
    5
    Views:
    418
    James Kanze
    Jun 27, 2008
  3. A.E lover

    large array?? how to avoid stack overflow?

    A.E lover, Jan 13, 2009, in forum: C Programming
    Replies:
    5
    Views:
    3,613
    Keith Thompson
    Jan 14, 2009
  4. Sam Roberts
    Replies:
    1
    Views:
    213
    Yukihiro Matsumoto
    Feb 11, 2005
  5. Kenneth McDonald

    Why stack overflow with such a small stack?

    Kenneth McDonald, Aug 30, 2007, in forum: Ruby
    Replies:
    7
    Views:
    249
    Kenneth McDonald
    Sep 1, 2007
Loading...

Share This Page