Thread: Limiting recursive stack

  1. #1
    anonytmouse
    Guest

    Limiting recursive stack

    Hello,
    I have a recursive function and need to put a limit on the stack it can build up (ie. how many times it can call itself). I have two questions.

    A. What is a good limit that will work on most machines? Is ten fine?

    B. What is a good way of applying the limit? At first I thought I would just use a global or static variable and increment it as I go. The problem with this is how to initialize it. There is obvious problems with initializing it inside the function and I don't want to have to set it before I call the function each time. Is there any neat ways around this?

    Thanks heaps!

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The best way would be to use a static variable. You should just add a parameter to your function that tells when it is initialized.

    Example
    Code:
    int recursive(int s, int init) {
        static int iterations;
        if(init == -1) 
           iterations = 10;
    
        if(iterations--);
            recursive(s, 0);
    }

  3. #3
    anonytmouse
    Guest

    Wink

    Thanks mate!
    That was simple and quick!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  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