[Ilugc] C pgm - Recursive -segmentation fault

  • From: rsubr@xxxxxxxxxxxxxxxxxxxxxxxx (Raja Subramanian)
  • Date: Tue Sep 14 21:28:48 2004

Hi,

Kannan_Ranganathan wrote:

Wow ..This is something new. infact the whole argument is centered on
weather a recursive program will run forever or not.

Initially I had a belief that a recursive program will run forever. After
all these discussions I reconciled recursion is dependent on stack memory.

A recursive program without a base case will run forever.  It's just the
same as an infinite for/while/goto loop.  No big deal.

In C, each function call uses some stack space to store the return
address and local variables.  Therefore recursion will cause stack
growth (explosion rather) and terminate your process when stack space
is exhausted.

If you are careful, you can do recursion without growing your stack.
Then you can recurse forever.  Functional programming languages like
haskel, prolog, mercury have been good at this game for many years.

Note that you cannot eliminate *all* recursive calls.  You can only
eliminate recursive calls under special conditions,
eg.  tail recursion - recursive call is last statement in your function.

Compilers: Principles Techniques and Tools (aka Dragon Book) has a lot
of stuff for an ardent reader.

- Raja

Other related posts: