VB6 Help: Fixing "Out of Stack Space" Error

  • Thread starter Thread starter madmike159
  • Start date Start date
Click For Summary
The discussion centers on a programming challenge related to calculating factorials of large numbers in VB6, specifically encountering an "out of stack space" error when attempting to compute factorials for numbers greater than 200,000. The issue arises from using recursion, which consumes stack space with each subroutine call. A suggested solution involves eliminating recursion by using a large 2-dimensional array to manage bookkeeping for the calculations, although this approach may still require substantial memory. Another potential workaround mentioned is increasing the computer's RAM, but this may not significantly enhance performance. Ultimately, one participant resolved the issue by consolidating the code into a single subroutine, which improved functionality.
madmike159
Gold Member
Messages
369
Reaction score
0
VB6 Help out of stack"

I was writing a program to work out factorials of large numbers. The doubble varible over flows at 9E+308. I work out some of the factorial then move to another subroutine and divide by ten until it is < 10 then continue timising it by integers. In the end I am left with a decimal number < 10 and another number which represents the exponant.

If I try to find the factorial of a very large number (200000+) it gives me an error "out of stack space", which I think is because it creats a stack to keep track of when you move between subroutine. I use the Call command, does anyone know a different command or a way to stop it running out of stack space?

Thanks:biggrin:
 
Technology news on Phys.org


I assume your factorial is written using recursion (where a subroutine calls itself repeatedly). Each subroutine call adds all the local variables in your subroutine to the stack for each recursive call. So eventually there is not enough room in the computer's memory to stack anything more.

It is usually possible to solve "recursive" algorithms in a program without actually using recursion. To do so, you'll need a huge 2-dimensional array. One dimension holds all the bookkeeping you would normally store in the local variables for one recursive call; the other dimension is used for the successive steps in the recursive algorithm. It's not at all clear that you would be able to compute the factorial of larger numbers this way, since it will also consume a lot of memory. That's because the algorithm itself is going to repeat about a gazillion steps (that's more than a brazillion, by the way).

You might be able to do larger numbers by doubling the RAM (memory) in your computer. But you won't really get orders of magnitude improvement.
 


I moved it all into one subroutine and it works very well, thanks for the help.
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
10K
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
Replies
5
Views
3K
  • · Replies 18 ·
Replies
18
Views
6K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
4K