Array Question

New to FreeBASIC? Post your questions here.
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: ID

Array Question

Postby nimdays » Aug 27, 2014 23:03

Sorry if this a stupid question ,still learning here.
This will produce error with VB(Subscript out of range) , but not with FB
Can somebody expain why ?

Code: Select all

Dim mem(0) As Integer
Dim i As Integer
For i = 0 To 10
  mem(i) = i
Next
D.J.Peters
Posts: 8189
Joined: May 28, 2005 3:28
Contact:

Re: Array Question

Postby D.J.Peters » Aug 27, 2014 23:37

You can enable array bound checking with -exx

fbc -exx code.bas

Joshy
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Re: Array Question

Postby anonymous1337 » Aug 28, 2014 17:01

Isn't that at runtime only? Some command-line compilations didn't provide me any warnings, even with fbc -exx -w pedantic test.bas.
fxm
Posts: 9987
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array Question

Postby fxm » Aug 28, 2014 18:01

Yes.
When there is the '-exx' option, the compiler adds code to check the arrays bounds and the null-pointers during runtime.
At compile time, the out-of-bounds can be checked only for static arrays and constant values of indexes.
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: ID

Re: Array Question

Postby nimdays » Sep 17, 2014 21:57

Thanks All :)
Theunis Jansen
Posts: 248
Joined: Jul 01, 2010 9:35

Re: Array Question

Postby Theunis Jansen » Sep 18, 2014 5:57

perhaps it is in the DIM statement the (0) will dim it to one element? while FB will as QB regard it as the default of 12 elements. Only guessing
jona
Posts: 35
Joined: Aug 28, 2014 6:44
Location: Puerto Princesa, Palawan, Philippines

Re: Array Question

Postby jona » Sep 23, 2014 4:21

I thought I understood arrays, but why does this program spit out sixteen "correct" results even though only five memory locations are allocated?

#lang "fb"
Dim mem(1 to 5) As Integer
Dim i As Integer
i=lbound(mem)
print "low bound is ";i
i=ubound(mem)
print "high bound is ";i
For i = 0 To 15
mem(i) = 2*i
Next i
for i=0 to 15
print mem(i)
next i
sleep
end
integer
Posts: 391
Joined: Feb 01, 2007 16:54
Location: usa

Re: Array Question

Postby integer » Sep 23, 2014 7:17

jona wrote:I thought I understood arrays, ...


It is a memory allocation method.
I do not consider this a bug, but merely "dangerous" and sloppy coding:

Code: Select all

#lang "fb"

'' NOTICE THE ORDER IN WHICH THE ARRAYS ARE DEFINED
Dim as integer kim(10), mem(1 to 5), mim(10), i, j=20

FOR I=0 TO 10:mim(i)=100+i:kim(i)=200+i:next i

i=lbound(mem) : print  "mem()  low bound is ";i
i=ubound(mem) : print  "mem() high bound is ";i

'' this exceeds the defined array limits
For i = 0 To j : mem(i) = 2*i : Next i

print   " "

print   "kim(0 to 20)"
for i=0 to j: print  kim(i); :next i:print  " "
print   " "

print   "mem(0 to 20)"
for i=0 to j : print   mem(i); :next i : print  " "
print   " "

print   "mim(0 to 20)"
for i=0 to j: print  mim(i); :next i: print  " "

sleep
end


OUTPUT:

    mem() low bound is 1
    mem() high bound is 5

    kim(0 to 20)
    28 30 32 34 36 38 40 207 208 209 210 0 1245120 4199390 1 3356480 3352920 0 1-10000000 2

    mem(0 to 20)
    0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40

    mim(0 to 20)
    100 101 102 103 104 105 106 107 108 109 110 1244816 1244820 20 4 1 5 1 0 2 4

You'll notice that some elements of kim( 0 to 6 ) have been overwritten with mem( 0 to 6)
ALL of the elements you specified within the bounds defined have the correct values.
mim(0 to 10) has the initial values defined.

If you exceed the bound limits of the array you can do some really cool things -- one being to crash!
FreeBasic allows you to use your PC.

It is not a nanny looking over your shoulder ready to slap your wrist.
That is the purpose of the array bounds checking: -e -ex -exx

I really like this compiler.
fxm
Posts: 9987
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array Question

Postby fxm » Sep 23, 2014 7:42

More simply, swapping the two DIM lines of your initial program and you will see the resulting bug:

Code: Select all

#lang "fb"

Dim i As Integer
Dim mem(1 to 5) As Integer

i=lbound(mem)
print "low bound is ";i

i=ubound(mem)
print "high bound is ";i

For i = 0 To 15
  mem(i) = 2*i
Next i

for i=0 to 15
  print mem(i)
next i

sleep
end

Code: Select all

low bound is  1
high bound is  5
 0
 2
 4
 6
 8
 10
 6
 0
 1245120
 4199390
 1
 3361896
 3355304
 26
 28
 30

Remark: only the compile option -exx allows to check the array bounds at runtime.
jona
Posts: 35
Joined: Aug 28, 2014 6:44
Location: Puerto Princesa, Palawan, Philippines

Re: Array Question

Postby jona » Sep 28, 2014 10:45

fxm wrote:Remark: only the compile option -exx allows to check the array bounds at runtime.

Where is a discussion of compiler options: what they do and how to invoke them? I'm using FBIde.
fxm
Posts: 9987
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array Question

Postby fxm » Sep 28, 2014 11:04

jona
Posts: 35
Joined: Aug 28, 2014 6:44
Location: Puerto Princesa, Palawan, Philippines

Re: Array Question

Postby jona » Sep 28, 2014 11:36


How do I invoke them from FBIde?
fxm
Posts: 9987
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array Question

Postby fxm » Sep 28, 2014 12:55

jona
Posts: 35
Joined: Aug 28, 2014 6:44
Location: Puerto Princesa, Palawan, Philippines

Re: Array Question

Postby jona » Sep 28, 2014 22:30

fxm wrote:See in forum beginners:
Recommended settings for FBIde users


Thanks. I spent a good hour searching for this information before posting my follow up question. Normally all I need is to pointed in the right direction and then I can find my way.

A lot of this is mysterious to me, but I do understand that memory is laid out in a long line of addressed memory cells, at least that's how it is with SRAM and flash memory. Ie, nowhere in your memory is a neatly laid out array similar to a book shelf, with rows (the individual shelves) and columns (the position in a particular row). I once wrote a program using a sparse (along the main diagonal) symmetrical nXn array for a Commodore 64. In order to save space I stored only the upper diagonal, and left off the trailing cells in each row that held zeros, since those cells will remain zero after inversion. It saved on memory space and run time, but there was some small overhead to convert my i's and j's into a single pointer, but that was just integer arithmetic. BASIC must do simpler computations for arrays that are stored whole.

-eex can be deleted for the final compilation (is there ever a final compilation?) of a particular program, and then reinstated for new programs?

cmd /c "<$file>" <$param> & pause brought everything together.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 10 guests