IonicWind Software

IWBasic => General Questions => Topic started by: GJ on July 11, 2008, 11:53:37 AM

Title: Array
Post by: GJ on July 11, 2008, 11:53:37 AM
Is there a way to determine if last item of array is reached ??
Following code did not gave me an error, i thought it dimensioned 2 items (a[0] and a[1])


'compile as console

def a[1]:STRING
def b,c:INT
def test:STRING
''test="just testing"
a="1","2","3","4","5","6","7","8"
''a[1]="1"
''A[6]="6"
test="just testing"

FOR b=0 TO 10
    print "b=",b, a[b]
NEXT b
print test

DO
UNTIL INKEY$<>""
END


Title: Re: Array
Post by: Ionic Wind Support Team on July 11, 2008, 01:08:48 PM
An array is just an area of memory and there aren't any checks for exceeding bounds at runtime.  The compiler just makes code and can't tell that your exceeding those bounds. 

Paul.
Title: Re: Array
Post by: GJ on July 11, 2008, 02:44:10 PM
Thanks Paul for explaining,  outside array can crash the program, i was searching for a UBOUND like/way of retreiving it.

def a[10]:string ===> UBOUND(a) ===> 11 elements maximum to assign to




'compile as console, code will crash as there can't be a b[3]
def a,b[2]:string
b[3]=string$(10,"2")
print b[3]
a=STRING$(10,"1")
print a


DO
UNTIL INKEY$<>""
END



I will code around this, no problem, just curious why  ;)

GJ
Title: Re: Array
Post by: Mike Stefanik on July 11, 2008, 04:19:06 PM
I believe that the way Paul allocates arrays is by using HeapAlloc (or something similar) and just hands you a chunk of memory based on the size of the array that you've dimensioned. Visual Basic uses SAFEARRAY structures for arrays which keeps track of things like the lower and upper bounds of the array and so on, and so runtime checking of the array bounds is possible; of course, it also imposes more overhead.

Bottom line, it's just like memory allocation with C/C++ in that you're responsible for making sure that you don't "draw outside the lines" so to speak.