[Ilugc] C pgm - Recursive -segmentation fault

  • From: Kannan_Ranganathan@xxxxxxxxxx (Kannan_Ranganathan)
  • Date: Mon Sep 13 17:24:53 2004

 As people rightly  pointed out it must be stack overflow.

But tell me an recursive function shld run forever(atleast in theory) right
??? 


Consider a case where my program does something repeatedly ..in such a case
I write a recursive program .what's the point in writing a recursive
function if it is going to exit abruptly.

Now the question arises how to clear this stack etc etc ???? 


Regards 
Kannan 

-----Original Message-----
From: ilugc-bounces@xxxxxxxxxxxxx [mailto:ilugc-bounces@xxxxxxxxxxxxx] On
Behalf Of Praveen
Sent: Monday, September 13, 2004 5:16 PM
To: Chakravarthi_Mohan
Cc: ilugc@xxxxxxxxxxxxx
Subject: Re: [Ilugc] C pgm - Recursive -segmentation fault

Chakravarthi_Mohan wrote:



void R()
{
    static int i =0;
    i++;
    printf("%d\n",i);

    R();
}

int main()
{
    R();
}

What is the problem here?

For any recursive function, you should have an exit criteria. Otherwise it
would recurse forever. Your function doesn't have an exit point.

When I run this program, Recursive Function R(), Called only 523714 
times, after that segmentation fault occurs WHY?

Since you don't have an exit point, repeated call of the function by itself
leads to stack overflow.

HTH.

--
Praveen Kumar
Web    : http://myriad-zero.net
Blog   : http://blog.myriad-zero.net
Mobile : +91-98407-02060
_______________________________________________
To unsubscribe, email ilugc-request@xxxxxxxxxxxxx with "unsubscribe
<password> address"
in the subject or body of the message.  
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc
************************************************************************** 
This email (including any attachments) is intended for the sole use of the
intended recipient/s and may contain material that is CONFIDENTIAL AND
PRIVATE COMPANY INFORMATION. Any review or reliance by others or copying or
distribution or forwarding of any or all of the contents in this message is
STRICTLY PROHIBITED. If you are not the intended recipient, please contact
the sender by email and delete all copies; your cooperation in this regard
is appreciated.
**************************************************************************

Other related posts: