0

Perhaps there is a fancy term for this. Lets say I have this code:

void a(){
  b();
}

void b(){
  c();
}

void c(){
  a();
  // do something
  if (FLAG_COMPLETE){
      return;
  }
}

The problem is it will go a->b->c->a->b->c until it FLAG_COMPLETE goes up. After it will track back through ALL those calls in back order. Is there a way to avoid the return to caller function from the callee? I know in assembly this would be quite easy to do.

I am using C++ CLI. I want to avoid overflowing the stack by calling functions in a loop.

Mewa
  • 502
  • 1
  • 9
  • 24
  • See: http://stackoverflow.com/questions/34125/which-if-any-c-compilers-do-tail-recursion-optimization – Reed Copsey Aug 10 '12 at 20:36
  • I see youve target it `stackoverflow` ;) quite literal. This is essentially a quite broken bit of design, but I guess you've found yourself here courtesy of somebody else's code. What did you intend doing if did detect recursion? Assert? – marko Aug 10 '12 at 20:43
  • @ReedCopsey, Thank you, that works great. It would have helped a great bit in my search to know what it's called. – Mewa Aug 10 '12 at 20:46

1 Answers1

1

If I read between the lines, it sounds like you want the contents of a(), then b(), then c(), to run forever, until FLAG_COMPLETE is set? If that's the case, then why not do that explicitly?

void RunUntilComplete()
{
    while(!FLAG_COMPLETE)
    {
        ContentsOfA();
        ContentsOfB();
        ContentsOfC();
    }
}
David Yaw
  • 27,383
  • 4
  • 60
  • 93