No bounds check

Questions about the BASICtools and MakeItC
Post Reply
basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

No bounds check

Post by basicchip »

>from help line
>One gets unexpected results if one does a string assignment of a short string from a longer string, vis:

Code: Select all

dim astring(12) as string
dim bstring(24) as string
 
bstring="Any string longer...1234"
astring=bstring
 
>astring shows up as 24 characters, in this example, but bstring remains intact (not knowing how they are allocated, it's not reasonable
>to assume they're in contiguous memory)

>1)Does the BASIC kernel automatically re-allocate memory to astring?
>2)I would have expected either a run-time error or truncation. Am I wrong?
>3) does this cause a memory leak that might overwrite other variables?

There are NO runtime bounds checks on arrays or strings, few compilers insert that code. ARMbasic is a compiler and not an interpreter which many of those interpreters do add bounds checks.

So the bug in your code will show up as unexpected results, and it does not cause any kind of a memory leak. Variables are allocated at compile time and their location does not change during the code execution. This also includes SUB/FUNCTION parameters and variables which also have fixed allocation in the "heap".

Variables are allocated as encountered, INTEGER and SINGLE from the bottom, and ARRAYs and STRINGs from the top of the available space. Right above that area is the STRING accumulator and above that is the stack.



Post Reply