Forum Index
Array base problem

 
Author Message
spamiam



Joined: 13 Nov 2005

Posted: 07 October 2008, 11:24 AM    Post subject: Array base problem

In the following code I was getting errors that looked like a wild pointer. When I manually specified the bases of the 2 arrays everything worked fine. If I let it use the default base (which should be 1, right?) then the subroutine would not work. It is acting as if the array base is not 1, maybe it is acting as if the array base is zero.

I searched all the files in the project and I never used "Option Base" at all.

Is this an issue with the compiler or do I have a programming bug somewhere?

-Tony

Code:
Private Sub RangerTask()

   Dim Range_Cmd(1 to 2) as byte
   Dim Readings(1 to 3) as byte

'this does not work:
'Dim Range_Cmd(2) as byte
'Dim Readings(3) as byte

   Dim Result as Integer
   Dim Reg as byte
   
   Do
   

   Range_Cmd(1) = CmdReg
   Range_Cmd(2) = RangeCmd
   Result = I2CCmd(1,SRF08_ADDRESS, 2, Range_Cmd,0,0)

   Call Delay(0.070)' 70mS wait for ranging to complete; 65ms min

   Reg = LdrReg
   Result = I2CCmd(1,SRF08_ADDRESS,1,Reg,3,Readings)   
   'Bytes: Light, Range_Hi, Range_Lo
   Light = Readings(1)
   Range = Cint(MakeWord(Readings(3), Readings(2)))
   Loop While (RangerEnable)

End Sub
Back to top
spamiam



Joined: 13 Nov 2005

Posted: 07 October 2008, 14:51 PM    Post subject:

I just compiled a code snippet and found that the array defaults to a zero base. If I specify the base in the definition of the array, then it will be correct. Also if I manually use "Option Base 1" then it works properly as well.

Isn't the compiler supposed to default to a base of 1?

Also, in the language reference, I think that the scope of Options is module wide, not a whole project. Is this true? If that is not explicitly stated, maybe it should be?

-Tony
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Location: Portland, OR

Posted: 07 October 2008, 15:33 PM    Post subject:

spamiam wrote:
Isn't the compiler supposed to default to a base of 1?
No, both VB6 and BasicX have a default array base of zero. The default is documented in the ZBasic manual.
Quote:
If no Option Base directive has been specified, the lower bound is zero.
I recommend always explicitly specifying both the lower and upper bound on array definitions. That way, there is no question as to the subscript range.

spamiam wrote:
I think that the scope of Options is module wide, not a whole project. Is this true? If that is not explicitly stated, maybe it should be?
The scope for Options varies. The Options Base, Explicit, AllocStr, Language, StringSize and Strict apply only to the module in which they appear. The remaining Options apply globally and may only appear in the first module compiled.

The discussion in the Options will be modified to make this more clear.


Last edited by dkinzer on 07 October 2008, 16:52 PM; edited 1 time in total
Back to top
spamiam



Joined: 13 Nov 2005

Posted: 07 October 2008, 16:03 PM    Post subject:

dkinzer wrote:
both VB6 and BasicX have a default array base of zero. The default is documented in the ZBasic manual.
Quote:
If no Option Base directive has been specified, the lower bound is zero.


Ah, it is amazing that I am only figuring this out now. For about forever, I have thought that Basic defaulted to 1-based arrays! My excuse is that I like to pass by reference, I usually specify "Option Base 1". Now I will try to be more precise by specifiying the lower bound at all times.

-Tony
Back to top
Display posts from previous:   
Page 1 of 1

 



ZBasic Microcontrollers Home
All content Copyright © 2005, 2006, 2007, 2008 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group