Array Out-of-Bounds When It's Not

General FreeBASIC programming questions.
Post Reply
azbluevw
Posts: 16
Joined: Dec 31, 2011 21:54

Array Out-of-Bounds When It's Not

Post by azbluevw »

I have a sub that reads from a global array of a user-defined data type. I'm compiling with -exx and getting an array out-of-bounds error. It's hard to post the code causing the problem as it's almost 8000 lines of code; I haven't been able to replicate the problem with something smaller.

The function in question is something like this:

Code: Select all

sub buggy(i as long)
print i, ubound(c), lbound(c)

if c(i).a <> -1 then	'this is the line that gives an error
...
ubound/lbound show that I'm well within the array, and thus should not be getting out-of-bounds. Not only that, but some variations I've tried also set some high bits in the error code (I can't see anything that shows any valid error codes greater than about 17).

I don't expect anyone to be able to fix my problem with what I've posted thus far, but I was wondering if anyone has any guesses on what could be causing this or how I might go about finding what's going on.

It's worth noting I'm not making use of any pointers anywhere, so I don't think it's possible to have overwritten anything important that would end up causing strange random errors.
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Array Out-of-Bounds When It's Not

Post by sancho3 »

Is this 8000 lines of codes in 1 file or do you have multiple files included?
Are you sure the sub is seeing the global variable?
I would consider naming global variables something other than a, b, c, etc.

Check the address of c(1) (or 0 if that is the low bound) from where you know its valid and again where it is showing the error.

If this is Windows then utilize Sargs debugger to help you track down the bug.
azbluevw
Posts: 16
Joined: Dec 31, 2011 21:54

Re: Array Out-of-Bounds When It's Not

Post by azbluevw »

I am such a dork; I can't believe I started a thread over this.
sancho3 wrote:
I would consider naming global variables something other than a, b, c, etc.
That was just a simplified example; the actual function started out like this:

Code: Select all

sub reconcileJump(ifPtr as long)
    'print "------Reconciling-------"; ifPtr
And this was actually hiding my problem; the function was being called with -1 as its argument, but with it printing a bunch of dashes before the number, I didn't notice the negative number. Hence my claim I'm a dork for starting this thread.
sancho3 wrote:
Check the address of c(1) (or 0 if that is the low bound) from where you know its valid and again where it is showing the error.
Thank you for this; this is actually how I found out what I was doing! I tried printing the addresses of the array with 0, 1, and the argument. When it then started crashing before printing the address with the argument, I tried printing the argument just before that, and that's when I saw I had been calling it with -1.

I apologize for wasting this community's time; this was such a silly mistake.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array Out-of-Bounds When It's Not

Post by fxm »

Can you update the thread title (by editing it from your first message) to highlight that it was a fake problem?
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Array Out-of-Bounds When It's Not

Post by counting_pine »

It happens. It's always interesting to come across new ways of making bugs. Thanks for sharing the answer with us :)
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Array Out-of-Bounds When It's Not

Post by sancho3 »

printing a bunch of dashes before the number, I didn't notice the negative number
This is too funny.
Good for you for figuring it out.
Post Reply