Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stack size limit for If/Then statements?
#1
Hi All,

I'm developing a program where I am implementing a lot of if/then statements to do logic, but the way it is designed, I will have to use a lot of straight logical statements. I have tried using this through DATA statements and arrays, dimension the arrays as $'DYNAMIC, etc...so I am just going the straightforward route.

A sample line for an IF / THEN statement is just this:

IF Lookahead$ = CHR$ (0) THEN G$="XXXXXXX": PUT #2 , ,G$ : G$=""

where the line has about 3 or less multi-statements, and only if they are true. Is is simple, but there are many of them. I am already using a logic tool to automate the process, that is why I cannot do it another way. The automation is simple and straightforward, at the expense of stack space because each of the statements are automatically generated and then can be used in QB64 code.
 
The kicker is, I have about 9,786 lines that I can use successfully - anything more, I will get a C++ error.

I think it has to do with the Main() sub being limited to approximately 10,000 lines or less (9,786 exactly), but am not entirely sure.

How can I increase the number of logic statements and conditionals that are outside of arrays? Is it just the stack space, or will I have to do it another way?

What are my options with this? I have tried using CLEAR, and that won't do either. Is it possible to somehow allocate a tremendous amount of stack size at the risk of very minimal variables? Or perhaps edit and recompile a modified version of Qb64Pe source code that will work for this increasing the line numbers and limits?

Thanks,
James
Reply
#2
Wow never knew main had limited amount of lines? I've seen programs with tons more lines than that.

Have you tried moving some of that If / Then work into subs or functions?
b = b + ...
Reply
#3
A quick and simple test shows that the main routine isn't limited to 10,000 lines or less of code.

Code: (Select All)
$Console:Only
Open "QB64_main_test.bas" For Output As #1

Do
    Cls
    Print "PROGRAM LAST SUCCESSFULLY COMPILED AT "; total; "LINES LONG"
    For i = 1 To 1000
        total = total + 1
        Print #1, "IF x = " + Str$(total) + " THEN END"
    Next
    Print #1, "PRINT " + Str$(total)
    Shell "qb64pe.exe -x QB64_main_test.bas" 'this should compile the program for us

Loop

I started that up and let it run for a bit, curious if I'd get that same glitch where it stops at less than 10,000 lines of code.

   

As you can see from the screenshot above, it compiled and ran over 15,000 lines with zero issues -- which is where I took the screenshot.   I let it keep going, and it went to over 30,000 lines and compiled just fine, before I stopped the process.

I don't know what the actual limits of lines might be inside the main module of a QB64 program, but I'm certain it's much more than 9,876 lines of code.
Reply
#4
Note that you *may* be running into the c++ limit if you're compiling with a 32-bit version of QB64PE.  The memory usage went up rather high in my tests, getting close to the 2GB memory mark, and if your lines are longer and more complex than mine, then I wouldn't be the least bit surprised if it isn't trying to use over 2GB of memory to compile -- and thus is erroring out when it hits the hard limit of a 32-bit program.

Does it do the same if you try and compile on a 64-bit version of QB64PE?  You may be hitting 32-bit limits and not any sort of limit in the language itself.
Reply
#5
@SMcNeill: I'm using the 64 bit version of Qb64pe on Ubuntu Linux, and I get a hard limit with the main sub that maxes out (trial and error) to about 9,786 lines of code before I get a C++ error. Even one line before this is fine, though. Any thoughts?

@BPlus: Yes indeed. 9,786 is the hard limit. And other than magic to increase stack size, there doesn't seem to be a practical way around it! The automation tool is rather crude, so any use of splitting the modules to or partitioning them to subs wont work. I'm trying to figure out a way, though. I really wish they would have a dynamic stack size. This has been an issues since 16 bit DOS, but shouldn't have these limits with 32 and 64 bit versions.
Reply
#6
All I can suggest is posting the code and letting others see if they run into the same glitch.  It's hard to diagnose an issue which one can't reproduce.  Are these nested IF statements?  Is that where the stack issue is coming from?

IF x then...
   if y then ...
     if z then ....

I have no idea how deep one could nest statements, before it hits some sort of stack limit on a person.
Reply
#7
It consists of one big loop, and the IF/THEN statements are single line expressions. Example:

LINE INPUT "File to read in:",FILE$
LINE INPUT "File to write out:",FILE2$

Open FILE$ For Binary As #1: 'For reading
Open FILE2$ For Output As #2: Close #2: Open FILE2$ For Binary As #2 ':For write

Do '(Main loop goes here)

IF Lookahead$=CHR$(0) THEN G$="(output)": PUT #2 , , G$: G$=""
IF Lookahead$=CHR$(1) THEN G$="(output)": PUT #2 , , G$: G$=""
...etc
Loop

-----------------------------------

^ The output is a type of automated script done by another program. The output is QB64 source code. The lines are simple, and there are no Gosubs, Gotos, or anything else. There are just a LOT of lines, because some of them are like this: 

