views:

210

answers:

6

Would you use recursion in live VC++ code?

+2  A: 

Yes. But never in dead code. That would be silly.

Shog9
It's not dead! It's pining for the fjords.
Aardvark
@Aardvark - it's really too bad that you can't vote up comments ;-)
Ferruccio
A: 

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)

bernhardrusch
A: 

Is there a way to determine at what point I would encounter a stack overflow?

Vulcan Eager
A: 

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?

graham.reeds
+6  A: 

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 initial stack size can be changed programatically and may default to different amounts depending on your OS/compiler/etc
  • How much of it is already used up depends on what your app (and the libraries your app uses) has previously done - this is often impossible to predict
  • How much of the stack each call requires depends on what you do in your function. If you only allocate say 1 integer on the stack, you may be able to recurse an enourmous amount of times, but if you are allocating a 200k buffer on the stack, not so much.

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.

Orion Edwards
A: 

Recursion is almost essential to traverse File structures like folder/directories.

Traversing a tree like structure is very easy if recursion is used.

Niyaz