Tail recursion in C

Doug Gwyn gwyn at smoke.BRL.MIL
Sat Jul 15 14:52:51 AEST 1989


In article <33133 at apple.Apple.COM> landon at Apple.COM (Landon Dyer) writes:
>At first glance, 'f()' is a tail-recursive function that recurses 'n+1'
>times.  But we're in trouble, because third parties (the global 'gLinkList'
>in this case) have pointers to locals that are contained in the control
>stack.

C allows general recursion, not just tail recursion.  Of course you're
constrained in that you can't legally use variables once their lifetime
has expired; however in your example all autos in the function nest
would still be alive for the n<0 branch of the innermost call.  What
you DO have to watch out for is not relying on the auto storage
mechanism to provide storage outside the dynamic scoping inherent in
the use of a stack discipline.  That's what the heap (malloc()) is for.



More information about the Comp.lang.c mailing list