November 01, 2025, 01:26:03 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Array

Started by GJ, July 11, 2008, 11:53:37 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

GJ

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



Ionic Wind Support Team

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.
Ionic Wind Support Team

GJ

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

Mike Stefanik

July 11, 2008, 04:19:06 PM #3 Last Edit: July 11, 2008, 04:20:40 PM by Mike Stefanik
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.
Mike Stefanik
www.catalyst.com
Catalyst Development Corporation