












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













The College of Saint Rose Fall 2012
A data type is a collection of values and a set of predefined operations on those values. It is an abstraction , as the details of the representation are hidden from the user.
A descriptor is the collection of the attributes of a variable. We will elaborate on this idea later. These attributes are sometimes needed only at compile time (if all attributes are static) or may need to be maintained at run time.
An object represents an instance of a user-defined (abstract data) type.
An design issue for all data types: What operations are defined and how are they specified?
Primitive data types are provided by most languages. These are not defined in terms of other data types.
They are often exactly the kinds of data that the hardware supports, or very similar. This makes operations on these potentially very efficient.
The integer data types in our languages usually correspond directly to the sizes of the chunks of bits that the underlying hardware can operate on.
Java defines 4 sizes: byte: 8 bits, short: 16 bits, int: 32 bits, and long 64 bits. This is part of the Java language specification.
In some languages, like C, the size of various integer types can vary from implementation to implementation.
A 32-bit int has 232 = 4.3 billion possible values, while a 64-bit int has 264 = 1. 84 × 1019 possible values.
Bit and byte significance is also an important consideration. It is very important from an architec- ture perspective but can play a role in programming languages as well.
When placing the bits within a byte, they are almost always arranged with the most significant bit (msb) on the left, least significant bit (lsb) on the right. This nearly never comes into play with a programming language.
We follow the same idea for stringing together bytes to make up words, longwords, etc.
0 1 1 0 1 0 1 0 0 0 0 0 1 1 0 0 MSB LSB
The endianness refers to the the order in which we store these bytes in memory.
high memory
. . . LSB MSB . . .
low memory
high memory
. . . MSB LSB . . .
low memory
An architecture can use either ordering internally, so long as it is consistent. However, endianness becomes important when we think about exchanging data among machines (networks). Network byte ordering (big endian) is required of networked data for consistency when exchanging data among machines that may use different internal representations.
The MIPS architecture is bi-endian. It can process data with either big or little endianness.
See Example: /home/cs433/examples/show bytes
Integer values may be treated as signed or unsigned. For an n-bit integer, the unsigned representa- tion can store values 0 through 2 n^ − 1.
Signed representations require must have a way to represent negative numbers.
It turns out there are a number of reasonable or at least seemingly-reasonable options.
Excess N representations are used in some circumstances, but are fairly rare.
For non-negative x, we just use the unsigned representation of x. For negative x, use the bit-wise complement (flip each bit) of −x. Programming tip: the ∼ operator will do a bitwise complement in C and Java. Examples:
Problems:
- we have a -0. - we can compare within a sign, but otherwise need to check sign.
Range: -7 to +7. Like Excess N, 1’s complement is used in practice, but only in specific situations.
For non-negative x, use the unsigned representation of x. For negative x, use the complement of −x, then add 1 (that seems weird..).
Now, that’s useful. 0 and -0 have the same representation, so there’s really no -0.
Also, very useful. We can quickly recognize -1 as it’s the value with all 1 bits no matter how many bits are in our representation. Another useful feature: 1’s bit still determines odd/even (not true with 1’s complement)
Like 1’s complement, we can compare numbers with the same sign directly, otherwise we have to check the sign. Given these convenient properties, 2’s complement representations are the standard and de- fault unless there’s some specific situation that calls for another representation. Historical note: Fortran had an IF statement:
which performed the equivalent of the following (in a more familiar C/Java syntax):
What value is this? 12 + 18 + 321 + ...
x =. 1010 1 2
x =. 0101
x +
x = .1 = 1
x =
How about. 1100?
x =. 1100 1 4
x =. 0011
x +
x = 1
x =
How can we denote an arbitrary fractional value, say, 15?
We can follow this procedure:
So 15 =. 001100110011 ...
When representing these in the computer, we have lots of decisions to make, such as how we place the radix point, etc. We want to store a wide range of values, but we’re limited to 2 n^ unique values in any n-bit representation.
Scientific notation helps us here. Consider some examples:
Floating point = integer part + mantissa × 2 exponent
sign mantissa exponent
If we use binary version of scientific notation, we note that all numbers (other than 0) have a leading 1. So we need not store it! This is known as the phantom 1 bit.
The mantissa is fractional, with the most significant bit representing the 12 ’s bit, the next the 14 ’s bit, etc.
The exponent is stored in excess notation (which is helpful for hardware that must align fractional values before addition or subtraction).
What about 0? It would be nice if that was the all-0’s value. However, 00000000000000000000000000000000 really would represent something like 1. 0 × 2 −^127.
Trying to store something smaller than that value would result in a floating point underflow.
There are many standards, which can be hard to implement. These will include several useful and unusual values, such as +∞, −∞, NaN (not a number), etc.
Most modern computers use the IEEE Floating-Point Standard 754 format.
The exponent bias indciates than an excess format is used. The smallest possible exponent (- for single precision) is represented as a binary 1. Thus, we add 127 to the number we wish to represent. The special exponent 0 is reserved for subnormal numbers.
For example, consider the single-precision IEEE 754 representation of the number 51.625. The straight binary representation:
We normalize this to
So we need a sign bit of 0, an exponent of 5, represented as 132 in 8 bits: 10000100, and the mantissa of 10011101 (remember the phantom 1!).
Most modern languages provide primitive data types to represent characters.
Most programming languages allow programmers to use string data types as a representation of sequences of characters.
There are many design decisions that need to be made.
For static length strings, a descriptor is needed only at compile time to store the static string value, its length, and the starting address. For dynamic strings, we need a run-time descriptor which two lengths: the current length and the maximum length (as currently) allocated.
We will look at the support for strings in a set of representative languages.
- primitive type with assignment and several operations
An ordinal type has a range of possible values can be easily associated with the set of positive integers. ( e.g. , Java int, char, boolean).
With enumeration types , all possible values are provided in the definition.
C and Pascal were the first to use enums. A C example:
See Example: /home/cs433/examples/enum
As we can see with C, these are often implemented by integers.
Use of enumeration types improves readability, and can improve reliability in certain cases. We have essentially created a group of related named constants, and defined a datatype, instances of which are restricted to the available types.
There are a few design decisions that need to be made for a language that supports this:
If the enumeation types are not coerced, this can allow better error checking (as in Java, C#) and restriction of operations to those apprpriate for an enumeration rather than integers.
We can see the power of Java enum types by looking at the examples at Oracle:
On the web: Enum Types at Oracle’s Java Tutorials at http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
- run-time bindings for subranges and run-time allocation - fixed for the entire life of the array - e.g. , Ada arrays - flexibility advantage: need not know the array size until it is used - but still allocated on the stack
The array notation needs to be resolved to addresses and values to be used.
Two options:
Address(X[n]) = Address(X[0]) + (n - 1) * element_size
or
Address(X[n]) = Address(X[0]) element_size + (n * element_size)
In the latter, the first part can be done at compile time in certain situations (faster array access).
A compile-time descriptor for a single-dimensional array needs (most of):
We also need to distinguish between rectangular and jagged multidimensional arrays.
Additional array design issues:
int a[] = {3, 9, 18, 21};
e.g. , in Ada:
List : array (1..5) of Integer := (1 => 17, 3 => 34, others => 0);
’SANFRAN’,’San Francisco 49ers’, ’SEATTLE’,’Seattle Seahawks’, ’STLOUIS’,’St. Louis Rams’, ’TAMPABAY’,’Tampa Bay Buccaneers’, ’TENNESSEE’,’Tennessee Titans’, ’WASHINGTON’,’Washington Redskins’);
This defines mappings between a token form of a football team’s name with the team’s full name. Later in the program, it is used to translate the tokenized version (which is found in the program’s data files) to the full name:
printf("
Here, both $roadteam and $hometeam have been assigned values when reading a data file.
These can be very convenient to use.
Design issues include:
For Perl, in the example above, we could add a new team to the association with:
$team_array{’LOSANGELES’} = ’Los Angeles Californians’;
And if we wanted to remove an element:
delete($team_array{’DALLAS’});
Python, Ruby, and Lua are other languages with direct support for associative arrays.
A record is a (potentially) heterogeneous aggregate of data elements.
Examples include Pascal records, C/C++ structs, and C++ classes.
See Example: /home/cs433/examples/ratios
In Ada:
type Emp_Rec_Type is record First: String (1..20); Mid: String (1..10); Last: String (1..20); Hourly_Rate: Float; end record; Emp_Rec: Emp_Rec_Type;
Individual members of a record can be accessed in various ways
Record storage is typically in a block of contiguous memory locations.
The names, types, and offsets (from the start of the record) associated with each field need to be stored in a (likely compile-time) record descriptor.
Pascal examples:
On the web: Pascal Programming Lesson 11: Record Data Structure at http://pascal-programming.info/lesson11.php
Note the use of either the. notation or a with construct to access fields.
What other operations might be included?
A union type is similar to a record, but it can store different type values at different times.
Pascal provides this functionality with case variant records, C/C++ with the union types.
An example from C:
union nodeTypeStruct { DRUM_computingNode *compNode; DRUM_networkNode (^) *netNode; } type;
There are some dangers associated with allowing pointers in a programming language:
int *x = (int *)malloc(10 * sizeof(int)); // use x free(x);
// use of x here is a dangling reference
int *x, i; for (i=0; i<10; i++) { x = (int *)malloc(10 * sizeof(int)); // do other stuff? } // only the last allocated x is still accessible here
References are more restrictive, and hence safer. No taking arbitrary addresses or pointer arith- metic.
We have seen that languages sometimes require dynamically allocated memory to be explicity returned to the system ( e.g. , C’s free, C++’s delete), or have the system determine which memory is still in use and reclaim what is no longer used ( e.g. , Java).
The process used by Java (and many other languages) to reclaim memory is called garbage collec- tion.
Approaches:
Garbage collection has been an active area of research in programming language design for many years – we are just scratching the surface of the approaches and ways to do it more efficiently.