VB5 Stack Space Limits

   Recently noted a confusing (to me) explaination of some utility code in the VB area of the Planet Source code website. The code was called "Overflow_Stack" and the code snippet determined the amount of VB Stack space. Here's it's explaination:
   Each VB App uses a single stack limited in size (20K in VB 3.0 Etc). The size can't be changed, so an "Out of Stack Space error can occur if your program performs uncontrolled recursion, such as a cascading event. Recursion occurs when a procedure calls itself (often repeatedly).
   I wonder if someone could give me a better example of "uncontrolled recursion"? Don't get the "cascading event/procedure calling itself examples.
   Is this guy talking about, say as a super theoretical example, having a procedure which contains an infinite Do While Loop, which itself calls other procedures(say each prints something) many many times over?
   Any ideas would be appreciated.
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

traygreenConnect With a Mentor Commented:
Recursion is when a code segment - say procedure or function calls itself
It is useful esp when doing calculations or string manipulations.

Problems arise when the termination of the recursive calls are not well though out.  It can lead to an infinite (or finite due to stack overflow) looping with the function calling itself.  It is the same concept as beginning a loop without an exit point.

The difference is that when using recursion, the function or proc will allocate space for each variable declared within itself.  This is where the memory issue comes into play.

Function Oops(byval pNum as long)as long
  dim lNum as long
  lNum= pNum + 1
  if  lNum <> 5 then
    msgbox pNum
  end if
  msgbox "5"
end function

private sub command1_Click()
  call oops(5)
end sub

this code will enter into a recursive loop from which it can not exit.  Everytime through the loop the contents of pNum and lNum will be pushed to the stack (kept in memory).  Eventually the stack size (physical allocation of memory) will be exceeded and the stack overflow error will occur.
Basically a stack is a last in first out stucture.  A simple example of recursion would be solving a factorial.
eg 4! = 4*3*2*1 = 24
To solve this using recusion

Function Factorial (pNum as long) as Long
  if pNum = 0 Then
    Factorial = 1
    Factorial = pNum * Factorial(pNum - 1)
end function

All Courses

From novice to tech pro — start learning today.