Runtime errors
Some mistakes can only be found while the program runs: an array index out of
range, a pointer that is null. Compiled with -exx, a program checks for
them, and stops with a numbered runtime error that says what went wrong and
where, instead of reading memory it does not own.
#cmdline "-exx"
'' -exx checks every array index while the program runs.
dim scores( 0 to 2 ) as int32 = { 70, 85, 90 }
dim as int32 total = 0
for i as int32 = 0 to 3 '' one too far
total += scores( i )
next
print total '' never reached
Output
Aborting due to runtime error 6 (out of bounds array access) at line 7 of bounds.tiko::(),
'SCORES' accessed with invalid index = 3, must be between 0 and 2
The loop's last pass asks for scores( 3 ). The check stops the program
before the read: runtime error 6, with the line, the array's name, the index
and the bounds it should have been within. The program's exit status is the
error number, 6, so a script that runs it can tell what happened.
#cmdline "-exx" puts the option in the source, so the file alone reproduces
the result. On the command line it is tiko -exx bounds.tiko.
-exx adds the array-bounds and null-pointer checks, and records the
procedure each runtime error happens in, so the message can name it.
The null-pointer check is shown in nullptr. Without
-exx neither check is made: the read past the end returns whatever is in
memory there, and nothing reports it. The message itself goes to standard
error, not standard output.
Checks cost speed: a comparison on every array access and a test on every
pointer dereference. Use -exx while developing and testing, and decide
deliberately whether a release build keeps it.
Activities
- Remove
#cmdline "-exx"and run the program again. What does it print? - Change the loop to
for i as int32 = -1 to 2. Which index does the message report?