IF Lookahead$=CHR$(0) + CHR$ (0) THEN G$="(output)": PUT #2 , , G$: G$=""
IF Lookahead$=CHR$(0) + CHR$ (1)  THEN G$="(output)": PUT #2 , , G$: G$=""

etc

So depending on the automated data, it could be between 256 or 65,536 or (heaven forbid) even 16,777,216 if we are tasked with up to 3 characters.

If I follow the automation through to completion, it will be about (give or take) 22,000 lines. But alas, I can only do this with 9,786 and it bombs out...

There are no other nested loops or anything. Just "If X = [statement] THEN [command 1] : [command 2]: [command 3] on a single line.
Reply
#8
No idea what's going on for you. 

This code produces a 65,535 line program set up as you describe:

Code: (Select All)
$Console:Only
Open "QB64_main_test.bas" For Output As #1

For x = 0 To 255
For y = 0 To 255
Print #1, "IF a$ = chr$( " + Str$(x) + ") + CHR$(" + Str$(y) + ") THEN g$ = " + Chr$(34) + "(output)" + Chr$(34) + ": PUT #2, ,g$: g$ = " + Chr$(34) + Chr$(34)
Next
Next

Shell "qb64pe.exe -x QB64_main_test.bas" 'this should compile the program for us

Compiles and runs with no issues on my system. (Even though it takes forever and ever and ever to compile.)

The generated program looks like the following:

If a$ = Chr$(0) + Chr$(0) Then g$ = "(output)": Put #2, , g$: g$ = ""
If a$ = Chr$(0) + Chr$(1) Then g$ = "(output)": Put #2, , g$: g$ = ""
If a$ = Chr$(0) + Chr$(2) Then g$ = "(output)": Put #2, , g$: g$ = ""
If a$ = Chr$(0) + Chr$(3) Then g$ = "(output)": Put #2, , g$: g$ = ""
If a$ = Chr$(0) + Chr$(4) Then g$ = "(output)": Put #2, , g$: g$ = ""
If a$ = Chr$(0) + Chr$(5) Then g$ = "(output)": Put #2, , g$: g$ = ""
If a$ = Chr$(0) + Chr$(6) Then g$ = "(output)": Put #2, , g$: g$ = ""
If a$ = Chr$(0) + Chr$(7) Then g$ = "(output)": Put #2, , g$: g$ = ""
If a$ = Chr$(0) + Chr$(8) Then g$ = "(output)": Put #2, , g$: g$ = ""
If a$ = Chr$(0) + Chr$(9) Then g$ = "(output)": Put #2, , g$: g$ = ""
If a$ = Chr$(0) + Chr$(10) Then g$ = "(output)": Put #2, , g$: g$ = ""
If a$ = Chr$(0) + Chr$(11) Then g$ = "(output)": Put #2, , g$: g$ = ""
If a$ = Chr$(0) + Chr$(12) Then g$ = "(output)": Put #2, , g$: g$ = ""
If a$ = Chr$(0) + Chr$(13) Then g$ = "(output)": Put #2, , g$: g$ = ""
If a$ = Chr$(0) + Chr$(14) Then g$ = "(output)": Put #2, , g$: g$ = ""
If a$ = Chr$(0) + Chr$(15) Then g$ = "(output)": Put #2, , g$: g$ = ""
If a$ = Chr$(0) + Chr$(16) Then g$ = "(output)": Put #2, , g$: g$ = ""
If a$ = Chr$(0) + Chr$(17) Then g$ = "(output)": Put #2, , g$: g$ = ""
If a$ = Chr$(0) + Chr$(18) Then g$ = "(output)": Put #2, , g$: g$ = ""

From the double loops, this checks for CHR$(0) + CHR$(0) to CHR$(255) + CHR$(255), which makes for 65,536 lines of code -- which compiles and runs with no isses on my system.

What exactly is the c++ compiler error that you're getting? And which version of QB64PE are you running?
Reply
#9
One thing to note:  Compiling 65,536 lines of code (as the above program generates) requires about 30GB of ram for cpp to do its job.  If you're thinking of doing 16million lines of code in such a manner, I hope you've got a LOT of memory available and LOTS and LOTS and LOTS of time.

Seems like your program needs some serious restructuring.  


SELECT CASE ASC(Lookahead$, 1)
    CASE 0
        g$ = "XXXXXXX"
        SELECT CASE ASC(Lookahead$,2)
        CASE 0
              g$ = "YYYYYYY"
              SELECT CASE ASC(Lookahead$, 3)
                    CASE 0: g$ = "ZZZZZZZ"
                    CASE 1: g$ = "AAAAAAA"
                    CASE 2: g$ = "BBBBBBB"
                END SELECT
         CASE 1

         END SELECT
  CASE 1
END SELECT
Reply
#10
@James - This is most likely a RAM issue. The RAM requirements have already been mentioned above.
Reply




Users browsing this thread: 7 Guest(s)