Forum Index
HomeZBasic Home   Forum RulesForum Rules   Forum FAQForum FAQ   MemberlistMemberlist   UsergroupsUsergroups   RSS FeedRSS Feed
Site SearchSite Search   LinksLinks   DownloadDownload   Digests and SubscriptionsDigests and Subscriptions
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in   RegisterRegister
Array base problem

 
Post new topic   Reply to topic    Forum Index -> ZBasic Native Mode
Author Message
spamiam



Joined: 13 Nov 2005
Posts: 661

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

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
Posts: 661

Posted: 07 October 2008, 14:51 PM    Post subject: Reply with quote

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
Posts: 2493
Location: Portland, OR

Posted: 07 October 2008, 15:33 PM    Post subject: Reply with quote

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
Posts: 661

Posted: 07 October 2008, 16:03 PM    Post subject: Reply with quote

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:   
Post new topic   Reply to topic    Forum Index -> ZBasic Native Mode Time synchro. with the server - Timezone/DST with your computer
Page 1 of 1

 


All content Copyright © 2005-2012 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