Page 1 of 1

Why Do I Get Subscript Errors Here?

Posted: Wed May 04, 2016 5:25 am
by abrogard
I get subscript out of range errors in this code every time I run it with the array dimensioned for 20000.

Like : DIM spots(20000)

Why? I should be able to go to 32000 shouldn't I? And in fact it seems to go wrong anywhere above 2000.

Here's the code. Just trying to fill an array with unique x,y coords.


SCREEN 11



' FOR x = 1 TO 750
' y = x * .75


' PSET (x, y)

' NEXT x

CLS

DIM myvar AS LONG
DIM height AS LONG
DIM wide AS LONG

DIM spots(2, 2000) ' here is the problem. when i change this.

height = 600
wide = 440


myvar = height * wide


'init random


RANDOMIZE (223)

'set 12000 random points

FOR i = 1 TO 12000
x = INT(RND * 600)
y = INT(RND * 440)


' search for matches


match = 0

IF i > 4 THEN

FOR k = 1 TO i - 2

IF spots(1, k) = x THEN

IF spots(2, k) = y THEN

match = 1

EXIT FOR

END IF

END IF

NEXT k


END IF



IF match = 1 THEN

PRINT "match"; x; y
i = i - 1

ELSE

PSET (x, y)

' spots(1, i) = x
' spots(2, i) = y

PRINT i

END IF


NEXT i

PRINT i

'put 2000 spots in an array




' FOR i = 1 TO 2000
' x = INT(RND * 600)
' spots(1, i) = x
' NEXT i


' FOR i = 1 TO 2000
' y = INT(RND * 440)
' spots(2, i) = y
' NEXT i

' print out 20 from array

' FOR i = 1 TO 20

' PRINT "Spot", i, "x", spots(1, i)
' PRINT "Spot", i, "y", spots(2, i)


' NEXT i


' match found printout

' IF match = 1 THEN
' PRINT "match found"
' PRINT spots(1, i)
' PRINT spots(2, i)
' END IF

Re: Why Do I Get Subscript Errors Here?

Posted: Sat Aug 06, 2016 6:53 am
by EkriirkE
Max variable size is 64K (65534) bytes, or 32K (32766) elements as noted.
You aren't assigning `spots` a variable type, so it is 4-byte wide double I believe.
Plus that is a multi-dimensional array so in reality you are trying to allocate 60000 elements of 4 bytes each (2, 20000) = 3 * 20001. Thus if keeping the first dim at 2 you can only do ` spots(2,10921) AS INTEGER` for maximum elements and not one iota larger (10921 is 32766/3-1)
If left un-typed, or typed for long or double, you can only have 64K/4bytes/3dims-1=spots(2,5460) maximum.
If you just had a single dimension as in your intro text it works fine up to 16383 elements because 64K / 4 bytes...

Re: Why Do I Get Subscript Errors Here?

Posted: Sat Aug 06, 2016 10:58 am
by abrogard
Yep. Exactly so. Thank you.

:)

Re: Why Do I Get Subscript Errors Here?

Posted: Tue Nov 17, 2020 7:15 pm
by EkriirkE
Lewis-H wrote:
Tue Nov 17, 2020 2:26 pm
Irrelevant garbage
I think you're lost

Re: Why Do I Get Subscript Errors Here?

Posted: Wed Sep 29, 2021 10:28 am
by Peter Swinkels
Why? I should be able to go to 32000 shouldn't I? And in fact it seems to go wrong anywhere above 2000.
I believe you can't declare any array with the total number of bytes exceeding 64kb. It's not just the number of elements but the resulting size that matters. Since you didn't explicitly declare a data type QBasic defaults to the
single precision data type where each element uses 4 bytes. You might want to add "DEFINT A-Z" at the top of your code so that each variable defaults to a 2 byte integer unless otherwise specified. This reduces the amount of memory used and is faster when doing calculations.