[Solved] Arrays with negative indexes not valid?

New to FreeBASIC? Post your questions here.
Luis Babboni
Posts: 303
Joined: Mar 15, 2015 12:41

[Solved] Arrays with negative indexes not valid?

Postby Luis Babboni » Feb 06, 2016 20:57

Hi,

I tried to dim an array A(1 to 8) and an array B(-8 to 8)
Could be possible that when I tried to fill B(-8) the value fall into array A overwriting the A´s values?
If is like this, is not possible to use negative indexes for arrays?

Thanks.
Last edited by Luis Babboni on Feb 06, 2016 22:34, edited 1 time in total.
fxm
Posts: 10037
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Arrays with negative indexes not valid?

Postby fxm » Feb 06, 2016 21:11

No problem to use negative indexes.
If you encounter a weird behavior, post your code please!
D.J.Peters
Posts: 8203
Joined: May 28, 2005 3:28
Contact:

Re: Arrays with negative indexes not valid?

Postby D.J.Peters » Feb 06, 2016 21:13

Something like this ?
(no problem here)

note: if you run in to problems with array indicies use

fbc -exx yourcode.bas

now FreeBASIC will check the array bounds
and you will get an array index out of bounds error.

Joshy

Code: Select all

dim as integer a( 0 to 16) = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17}
dim as integer b(-8 to  8) = {10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170}
var aStart = lbound(a), aEnd=ubound(a)
var bStart = lbound(b), bEnd=ubound(b)

for i as integer=aStart to aEnd
  print a(i),
next
print
for i as integer=bStart to bEnd
  print b(i),
next
sleep
Luis Babboni
Posts: 303
Joined: Mar 15, 2015 12:41

Re: Arrays with negative indexes not valid?

Postby Luis Babboni » Feb 06, 2016 21:34

It seems i found the mistake, sorry.

It was enough that you said that it must be possible to look harder and found the mistake.

I Shamed. :-(
jona
Posts: 35
Joined: Aug 28, 2014 6:44
Location: Puerto Princesa, Palawan, Philippines

Re: Arrays with negative indexes not valid?

Postby jona » Mar 11, 2016 12:36

D.J.Peters wrote:Something like this ?
(no problem here)

note: if you run in to problems with array indicies use

fbc -exx yourcode.bas

now FreeBASIC will check the array bounds
and you will get an array index out of bounds error.


How do I use "fbc -exx yourcode.bas"? Is it something I run inside the IDE?
MrSwiss
Posts: 3657
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: [Solved] Arrays with negative indexes not valid?

Postby MrSwiss » Mar 11, 2016 13:10

jona wrote:How do I use "fbc -exx yourcode.bas"? Is it something I run inside the IDE?

Depends on the IDE ...
Generally: it is the Command Line that calls FBC, whether it's done from a Console or out of an IDE, is irrelevant.
If you're doing it from an IDE, refer to the IDE's Documentation.
St_W
Posts: 1513
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: [Solved] Arrays with negative indexes not valid?

Postby St_W » Mar 11, 2016 17:40

MrSwiss wrote:
jona wrote:How do I use "fbc -exx yourcode.bas"? Is it something I run inside the IDE?

Depends on the IDE ...
Generally: it is the Command Line that calls FBC, whether it's done from a Console or out of an IDE, is irrelevant.
If you're doing it from an IDE, refer to the IDE's Documentation.

If you're using FBedit:
http://users.freebasic-portal.de/stw/fi ... FBedit.pdf
fxm
Posts: 10037
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [Solved] Arrays with negative indexes not valid?

Postby fxm » Mar 11, 2016 17:57

St_W wrote:
MrSwiss wrote:
jona wrote:How do I use "fbc -exx yourcode.bas"? Is it something I run inside the IDE?

Depends on the IDE ...
Generally: it is the Command Line that calls FBC, whether it's done from a Console or out of an IDE, is irrelevant.
If you're doing it from an IDE, refer to the IDE's Documentation.

If you're using FBedit:
http://users.freebasic-portal.de/stw/fi ... FBedit.pdf

For FBIde:
viewtopic.php?f=2&t=19950
jona
Posts: 35
Joined: Aug 28, 2014 6:44
Location: Puerto Princesa, Palawan, Philippines

Re: [Solved] Arrays with negative indexes not valid?

Postby jona » Mar 11, 2016 22:48

[quote="fxm"
For FBIde:
http://www.freebasic.net/forum/viewtopi ... =2&t=19950[/quote]
Thank you. I have a 0-127X0-63 matrix and I want to flip each of the constituent 0-7X0-7 matrices about their major axes. So the indices for the four-deep nested loops becomes a little complicated. I have always had a hard time with this sort of thing, so having my hand held is helpful. I assume that -exx slows down execution?
MrSwiss
Posts: 3657
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: [Solved] Arrays with negative indexes not valid?

Postby MrSwiss » Mar 12, 2016 8:45

jona wrote:I assume that -exx slows down execution?
Yes, it does, because:
-exx adds "checking" Code that tests for Zero Pointers and Array Bounds.
Therefore, as soon as the Code is *debugged* one should recompile without -exx.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 13 guests