Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Checked builds

The same source can be built to find bugs or to run fast. A checked build adds code that tests every array index and every pointer, and stops the program at the first bad one, with its location:

'' A checked build: -exx adds array-bounds and null-pointer checks,
'' and records where each error happens.
#cmdline "-exx"

function Total( scores( ) as int32, byval count as int32 ) as int32
    dim as int32 sum = 0
    for i as int32 = 1 to count
        sum += scores( i )
    next
    return sum
end function

dim scores( 1 to 3 ) as int32 = { 70, 85, 90 }
print Total( scores( ), 4 )        '' one too many

Output


Aborting due to runtime error 6 (out of bounds array access) at line 8 of checked.tiko::TOTAL(), 
'SCORES' accessed with invalid index = 4, must be between 1 and 3

The program exits with code 6, the number of the runtime error, and the message goes to standard error. It names the line, the procedure and the array, the index used and the bounds the array has, which is usually enough to see the bug without a debugger: the loop runs to count, and the caller passed one more than the array holds.

Without -exx, nothing checks the index. The same program reads whatever memory lies past the end of the array, adds it to the total, and prints a number as if nothing were wrong.

BuildOptionsUse it for
checked-exx, often with -gdevelopment and tests
release-O2 or -O3the program you ship

The checks cost time: every index is compared with both bounds, and every pointer with null. Test with the checks on and ship with them off, or, for a program where a wrong answer costs more than speed buys, ship checked. The parts of -exx are available one at a time as -earray, -enullptr and -elocation.

Arrays and Error Handling say more about the errors themselves.

Activities

  1. Remove the #cmdline line, build again, and run the program.
  2. Replace -exx with -earray and compare the message.