-
May 3rd, 2004, 11:03 AM
#1
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
-
May 3rd, 2004, 11:13 AM
#2
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
-
May 3rd, 2004, 11:25 AM
#3
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
-
May 3rd, 2004, 11:31 AM
#4
sounds as a loop job to me.
why do you insist of a recursive function?
**** **** **** **** **/**
-
May 3rd, 2004, 11:32 AM
#5
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);
-
May 3rd, 2004, 11:41 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|