Errors in the Usage of Variables

| SimCon Home | fpt Reference Manual Home |

Classes of Usage Errors

The classes of usage error are:

  1. Variables read before they are assigned a value
  2. Values written to variables which are never used
  3. Variables which are declared but never used

Note that the first category includes uninitialised variables, even if they are assigned values later in the program code, and the second category includes values which are unused, even if the variable itself is used elsewhere.

Detection by fpt

The fpt command to detect errors in usage is:

CHECK USAGE

This command checks for:

  1. Variables read but never assigned a value
  2. Variables which are assigned a value but are never used
  3. Variables which are declared but never used

Note the difference between these categories and those above. The CHECK USAGE command does not take program flow into account.

The output from the CHECK USAGE command for the computer game TREK7 is shown below:

Symbol Usage Check ================== Objects which are read but never written to ------------------------------------------- Name Scope Use/COMMON Address Type Size Value/Bounds ---- ----- ---------- ------- ---- ---- ------------ idevz pike,mordor /g/ 80 INTEGER *4 df idriz pike,mordor /g/ 84 INTEGER *4 df iitvl1 spock Local INTEGER *4 df imaxz pike,mordor /g/ 88 INTEGER *4 df ------------------------------------------------------------------------------- !!! Symbol Usage Check !!! !!! Total number of objects read but never written to (Errors): 4 ------------------------------------------------------------------------------- Objects which are written to but never read ------------------------------------------- Name Scope Use/COMMON Address Type Size Value/Bounds ---- ----- ---------- ------- ---- ---- ------------ irunz blish,knuth,niven,atack,kzin,fizbin,harpo,info,godot,main /q/ 76 INTEGER *4 df rtzro moe Local REAL *4 df rtzto spock Local REAL *4 df ------------------------------------------------------------------------------- !!! Symbol Usage Check !!! !!! Total number of objects written to but never read (Warnings): 3 ------------------------------------------------------------------------------- Objects which are declared but never used ----------------------------------------- Name Scope Use/COMMON Address Type Size Value/Bounds ---- ----- ---------- ------- ---- ---- ------------ itvl1 alpha Local INTEGER *4 df rtzro spock Local REAL *4 df ------------------------------------------------------------------------------- !!! Symbol Usage Check !!! !!! Total number of objects declared but never used (Notes): 2 -------------------------------------------------------------------------------

Analysis of Symbol Lives

fpt also analyses the "lives" of variables. A symbol life starts at the beginning of a routine, where it is either initialised by a DATA specification, or is in an uninitialised state, or at a statement where it is assigned a value. If two different lives can reach the same right-hand-side use of the variable they are equivalent. If two different lives can never reach the same right-hand-side uses they are independent. The fpt command:

SHOW LIVES

lists the lives of all variables. If any life is reported that:

"**** Uninitialised state may be read ****"

then the variable can be read before it has been assigned.

If any life is shown to have 0 read accesses, the value is never used.

Part of the symbol life listing from TREK7 is shown below:

Symbol Lives ============ ------------------------------------------------------------------------------- Symbol: a ========= Name Scope Use/COMMON Address Type Size Value/Bounds ---- ----- ---------- ------- ---- ---- ------------ a timex Local INTEGER *4 df Lives ===== ------------------------------------------------------------------------------- : : Life: 4 ------- Assignments: 1 Read accesses: 0 Units: Undetermined Assignments: ------------ Line: 1255, File: ... ecrypt1/Fortran_codes/Trek7/original_source/trekc.for IF (nomad .EQ. 23)a = a+0.2 ------------------------------------------------------------------------------- : : ------------------------------------------------------------------------------- Symbol: angle ============= Name Scope Use/COMMON Address Type Size Value/Bounds ---- ----- ---------- ------- ---- ---- ------------ angle boom,fizbin,spock,bloch,chekov,cancel,main /h/ 0 REAL *4 df (1:4) Lives ===== Life: 1 (Lives not analysed) --------------------------------- Assignments: 5 **** Uninitialised state may be read **** Read accesses: 4 Units: Undetermined Assignments: ------------ Line: 146, File: ... ecrypt1/Fortran_codes/Trek7/original_source/trekb.for angle(it) = a Line: 150, File: ... ecrypt1/Fortran_codes/Trek7/original_source/trekb.for 77 READ (l,*,ERR = 130)angle(it) Line: 157, File: ... ecrypt1/Fortran_codes/Trek7/original_source/trekb.for 130 angle(it) = distp Line: 463, File: ... ecrypt1/Fortran_codes/Trek7/original_source/trekc.for angle(it) = angle(it)*3.14159265/180.0 Line: 471, File: ... ecrypt1/Fortran_codes/Trek7/original_source/trekc.for angle(it) = angle(it)*180.0/3.14159265 **** Uninitialised state **** -------------------------------------------------------------------------------

Note that life 4 of a is never used and that life 1 (The only life) of angle can be read before it is assigned.

At present the life listing is long and cumbersome. Work is in train to extract the cases where errors are suspected.

Copyright ©1995 to 2017 Software Validation Ltd. All rights reserved.