stack overflow

Discussion in 'C++' started by Piet L., Jan 15, 2005.

  1. Piet L.

    Piet L. Guest

    hey,

    I have a program where you can set different parameters.
    One of the parameters is Nr_Of_Runs.
    This number will then be used in an array:
    success[Nr_Of_Runs].
    It works with Nr_Of_Runs <= 5000
    But if I what to do more runs, I get a stack overflow.
    Can someone help?

    thx

    PL.
     
    Piet L., Jan 15, 2005
    #1
    1. Advertising

  2. > hey,
    Yo, dude.

    > I have a program where you can set different parameters.
    > One of the parameters is Nr_Of_Runs.
    > This number will then be used in an array:
    > success[Nr_Of_Runs].
    > It works with Nr_Of_Runs <= 5000
    > But if I what to do more runs, I get a stack overflow.
    > Can someone help?


    Use std::vector<whateverTypeSuccessHad> instead of array.

    > thx

    Anytime.

    --
    --Larry Brasfield
    email:
    Above views may belong only to me.
    "Piet L." <> wrote in message news:...
     
    Larry Brasfield, Jan 15, 2005
    #2
    1. Advertising

  3. Piet L. wrote:

    > hey,
    >
    > I have a program where you can set different parameters.
    > One of the parameters is Nr_Of_Runs.
    > This number will then be used in an array:
    > success[Nr_Of_Runs].
    > It works with Nr_Of_Runs <= 5000
    > But if I what to do more runs, I get a stack overflow.
    > Can someone help?


    Stack has limited space. Use std::vector instead of built in arrays.




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Jan 15, 2005
    #3
  4. Piet L.

    Piet L. Guest

    Yes,
    but I also want to use dimensional arrays.
    How about that?
    Can't I extend my memory use or so?
    I'm using Windows XP, and have a memory of 1024
     
    Piet L., Jan 15, 2005
    #4
  5. Piet L. wrote:

    > Yes,
    > but I also want to use dimensional arrays.
    > How about that?
    > Can't I extend my memory use or so?
    > I'm using Windows XP, and have a memory of 1024



    I do not know whose message you replied to, however you can use vectors
    for multi-dimensional arrays. Here is a two-dimensional 30x20 vector of
    ints, all initialised to 0:


    #include <vector>


    int main()
    {
    using namespace std;

    vector<vector<int> >array(30, vector<int>(20));
    }



    Notice the space, between the two '>'s. If you place them together, the
    compiler may get confuse it with the operator >> (bitwise or whatever)
    and produce an error.



    Basically, what you need to do is to read a good up to date ISO C++
    book. If you have some programming background in any language, a good
    choice is "Accelerated C++" by Andrew Koenig, Barbara Moo:

    http://www.acceleratedcpp.com




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Jan 15, 2005
    #5
  6. Piet L.

    Piet L. Guest

    it seems like my previous message didn't came true...

    I want to use stacks because I need 2 dimensional arrays.
    thx
     
    Piet L., Jan 15, 2005
    #6
  7. Piet L. wrote:

    > it seems like my previous message didn't came true...
    >
    > I want to use stacks because I need 2 dimensional arrays.



    Did you see my reply?




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Jan 15, 2005
    #7
  8. "Piet L." <> wrote in message news:...
    > it seems like my previous message didn't came true...
    >
    > I want to use stacks because I need 2 dimensional arrays.


    That is a non-sequitur. You can create 2D arrays either
    on the stack or elsewhere. For your arrays that blow
    the default stack limit, unless you do nothing with them
    the time needed to dynamically allocate it will be small
    compared to the time spent manipulating it. So the
    advice Mr. Vranos and I have tendered stands: Use
    std::vector, which will handle the detail of dynamic array
    allocation for you and act very much like an array.

    Alternatively, consult the documentation for the compiler
    and/or linker that you use so that you can set a stack size
    limit appropriate to your 2D array created on the stack.
    Just how to do that is off-topic here.

    > thx

    You're welcome.

    --
    --Larry Brasfield
    email:
    Above views may belong only to me.
     
    Larry Brasfield, Jan 15, 2005
    #8
  9. Piet L.

    rami Guest

    if this is just test program you can tune the stack size in your
    complier options and if for production better switch to heap based
    memory allocation...
     
    rami, Jan 17, 2005
    #9
  10. Piet L.

    Howard Guest

    "Piet L." <> wrote in message
    news:...
    > hey,
    >
    > I have a program where you can set different parameters.
    > One of the parameters is Nr_Of_Runs.
    > This number will then be used in an array:
    > success[Nr_Of_Runs].
    > It works with Nr_Of_Runs <= 5000
    > But if I what to do more runs, I get a stack overflow.
    > Can someone help?
    >
    > thx
    >
    > PL.


    I don't see how you can be using a variable sized array but getting stack
    errors? If you dynamically allocate an array (using "new"), then it's going
    into the "free store" (heap), not the stack. On the other hand, if you're
    simply declaring the array with a constant size, then I fail to see how
    you're using Nr_Of_Runs, since it needs to be a compile-time-constant, and
    the ability to set it tends to imply you're allocating the array
    dynamically. How about some code that demonstrates the problem?
    -Howard
     
    Howard, Jan 17, 2005
    #10
    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. =?Utf-8?B?amJpeEBuZXdzZ3JvdXBzLm5vc3BhbQ==?=

    Stack overflow exception

    =?Utf-8?B?amJpeEBuZXdzZ3JvdXBzLm5vc3BhbQ==?=, Apr 20, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    7,260
    Rick Spiewak
    Apr 22, 2004
  2. Mr m?ll
    Replies:
    2
    Views:
    1,407
    Mr m?ll
    Oct 16, 2004
  3. Replies:
    0
    Views:
    499
  4. Replies:
    0
    Views:
    405
  5. Kenneth McDonald

    Why stack overflow with such a small stack?

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

Share This Page