Array index bug?

General FreeBASIC programming questions.
Phydaux
Posts: 11
Joined: Feb 21, 2006 18:40

Array index bug?

Postby Phydaux » Mar 11, 2007 16:18

Using FBC v0.17 (03-11-2007) for win32
Using a variable to access an array, the variable will bet set to 0 if it's 1 greater than the upper bound of the array.

The following will loop for ever

Code: Select all

Dim bar As Integer
Dim foo(1) As Integer

For bar = 0 To 2
    Print bar
    foo(bar) = 0
Next bar


If you use any other values > than the upper bound of the array index they're unaffected.

Code: Select all

Dim bar As Integer
Dim foo(1) As Integer

bar = 0
Print bar
foo(bar) = 0
Print bar
bar = 1
Print bar
foo(bar) = 0
Print bar
bar = 2
Print bar
foo(bar) = 0
Print bar
bar = 3
Print bar
foo(bar) = 0
Print bar
bar = 4
Print bar
foo(bar) = 0
Print bar
sleep

Produces the following output:

0
0
1
1
2
0
3
3
4
4

But... if you declare the index variable after the array var, the bug doesn't occur. :P

Loops 3 times only:

Code: Select all

Dim foo(1) As Integer
Dim bar As Integer

For bar = 0 To 2
    Print bar
    foo(bar) = 0
Next bar
VonGodric
Posts: 997
Joined: May 27, 2005 9:06
Location: London
Contact:

Postby VonGodric » Mar 11, 2007 16:30

you are going out of array bounds. compiler with -exx option to set warnings.
Pritchard
Posts: 5492
Joined: Sep 12, 2005 20:06
Location: Ohio, USA

Postby Pritchard » Mar 11, 2007 17:17

Using a variable to access an array, the variable will bet set to 0 if it's 1 greater than the upper bound of the array.
I'm surprised that you've managed to have anything set even greater than that. You're going out of bounds and trying to access memory you shouldn't be -_-;; If you compiled with -exx your programs would simply just stop at that point. It's not infinite loop. It's a freeze/crash you're experiencing.

As to why you're managing to set the array indexes above ubound + 1, I don't know O_O;; You could be setting uh, something else and that could be really bad :D FB doesn't have to work or break if you go out of bounds (above OR below), because you shouldn't be doing that. It catches these for you anyways and WILL NOT ALLOW IT!

Code: Select all

dim foo(1) as integer
print foo(2)
sleep


Code: Select all

C:/Documents and Settings/Owner/Desktop/bas.bas(2) error 81: Array out-of-bounds, found ')' in 'Print foo(2)'
Phydaux
Posts: 11
Joined: Feb 21, 2006 18:40

Postby Phydaux » Mar 11, 2007 18:38

VonGodric wrote:you are going out of array bounds. compiler with -exx option to set warnings.
Ya think! :P

I didn't think that the index var would be changed, even going out of bounds.

Pritchard wrote:FB doesn't have to work or break if you go out of bounds
That is a fair point.

I know what is happening now anyway. Going out of bounds is just pointing to another part of memory, and it just happened to be the same part of memory as the index var.

Clearly I should check the index var before passing it to the array. :P

I had assumed that Err would have a value (6?) if an array was accessed out of bounds using FB's default error handling.
I was being lazy with my code (just trying to parse a text file) and I was using err to try and catch reading from a file and going out of bounds in the same line of code.
Pritchard
Posts: 5492
Joined: Sep 12, 2005 20:06
Location: Ohio, USA

Postby Pritchard » Mar 11, 2007 19:22

Clearly I should check the index var before passing it to the array. :P
I think it's safer to just not allow your index to go out of bounds. Set a max and a min:

Code: Select all

Min = LBound(Array)
Max = UBound(Array)

'' change your index

index += 1

If Index > Max then Index = Min
That makes your code "handle things for you", in a sorts of way. The less you have to worry about the less manual checks you need and the overall code will be less/faster.

Now if you were loading something else's code or value, or making a library of sorts, it depends :P Your library will generally want to return an error if someone's trying to do something they can't, unless it's a very simple library meant to be very optimized. Checking for out of bounds access in these cases can actually speed things up though. If they go out of bounds, you exit the sub immediately and just don't execute any of the function's code. . .
Phydaux
Posts: 11
Joined: Feb 21, 2006 18:40

Postby Phydaux » Mar 11, 2007 19:52

I've been coding in VBA all week in work, and was stuck in that mind-set. VBA reports an out of bounds (out of range) in the Err object. And I wanted to catch it like that in my FB code. Like I said before, I was just being lazy and thought I stumbled across a bug. :)

Return to “General”

Who is online

Users browsing this forum: albert, angros47, MSN [Bot] and 25 guests