Page 1 of 1

Array base problem

Posted: 07 October 2008, 4:24 AM
by spamiam
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: Select all

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

Posted: 07 October 2008, 7:51 AM
by spamiam
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

Posted: 07 October 2008, 8:33 AM
by dkinzer
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.
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.

Posted: 07 October 2008, 9:03 AM
by spamiam
dkinzer wrote:both VB6 and BasicX have a default array base of zero. The default is documented in the ZBasic manual.
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