Docsity

Computer Science 433 Programming Languages Topic Notes, Study Guides, Projects, Research of Computer science

Toggle sidebar
bg1
Computer Science 433
Programming Languages
The College of Saint Rose
Fall 2012
Topic Notes: Data Types
Adata 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.
Adescriptor 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
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.
Integers
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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Computer Science 433 Programming Languages Topic Notes and more Study Guides, Projects, Research Computer science in PDF only on Docsity!

Computer Science 433

Programming Languages

The College of Saint Rose Fall 2012

Topic Notes: Data Types

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

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.

Integers

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.

  • little endian (x86)

high memory

. . . LSB MSB . . .

low memory

  • big endian (Sun Sparc, 68K, PPC, IP “network byte order”)

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.

  • Signed Magnitude The simplest way is to take one of our bits (usually the highest-order bit) and use it to indicate the sign: a 0 means positive, and a 1 means negative. With n bits, we can now represent numbers from −(2n−^1 − 1) to (2n−^1 − 1) Positive numbers just use the unsigned representation. Negative numbers use a 1 in the sign bit then store the magnitude of the value in the rest.

signed +N

value

unsigned value of representation

−N

Excess N representations are used in some circumstances, but are fairly rare.

  • 1’s complement

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.

signed

value

unsigned value of representation

Range: -7 to +7. Like Excess N, 1’s complement is used in practice, but only in specific situations.

  • 2’s complement

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)

signed

value

unsigned value of representation

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:

IF (I) GOTO 10,20,

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:

  1. Multiply by 2, write integer part.
  2. Keep fractional part, repeat until 0, or a repeating pattern emerges.

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:

.0001011 = 1. 011 × 2 −^4

.1 = 1. × 2 −^1

1 .1 = 1. 1 × 20

−101 = − 1. 01 × 22

1111 = 1. 111 × 23

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.

  • Single-precision (32 bits) - 1 sign bit, 8 bit exponent with bias, 23 bit mantissa
  • Double-precision (64 bits) - 1 sign bit, 11 bit exponent with bias, 52 bit mantissa

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

1. 10011101 × 25

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!).

  • Of historical interest: EBCDIC (Extended Binary Coded Decimal Interchange Code) devel- oped by IBM for punched cards in the early 1960s and IBM still uses it on mainframes today, as do some banking systems in some circumstances.
  • Unicode (1991), ASCII superset (for our purposes) – 2-byte characters to support interna- tional character sets

Most modern languages provide primitive data types to represent characters.

Strings

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.

  • Are they arrays of characters or a primitive type?
  • Are lengths static or dynamic?
  • If dynamic, are they counted or null-terminated?
  • What operations are supported directly?

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.

Language String Examples

We will look at the support for strings in a set of representative languages.

  • C and C++ - strings are not primitives - they are char arrays and a library of functions that provide operations in C or C++ - end of meaningful portion of allocated array indicated by a null (’\0’) character – means length is an O(n) operation - C++ provides a String class that encapsulates a C char array
  • SNOBOL4 (a string manipulation language) - strings are primitive types with many operations, including elaborate pattern matching
  • Fortran and Python

- primitive type with assignment and several operations

  • Java - primitive type (sort of) implemented in the String class - Strings are immutable - mutable strings are provided by the StringBuffer class
  • Perl, JavaScript, Ruby, PHP - primitives with built-in pattern matching, using regular expressions

Enumeration Types

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:

  • ss an enumeration constant allowed to appear in more than one type definition, and if so, how is the type of an occurrence of that constant checked?
  • are enumeration values coerced to integer?
  • can other types be coerced to an enumeration type?

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

Subranges

- 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

  • Fixed heap-dynamic - allocation occurs by user specification, not at elaboration time - memory comes from heap, not stack - e.g. C++ arrays created by new, Java arrays - similar to stack dynamic except allocated on the heap
  • Heap-dynamic - subscript ranges and memory allocation dynamic - arrays can grow or shrink during execution - very flexible, but more complex run-time - e.g. , Perl, JavaScript, Python, Ruby

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):

  • element type
  • index type
  • index lower bound (forced to 0 in C/C++/Java)
  • index upper bound
  • address

We also need to distinguish between rectangular and jagged multidimensional arrays.

  • Rectangular arrays have congruent rows and columns - e.g. , Fortran, Ada, C# - must distinguish row major (most languages) vs. column major linearization (Fortran)
  • Jagged arrays can have different length rows - essentially an array of arrays - e.g. , C++, Java

Additional array design issues:

  • What types are legal for subscripts? - Fortran, C: integer only - Ada: integer or enumeration (includes Boolean and char) - Java: integer types only
  • Are subscripting expressions in element references range checked? - C, C++, Perl, and Fortran do not specify range checking - Java, ML, C# specify range checking - In Ada, by default it requires range checking, but can be turned off
  • When are subscript ranges bound?
  • What is the maximum number of subscripts?
  • Can array objects be initialized? e.g. , in C, C++, Java, C#:

int a[] = {3, 9, 18, 21};

e.g. , in Ada:

List : array (1..5) of Integer := (1 => 17, 3 => 34, others => 0);

  • Is there support for heterogeneous arrays?

’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("$boldroad $team_array{$roadteam} $score $endboldroadat$boldhome $team_array{$hometeam} $score2 $endboldhome

${gametime} PM\n");

Here, both $roadteam and $hometeam have been assigned values when reading a data file.

These can be very convenient to use.

Design issues include:

  • What is the form of references to elements?
  • Is the size static or dynamic?
  • How can we access elements efficiently (likely answer: hashing)

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.

Records

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

  • Of keyword in COBOL -. operator in C/C++

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?

  • assignment is often supported if the types are identical (fieldwise copy)
  • Ada allows record comparison
  • COBOL provides MOVE CORRESPONDING, which copies a field of the source record to the corresponding field in the target record

Unions

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:

  • dangling reference – a pointer to memory no longer allocated for its previous purpose

int *x = (int *)malloc(10 * sizeof(int)); // use x free(x);

// use of x here is a dangling reference

  • memory leaks – allocate memory but never free it for reuse
  • lost heap-dynamic variables – an allocated heap-dynamic variable that is no longer accessible because we no longer are maintaining a pointer to that variable ( i.e. , it is garbage , and a memory leak if nothing is done about it)

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

  • casts to treat a pointer to one datatype as a pointer to another (as is allowed in C/C++)

References

  • C++ includes a special kind of pointer type called a reference type that is used primarily for formal parameters (more on this later)
  • Java reference variables replace pointers entirely – all are references to objects, rather than being addresses
  • C# includes both the references of Java and the pointers of C++

References are more restrictive, and hence safer. No taking arbitrary addresses or pointer arith- metic.

Dealing with Memory Leaks

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:

  • reference counters (eager approach) - maintain a counter for each allocation unit, if counter goes to 0, unit can be reclaimed - approach suffers from space/time considerations - circular lists lead to reference counting problems - process is incremental, so costs are spread over time
  • mark-sweep (lazy approach) - allocation continues until heap space becomes low - when a garbage collection is triggered ∗ mark all allocation units for removal ∗ follow all pointers, unmarking any reachable unit ∗ when no more pointers to trace, deallocate remaining marked allocation units - main disadvantage: it is an expensive process that causes the program to pause its execution for a significant time, though this can be reduced by running periodically not just when memory available becomes very low

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.