3

I need to use an stack in a recursive function. Between function recursive calls the stack has to keep the contents and only modified by push or pop operations inside the function.

A way to do this it to define a global stack variable like this:

StackPtr stack = createStack();

Another way is to pass an stack to the function:

int recursiveFunction(int n, StackPtr stack);

Is there a way to do this but without global stack or passing an stack to the function? The idea is to encapsulate completely the function so the user just has to call it independently of the program specifications. It is like to define an static stack that conserve the stacks contents between recursive calls.

I tried:

int recursiveFunction(int n){
    static StackPtr stack = NULL;
    stack = createStack();
...
}

But the function reset the stack each call. I had to create the stack in the way shown because if I put:

static StackPtr stack = createStrack();

An "not initialized constant" error is thrown.

Thanks.

7
  • May be static local? I would pass the stack structure pointer as an argument. Also, what is 'StackPtr' is it a class? Commented Apr 23, 2018 at 15:19
  • You can add a boolean variable initialized with initial value of false to your try with the static stack. If it is false (will be so on the first call), create the stack and mark it true. Commented Apr 23, 2018 at 15:23
  • Not directly related, but I'd rethink the design. If you use an explicit stack, why do you need recursion in the first place? :o Commented Apr 23, 2018 at 15:29
  • Possible duplicate of How to initialize static pointer with malloc in C? Commented Apr 23, 2018 at 15:32
  • But each return addres, recursive or not, each local stack frame, recursive or not, each saved group of machine registers are stored/pushed an then back popped from the system global stack setup by the program loader and/or startup code before main() is called. Commented Apr 23, 2018 at 15:43

2 Answers 2

8

The usual solution is to use a helper function. The main function creates the stack (or whatever object is necessary), calls the helper (which is the actual recursive function) and then frees the stack before returning:

static int helper(StackPtr stack, int n) {
    ... /* Recursive calls to helper */
}

int mainFunction(int n){
    StackPtr stack = createStack();
    int rv = helper(stack, n)
    freeStack(stack);
    return rv;
}

Using a local static variable should normally be avoided since it makes the function non-reentrant and thread-unsafe.

Sign up to request clarification or add additional context in comments.

5 Comments

If the createStack and freeStack functions are expensive, this may not be the best solution. I upvoted this answer anyway.
I wanted to avoid calling the function with an stack parameter.
@carlitos_30: you call the main function, so the caller doesn't need to know anything about stacks. The rest is an implementation detail.
@michael: if the stack functions are expensive, they themselves could recycle stacks using a mutex-protected pool. However, it is very rare for that to be worthwhile.
@rici I understood what you meant. I confused the "mainFunction", with the "main" function. Thanks.
4

You can do this:

int recursiveFunction(int n){
  static StackPtr stack;

  if (stack == NULL)
    stack = createStack();

  ...
}

Upon startup, stackis initialized to NULL. The first time recursiveFunction is called, stack will be initialized. That's it.

But if thread safety is required, this solution won't work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.