I'm confused re: array indexing
Larry C.
Posts: 48
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,
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Powered by enthusiasm
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?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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)!
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.
Steve