Shop OBEX P1 Docs P2 Docs Learn Events
I'm confused re: array indexing — Parallax Forums

I'm confused re: array indexing

Larry C.Larry C. Posts: 48
edited 2010-02-11 23:16 in Propeller 1
I'm working on a project where I have declared a data block:

VAR
word dblock[noparse][[/noparse]64]

which, I think, sets aside a group of 64 contiguous word-length memory slots that I can access as dblock[noparse][[/noparse]0] through dblock[noparse][[/noparse]63].

I just discovered that due to a typing error in my SPIN code, I have been writing and reading outside this block. For instance, dblock[noparse][[/noparse]109] ... dblock[noparse][[/noparse]128] seems to work OK (which is why I didn't spot it, I guess).

So, why does this work? What *does* happen when you try to access a data block outside it's declared index range? Can someone enlighten me on this?

THanks,

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-02-11 00:13
    Since the hub memory is byte addressable, the Spin interpreter multiplies the index by two and adds the address of the start of the dblock area. The result is used as an address to read and/or write a word in hub memory. Exactly what gets referenced depends on what the rest of your program looks like. You can get the Propeller Tool to show you where things go in memory, so you could figure this out. The Propeller tool does reorder the variables in a VAR section to put the longs first, words next, and bytes last
  • SRLMSRLM Posts: 5,045
    edited 2010-02-11 04:18
    Spin (and the hardware) doesn't do array bounds checking, so starting with index = 64 you just keep on going past the end of the where you want to be, and accessing whatever memory is there.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Powered by enthusiasm
  • Larry C.Larry C. Posts: 48
    edited 2010-02-11 18:36
    Thanks. That's just what I wanted to know.

    I guess the next logical question is: Why does the SPIN language provide for array size declarations, if it doesn't check that you stay within bounds?
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2010-02-11 18:51
    It is really only there so you can set aside blocks of RAM. They are only arrays in the loosest sense of the word.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!

    Some of my objects:
    MCP3X0X ADC Driver - Programmable Schmitt inputs, frequency reading, and more!
    Simple Propeller-based Database - Making life easier and more readable for all your EEPROM storage needs.
    String Manipulation Library - Don't allow strings to be the bane of the Propeller, bend them to your will!
    Fast Inter-Propeller Comm - Fast communication between two propellers (1.37MB/s @100MHz)!
  • Mike GreenMike Green Posts: 23,101
    edited 2010-02-11 18:56
    Why does C provide for array size declarations when it doesn't check that you stay within bounds? Why does C (and many other programming languages) provide for pointers when the use of a null pointer is one of the most common program errors.

    Array bounds checking is costly in terms of code size and execution speed. That's why, when it is available, it's optional. The same is true for pointer use. Not only is it costly to check for valid pointers, it's difficult to impossible to do. In many cases, the best that can be done is to check for an attempt to use a null pointer to reference something.
  • w8anw8an Posts: 176
    edited 2010-02-11 23:16
    This is exactly why Windows has so many thousands of patches.

    Steve
Sign In or Register to comment.