Arrays

New to FreeBASIC? Post your questions here.
KenHorse
Posts: 56
Joined: Jan 27, 2012 0:08

Arrays

Post by KenHorse »

Consider the following code:

Code: Select all

Dim Buf(128) as Ubyte
Dim A As Byte

For A = 1 to 128
    Buf(A) = A
Next 
Obviously that will fill array Buf with Buf(1) 1, Buf(2) 2 and so on

Other languages allow us to see the entire array contents either by specifying the 1st index or none at all. For example:
Print Buf(1) will output
1
2
3
4
5
.......

or perhaps

Print Buf() will output the same

Doesn't seem to be an equivalent in FB?
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Arrays

Post by BasicCoder2 »

Interesting.
This works ok.

Code: Select all

Dim Buf(128) as Ubyte

For A as ubyte = 1 to 128
    Buf(A) = A
    Print Buf(A)
Next 

SLEEP
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Arrays

Post by MrSwiss »

KenHorse wrote:

Code: Select all

Dim Buf(1 To 128) as Ubyte ' corrected definition 
Dim A As UInteger ' fastest Loop counter

For A = 1 to 128
    Buf(A) = A
    Print Buf(A) ' or use another For Loop for print(ing)
Next
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Arrays

Post by BasicCoder2 »

byte value range -128 to +127
ubyte value range 0 to 255

Code: Select all

Dim Buf(0 to 128) as Ubyte
Dim A As uByte  'change to unsigned byte

For A = 1 to 128
    Buf(A) = A
    Print Buf(A)
Next 

Sleep
This one will keep looping,

Code: Select all

Dim Buf(0 to 128) as Ubyte
Dim A As Byte

For A = 1 to 127  'within value range of a byte
    Buf(A) = A
    Print Buf(A)
Next 

Sleep
KenHorse
Posts: 56
Joined: Jan 27, 2012 0:08

Re: Arrays

Post by KenHorse »

Thanks for the comments. I was hoping to simplify the code a bit but your ideas will work (of course)

One more question but I'll start a new thread about it
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Arrays

Post by fxm »

BasicCoder2 wrote:This one will keep looping,

Code: Select all

Dim Buf(0 to 128) as Ubyte
Dim A As Byte

For A = 1 to 127  'within value range of a byte
    Buf(A) = A
    Print Buf(A)
Next 

Sleep
This code ends by a run-time error when attempting to test the index +128 because converted to -128 (due to byte range), otherwise, as -128 is <+ 1 the for loop itself would continue indefinitely.

Code: Select all

Dim Buf(0 to 128) as Ubyte
Dim A As Byte

For A = 1 to 127  'within value range of a byte
    Print A,
    Buf(A) = A
    Print Buf(A)
Next

Sleep

Code: Select all

 120          120
 121          121
 122          122
 123          123
 124          124
 125          125
 126          126
 127          127
-128
Aborting due to runtime error 6 (out of bounds array access) at line 6 of D:\Use
rs\T0003830\Documents\Mes Outils Personnels\FBIde0.4.6r4_fbc1.06.0\FBIDETEMP.bas
::()
Last edited by fxm on Aug 04, 2017 5:37, edited 1 time in total.
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: Arrays

Post by sancho2 »

Since this is the beginners thread there are a couple of things I would like to say:

In your example you have defined the array

Code: Select all

Dim Buf(128) as Ubyte
This defines an array of 129 elements, 0 to 128. In your code, your loop skips element at index 0.
Freebasic allows you to better define an array stating both its lower bounds and its upper bounds as Mr Swiss shows you:

Code: Select all

Dim Buf(1 To 128) as Ubyte ' corrected definition 
The iterator variable A is a signed byte variable with a limit of 127. In binary 127 is 0111 1111. If you add 1 to 127 you get 128, binary 1000 0000. With a signed variable however the left most bit inicates the sign (1 for negative, 0 for positive), so instead of 128 you get -128 (2's compliment arithmatic).
So when the loop tests the iterator after 127, it sees -128 and continues on.
Fxm is using the -exx compiler option which adds array bounds checking and produces a runtime error.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Arrays

Post by fxm »

Yes, I repeat that for the Nth time:
I highly recommend during all testing phase of a program to always compile all sources with the -exx option.
RockTheSchock
Posts: 252
Joined: Mar 12, 2006 16:25

Re: Arrays

Post by RockTheSchock »

Allways use local integer/uinteger variable as index for small arrays. It should be safer and faster. You can also use lbound / ubound if array size is dynamic or if you change it later sometimes.

Code: Select all

Dim Buf(1 To 128) as Ubyte

For i As Integer= lbound(buf) to ubound(buf)
    Buf(i) = i
Next
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Arrays

Post by dodicat »

Using -exx is very simple with fbide, it is easy to freeze the console output. by setting cmd /c ""<$file>" <$param> & pause" in the run preference.

With fbedit a quick run (which would be the obvious choice for the test loop ) doesn't use any given flags, so would be ineffective.
With Poseidon, the flag -exx is accepted, but the console screen closes and the error is not seen.
Wxide seems the same as Poseidon.
I haven't tried winFBE yet.

If your fb.exe in on path (as perhaps with a Linux install) then -exx with a console/terminal compile would work OK I think.

Perhaps there is some way to tweak Poseidon and Wxide.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Arrays

Post by fxm »

Following my recent request:
- With poseidonFB, we can now check the box "Use Console Launcher To Run Program".
- With WinFBE, we can now check the box "Run compiled program using command window".
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Arrays

Post by dodicat »

Poseidon Ide
I see the console launcher, but I cannot see the box to enter the compiler flags anymore.

OK I see it now, right at the bottom.
Thank you.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Arrays

Post by MrSwiss »

dodicat, your information on FBEdit, is not correct ...
dodicat wrote:With fbedit a quick run (which would be the obvious choice for the test loop ) doesn't use any given flags, so would be ineffective.
FBEdit --> Options --> Debug / Quick Run - Options
you can preset:
first line (debug): $A\FBdebugger\FBdebugger.exe
second line (quick run): fbc -s console -exx
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Arrays

Post by fxm »

MrSwiss wrote:second line (quick run): fbc -s console -exx
Yes, but the IDE does not provide a "pause" to read the run-time error message!
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Arrays

Post by MrSwiss »

<sarcasm>fxm, just put a *Sleep* at the end (of code) and, your massive problem is solved</sarcasm>
Post Reply