Visual Basic Tip: 01/08/96


Stack Space.

Each Visual Basic application uses a single stack limited to 20K
in size (20K in VB 3.0, 16K in VB 1.0 and 2.0).  The 20K size
can't be changed, so an "Out of Stack Space" error can easily
occur if your program performs uncontrolled recursion, such as a
cascading event.  Recusion occurs when a procedure calls itself
(often repeatedly).

Visual Basic itself uses the stack, even in a .EXE, so the practical
limit is lower than 20K.  Use code similar to the listing below to
test the amount of stack space available.  Each GoSub uses 4 bytes
of stack space, so it is easy to calculate the free stack space. 

 Sub Command1_Click ()
 'Calculates the amount of free stack space.
 'Used space = 20480 - free space.
   Dim Cnt As Integer
   Cnt = 0
 Overflow_Stack:
   Me.Cls
   Cnt = Cnt + 1
   Print "free", Cnt * 4
   GoSub Overflow_Stack
 End Sub

Procedure arguments and local variables in Sub and Function
procedures take up stack space at run time.  However, Global
variables, module-level variables, and procedure-level variables
declared with the STATIC keyword, and arguments in Sub or Function
procedures that are defined with the STATIC keyword, don't take up
stack space because they are allocated in the module data segments
of form and code modules.

Return to Phil's Web Page

Created by: Philip