[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

VB5 Stack Space Limits

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Suggested Courses

656 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