[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB5 Stack Space Limits

Posted on 1999-01-16
2
Medium Priority
?
200 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 390 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

872 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