PDA

View Full Version : Prop seems to use an array member that is not defined



John Kauffman
11-21-2010, 10:21 PM
I seem to be using an array member that does not exist.
This is a test - I am trying to figure out why this happens. I don't need a fix. I know I can fix it by changing the code.
I believe I am following the information of bottom of page 210

The sequence of events, as I understand them:
I create an array of MyNumbers[7] so the members are named MyNumbers[0] ... [6]
I create a variable with the number of elements ArraySize = 7
I use a loop to fill the array from MyNumbers[0] to MyNumbers[7]
The filling and reading of MyNumbers[7] seems to work, even though there should only be members named MyNumbers[0] to MyNumbers[6].

What is happening that SPIN / Prop does not choke on filling an array variable that (I think) does not exist?

Thanks.



CON
_CLKMODE = XTAL1 + PLL16X
_XINFREQ = 5_000_000

VAR
long MyNumbers[7] ' create array of longs: 7 members, numbered 0 to 6
long ArrayIndex ' used for loops - current index of MyNumber array
long ArraySize 'used for loops - number of members in array (=7)

OBJ
VgaText : "VGA_Text.spin"

PUB Start
ArraySize := 7
VgaText.Start(16)

''''''''''''''''''''''''
' * keep simple * seed array of 7 members with values 0...6, so index = value
repeat ArrayIndex from 0 to (ArraySize-1)
MyNumbers[ArrayIndex] := ArrayIndex

''''''''''''''''''''''''
'( debug display)
' next line expects to set up a failure
' by over-running number of members in array
' n.b. NOT using 'to ArraySize-1'
repeat ArrayIndex from 0 to ArraySize ' should try to fill a [7] which does not exist
MyNumbers[ArrayIndex] := ArrayIndex
' Above: ? On list iteration, how can a value be put in MyNumbers[7] that does not exist?
vgaText.dec(ArrayIndex)
vgaText.str(STRING(" holds "))
vgaText.dec(MyNumbers[ArrayIndex])
' Above: ? how can Prop return a value that is beyond range of array
vgaText.out(13)
WaitCnt(clkfreq*1/1 + cnt)

wjsteele
11-21-2010, 10:41 PM
You're asking for bounds checking, which doesn't exist in Spin.

In fact, you can even ask for something strange like MyArray[-1] and it will return the value stored in memory immediately before your array.

Bill

John Kauffman
11-21-2010, 10:59 PM
Thanks for the quick reply that completely explains it.
You mean I will have to rely on doing careful coding? Good grief, I'm in for trouble.

Duane Degn
11-22-2010, 01:27 AM
But this also allows lots of neat tricks.

For example, you only need to pass the address of one variable to an object and as long as you know what order your variables are in (in the top object) you can access them from a lower object.

Duane

stevenmess2004
11-22-2010, 09:40 AM
For example, you only need to pass the address of one variable to an object and as long as you know what order your variables are in (in the top object) you can access them from a lower object.

But you have to be very careful doing this because spin reorders some variables...

If you want this to work than make sure you have all your long variables first, than words and finally bytes.