Shop OBEX P1 Docs P2 Docs Learn Events
Array problems — Parallax Forums

Array problems

LuckyLucky Posts: 98
edited 2010-03-27 23:07 in Propeller 1
Is it possible for one array of variables to write to another array of variables by accident if you go over the first array's size? For example, if you had myArray[noparse][[/noparse]45] and myArrayTwo[noparse][[/noparse]60], and you had a repeat statement that wrote numbers in myArray from index 0 to 49, would 45-49 numbers overflow into myArrayTwo?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"You do not really understand something unless you can explain it to your grandmother."


-Lucky[size=-1][/size]

Comments

  • localrogerlocalroger Posts: 3,452
    edited 2010-03-27 21:03
    Yes, Spin does not do array bound checking.

    It's more complicated than that though -- the PropTool also rearranges your vars to group them by type starting with all the longs, then all the words, then finally all the bytes to keep everything aligned in Hub RAM without waste. So if MyArray is type Word and MyArrayTwo is type Long, MyArray will step on some totally different data if you overflow it.

    This is annoying and totally not like most versions of BASIC, but it is like most C-based languages which are used for a lot of important work in the world.
  • LuckyLucky Posts: 98
    edited 2010-03-27 21:24
    Thanks, I was wondering if that was possible. I looked back at the method that was causing trouble and went through it for a few minutes, and found the problem. Those repeat and step statements can be tricky, especially when you have two different counter variables going on at the same time.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "You do not really understand something unless you can explain it to your grandmother."


    -Lucky[size=-1][/size]
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-03-27 21:31
    Localroger said...
    So if MyArray is type Word and MyArrayTwo is type Long, MyArray will step on some totally different data if you overflow it.
    Data is already bad ... but you can do even worse things ... how about overwriting the stack ... just a return value here and a return-address there ... return to nowhere or to places you definitely don't want it to return to ;o) ...
    What about overwriting SPIN or PASM code ... or the first 16 bytes of HUB-RAM containing the clock frequency and some important pointers ...

    You see ... anything can happen when you exceed the limit of an array positive or maybe with a negative index?! ... What can happen is unpredictable. The worst is switching input-pins to be an output. Depending on the hardware attached to those pins you can fry the prop and/or external hardware.

    ·
  • localrogerlocalroger Posts: 3,452
    edited 2010-03-27 23:07
    Yeah MagIO2, it's never a good idea to overrun an array, but I was just trying to gently make the point that you don't predictably stomp the next item in the VAR section if you do do it. Which I guess is your point too.
Sign In or Register to comment.