Reputation: 59516
When writing production-quality VC++ code, is the use of recursion acceptable? Why or why not?
Upvotes: 1
Views: 351
Reputation: 54823
Recursion is almost essential to traverse File structures like folder/directories.
Traversing a tree like structure is very easy if recursion is used.
Upvotes: 0
Reputation: 123662
Is there a way to determine at what point I would encounter a stack overflow?
Not really. A stack overflow happens when you exhaust the stack space - however...
The only times I've ever hit one is in an infinite loop, or using the aforementioned 200k buffer.
I find it far more prefereable for my app to just crash, than for it to loop forever using 100% CPU and have to be forcefully killed (this is a right PITA on a remote server over a bad connection as windows lacks SSH)
A rough guideline: Do you think your recursive function is likely to call itself more than say 10,000 times consecutively? Or are you doing something dumb like allocating 200k buffers on the stack?
If yes, worry about it.
If no, carry on with more important things.
Upvotes: 6
Reputation: 16486
Is there a way to determine at what point I would encounter a stack overflow?
Depends how deep you go, and how large the actual recursion is. I take it you understand what recursion does?
Upvotes: 0
Reputation: 11910
Sure - e.g. if you want to traverse a tree structure what else would you use ?
Maybe you would like to have something like a maximum depth to be sure you're not writing an infinite loop. (if this makes sense in your example)
Upvotes: 0