CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2000
    Location
    Germany
    Posts
    115

    Question function recursion

    Hi NG,

    in a ANSI-C program I use a function <fi_load()>, wich is called recursive appending of new data. The program normally never ends and <fi_load()> calls himself again and again...

    - Is this a problem, because the program must remeber all of the returns/recursion-adresses?
    - May it cause an overflow?
    - What is a better way in ANSI-C?

    This is a sample-code to show, what I mean:
    Code:
    int main(int argc, char* argv[])
    {
    	fi_load("index.pts");
    	return 0;
    }
    
    int fi_load(char* file[])
    {
    	char str_next[80];
    
    	<... read file and do something ...>
    	<... if an error occurs : return 1>
    	<... get name of next file to process : str_next>
    	
    :confused:	fi_load(str_next);
    
    	return 0;
    }
    Thanks in advance,
    Ferdinando

  2. #2
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729
    There's no terminating condition in the function. In order for recursion to work properly, there has to be a terminating condition and the data you pass to the function being called recursively has to approach the terminating condition. And yes, very deep recursion can cause stack exhaustion.
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  3. #3
    Join Date
    Aug 2000
    Location
    Germany
    Posts
    115
    Originally posted by cma
    There's no terminating condition in the function. In order for recursion to work properly, there has to be a terminating condition and the data you pass to the function being called recursively has to approach the terminating condition. And yes, very deep recursion can cause stack exhaustion.
    There is a terminating condition, but only if an error occurs...

    I know that this is not enough, but how can I implement a better recursion?

    F.E. I have got 5 files FILE1, FILE2 .. FILE5
    Each of this FILE contains a pointer to the next file to process.
    F.E. (in real it is appending of user-inputs...)
    FILE1 => FILE2
    FILE2 => FILE3
    ...
    FILE5 => FILE1

    As you see, the chain never ends and I must process each file with the same code/function.

    How can I do it better?

    Thanks again,
    Ferdinando

  4. #4
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    sounds as a loop job to me.
    why do you insist of a recursive function?
    **** **** **** **** **/**

  5. #5
    Join Date
    Jul 2002
    Location
    St. Louis, MO
    Posts
    484
    Use a loop, until you have processed all the files.... Then you don't need the recursive call at all (just call the function once to load each file)..

    Code:
    MyFILE* f = (first file)...;
    
    do
    {
        load(f);
        f = f->next;
    } while(f);

  6. #6
    Join Date
    Aug 2000
    Location
    Germany
    Posts
    115
    Originally posted by HeartBreakKid
    Use a loop, until you have processed all the files.... Then you don't need the recursive call at all (just call the function once to load each file)..

    Code:
    MyFILE* f = (first file)...;
    
    do
    {
        load(f);
        f = f->next;
    } while(f);
    A very good idea instead of recusion...
    Since I started again with ANSI-C I am a big blockhead

    Thanks again,
    Ferdinando

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured