Thread: how to prevent stack-overflow?

  1. #1
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37

    how to prevent stack-overflow?

    hi all,
    I write some codes based in recursive method(or some similar methods),eg. in maze-searching... they work ,but sometimes cause stack-overflow.
    i try to write a function to prevent this, but i don't know if it works...
    1: use a varible size to count the number of recursion times
    2: after each call, check size using following code
    Code:
    .....
    if (size == 4*BUFSIZ)//prevent stack overflow after too many setps
      {
        printf("\nMaze too large,Stack overflowed!");
        exit(0);
      }
    ...
    the problem is that i'm not clear how many steps can a system stack hold...
    plz help with this, thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The stack has varying size depending on the system it's on. Some OS also allows you to set a new stack default. There's no portable, easy way of telling.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Like Elysia says, the stack may be "any" size - kernel stack in Windows is 8K [or at least it was on XP], whilst application stacks in Windows are "large" [as in hundreds of kilobytes or more - e.g. my gcc-compiled simple c++ code has a max stack-size of 2MB].

    In embedded systems, there may be really small stacks, a few hundred bytes may well be sufficient to do certain processing in a 16-bit system.

    The linker usually has some way to preset the stack-size to a max size - but it's dependent on the environment (compiler/linker & OS) what you have to do and what you CAN do [e.g. on a system with 64KB of memory, it's kind of hard to set a 2MB stack!]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I write some codes based in recursive method(or some similar methods),eg. in maze-searching
    Implement the equivalent non-recursive method instead.

    Recursion is just a disguised loop, so you can always implement an equivalent iterative form.
    If you do need to allocate space to preserve say your back-tracking information, you can always allocate some memory. The advantage is that you can easily spot the "out of memory" and take appropriate action.

    There is no equivalent mechanism for telling you're about to run out of stack, how much memory each stack frame takes up, or how much stack there is to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    One can usually go into a very significant amount of recursion before blowing the stack. The thing that will unnecessarily limit you are declaring too many variables within the recursive function, and not taking advantage of performing tail-recursion elimination refactoring.

    Certainly don't declare any arrays in the recursive function if you can at all help it.

    Post the code here if it isn't too big and we'll help refactor it to remove unnecessary variables, and make it at least partially iterative if possible.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You would want to return not exit(0) from a recursive call. Return will unwind the stack on the current call.

    If you never return you are guaranteed to overflow the stack.

  7. #7
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37

    :)

    Quote Originally Posted by iMalc View Post
    Post the code here if it isn't too big and we'll help refactor it to remove unnecessary variables, and make it at least partially iterative if possible.
    my code is too long to fit here , i'll manage to do it myself, thanks.

    thank you guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 03:20 PM
  2. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM