Solved

VB5 Stack Space Limits

Posted on 1999-01-16
2
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Sending a email via excel using vba 6 115
vbModal 12 73
checkbox to hide entire section 10 75
Fastest way to find and count same items VB6 16 62
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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

738 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