Solved

VB5 Stack Space Limits

Posted on 1999-01-16
2
187 Views
Last Modified: 2010-05-03
Hi,
   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.
0
Comment
Question by:mrmazda
  • 2
2 Comments
 
LVL 3

Accepted Solution

by:
traygreen earned 130 total points
ID: 1469031
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
    oops(lNum)
  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.
0
 
LVL 3

Expert Comment

by:traygreen
ID: 1469032
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
  else
    Factorial = pNum * Factorial(pNum - 1)
  endif
end function

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now