Notes 2:
Addendum to Essentials of Data Structures, Cpt 2

Math 61 -- D. C. Smolarski, S.J.
Santa Clara University, Department of Mathematics and Computer Science

[Return to Math 61 Homepage | Return to Notes Listing Page]

Contents


Symbol Table

(DCS 2.4)

To see a symbol table generated by the g++ compiler on the departmental HP server, "math," write a small program (call it test1.cxx for example) and compile it with the follow switches:

         math 21:g++ -g -S test1.cxx
This will create a file test1.s which consists of assembly language code and includes the symbol table information with the original variable names. Omitting the -g switch will generate the assembly language file but not include information which includes the original variable names.

To see a symbol table generated by compilers on SCU's DEC Alpha server, write a small program and compile it with the "switch" /LIST as follows:

For Fortran-90/95:

          [JDOE]>F90 TEST1/LIST
Then type (or print out) file TEST1.LIS to see the source code along with diagnostics which includes the "symbol table" of variables.

For C++, use the command:

          [JDOE]>CXX TEST1/LIST
instead.

Numeric Precision

Some languages have different data-types for the same general numeric type. C++ integers may be declared as short, int, or long. C++ real numbers may be declared as float or double. FORTRAN real numbers may be declared as REAL or DOUBLE PRECISION or, on some compilers, REAL*16.

Extended precision is commonly achieved by increasing the number of bytes used to store a single variable, and "bytes" rather than "words" is becoming the more standard measurement of size. Hence, character data is commonly stored in a single byte, and integers and reals in 4 bytes. Double precision reals are usually stored in 8 bytes.

One should know the actual (decimal) precision of these data types when using them, since merely requesting many decimal places when printing out values, does not mean that all the decimal places are accurate. This is seen in the sample C++ program at this link and the output at this link. (The code was run on the HP Unix server math, but similar answers were obtained on a Sun Sparcstation20 or the SCU DEC Alpha.)

Note that the values of x and y should be the same (since the same arithmetic expression was used to calculate both values), but since x is standard precision (float), the accuracy stops around the sixth decimal place, even though other numbers continue to be printed out.

Note that there is no industry wide standard or consistency among machines and compilers for the number of decimal digit accuracy that the standard types reflect. In other words, it is possible that single precision on a Cray, may easily correspond to double precision on a PC.

Extended Precision in Fortran-90/95:

To address the possible inconsistency and to permit a user to specify greater precision, Fortran-90/95 has introduced a data KIND attribute. The KIND function accepts an argument of one element of a specific data type, and returns an integer value, which often is the number of bytes used to store that data element. Hence,

    KIND('a')
may return the value of 1 and
    KIND(1)
may return the value of 4.

What is of greater use is the ability, in Fortran-90/95, to prescribe a new "kind" of numeric data having specific precision, and then to be able to declare variables of that new, extended precision, data type. This involves making use of the two new functions (which return integer values),

 
          SELECTED_REAL_KIND(i,j)
          SELECTED_INT_KIND(i)
In each case i indicates the number of digits accuracy desired, and j (in the case of reals) would indicate the maximum exponent desired. These would be used in defining parameters which, in turn, could be used to declare higher precision numeric variables. For example, to declare a real "kind" with 20 decimal digits accuracy, and exponent range between -350 and +350, and then declare variables of this precision, we would include the following at the beginning of the segment code:
  INTEGER, PARAMETER :: MYPREC20=SELECTED_REAL_KIND(20,350)
  REAL(KIND=MYPREC20)  :: X, Y, Z
Note also that the actual numeric value of MYPREC20 is irrelevant, since it is merely determined for immediate use to declare variables of that "flavor" of REAL. Nowhere else in the program code is the actual value of MYPREC20 ever used (or is it of any actual usefulness!).

Declaring Simple (Scalar) Variables

FORTRAN Variables need not be declared.

However, undeclared variables can be a source of endless problems.

If variables are not declared, they are given a "default type" based on the first letter of the identifier: if the initial letter is between I and N (inclusive), the variable is assumed to be of type INTEGER. Otherwise, it is of type REAL.

To turn off default typing, one includes before any declarations the following statement:

          IMPLICIT NONE
To explicitly declare variables (which is the recommended practice in FORTRAN now), one uses a type declaration statement as follows:

FORTRAN-77 Style

          REAL A, B
	  INTEGER I, J
	  COMPLEX X, Y
	  DOUBLE PRECISION W, Z
	  CHARACTER*20 NAME
	  LOGICAL TEST
Fortran-90/95 Style
          REAL    ::  A, B
	  INTEGER ::  I, J
	  CHARACTER (LEN=20) :: NAME
	  LOGICAL ::  TEST
In Fortran, all variables must be declared before the first executable statement in the program segment.

(For more information, follow this link.)

C++ Variables must be declared before use.

There are various lengths of both integer and real variables in C/C++. One can declare standard integers as int or as short or long. Real numbers may be declared as standard float or as double.

As in Fortran, the type comes first and the identifiers follow:

          int i, j;
	  float a, b;
	  double x, y;
	  char letter;
In C/C++ a char variable may only store a single character and, until recently, there was no type corresponding to LOGICAL (int variables can be used instead with the number 0 [for false] and 1 [for true]). In the latest version of C++, type bool has been introduced for boolean or logical variables (but not every PC compiler on campus recognizes this new type).

There is nothing corresponding, in the official C++ standard language, to COMPLEX.

In C/C++, new variable may be declared in any "block" and their duration is the block in which they are declared. The only restriction is that they are declared before they are actually used.

(For more information, follow this link.)


This page is maintained by Dennis C. Smolarski, S.J.
© Copyright 1998, 1999 Dennis C. Smolarski, SJ, All rights reserved.
Last changed: 26 March 1999.