Shop OBEX P1 Docs P2 Docs Learn Events
Prop seems to use an array member that is not defined — Parallax Forums

Prop seems to use an array member that is not defined

John KauffmanJohn Kauffman Posts: 653
edited 2010-11-22 01:40 in Propeller 1
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)

Comments

  • wjsteelewjsteele Posts: 697
    edited 2010-11-21 14:41
    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 KauffmanJohn Kauffman Posts: 653
    edited 2010-11-21 14:59
    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 DegnDuane Degn Posts: 10,588
    edited 2010-11-21 17:27
    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
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2010-11-22 01:40
    Duane Degn wrote: »
    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.
Sign In or Register to comment.