
Data Types Data Types COSC337 • Evolution of Data Types: – FORTRAN I (1956) - INTEGER, REAL, arrays… – COBOL allowed users to define precision – ALGOL (1968) provided a few basic types, and by Matt Evett allowed user to form new aggregate types. – Ada (1983) - User can create a unique type for adopted from slides by Robert Sebesta every category of variables in the problem space and have the system enforce the types Descriptors Primitive Data Types: • Def: A descriptor is the collection of the • Primitive Data Types are those types not attributes of a variable defined in terms of other data types. – If all attributes are static, descriptors are required • Integer only at compilation time (usually stored within – Almost always an exact reflection of the hardware, compiler’s symbol table) so the mapping is trivial – If dynamic, this information must be stored in – There may be as many as eight different integer memory (Lisp does this via property lists) and types in a language used by run-time system. • Ex: int, long, char, byte • Data type is part of a descriptor. Floating Point Internal Representation of Floats • Model real numbers, but only approximately • IEEE Floating-Point Standard 754 • Languages for scientific use support at least sign bit two floating-point types 8 bits 23 bits • Usually the representation matches the exponent fraction hardware’s, but not always; some languages allow accuracy specs in code 11 bits 52 bits – e.g. (Ada) exponent fraction • type SPEED is digits 7 range 0.0..1000.0; • type VOLTAGE is delta 0.1 range -12.0..24.0; Decimal Boolean • Common in systems supporting financial applications • Could be implemented as bits, but often as • Store a fixed number of decimal digits (coded) bytes – Advantage: accuracy • Advantage: readability – Disadvantages: limited range, wastes memory Strings Examples of Strings • Pascal • Values are sequences of characters – Not primitive; assignm ent and comparison only (of • Design issues: packed arrays) – Is it a primitive type or just a special kind of array? • Ada, FORTRAN 77, FORTRAN 90 and – Is the length of objects static or dynamic? BASIC • Operations: – Somewhat primitive – Assignm ent – Assignm ent, comparison, catenation, substring – Comparison (=, >, etc.) reference – Catenation (or “concatenation”) – FORTRAN has an intrinsic for pattern matching – Substring reference – Ada provides catenation (N := N1 & N2 ) and – Pattern matching (find matching substrings, etc.) substrings (N(2..4)) Strings in C More String Examples •C • SNOBOL4 (a string manipulation language) – Not primitive – Primitive – Use char arrays and a library of functions that – Many operations, including elaborate pattern provide operations matching • C++ • Perl – Provides C-style strings – Patterns are defined in terms of regular expressions – Use of STL class provides “primitive” strings • A very powerful facility! Similar to Unix’s grep • e.g: /[A-Za-z][A-Za-z\d]+/ • Java – String class primitive String Length Encoding Utility (of string types): • String Length Options: • Aid to writability • Static - FORTRAN 77, Ada, COBOL • As a primitive type with static length, they are – e.g. (FORTRAN 90) inexpensive to provide--why not have them? CHARACTER (LEN = 15) NAME; • Dynamic length is nice, but is it worth the – NAME knows its own length expense? • Limited Dynamic Length - C and C++ actual – An additional pointer indirection. length is indicated implicitly by a null character delimiter • Dynamic - SNOBOL4, Perl, C++ and Java String classes Implementing Strings Ordinal Types • Static strings - compile-time descriptor • Def: ordinal type = range of possible values • Limited dynamic strings - may need a run-time can be easily associated with the set of positive descriptor for current & max length integers – But not in C and C++. Of course there’s no index – Enumeration checking provided! – Subrange • Dynamic strings - need run-time descriptor; allocation/deallocation is the biggest implementation problem Enumerations Enumerations in Languages • The user enumerates all of the possible values, • Pascal which are symbolic constants. – cannot reuse constants; they can be used for array – Design Issue: Should a symbolic constant be subscripts, for variables, case selectors; NO input allowed to be in more than one enumeration type? or output; can be compared. • No in C, C++, Pascal, because they’re implicitly • Predecessor and successor functions, loops converted into integers. •Ada • Ex (Pascal): – constants can be reused (overloaded literals); type WATER_TEMP = ( Frigid, Cold, Warm, Hot); disambiguate with context or type_name ‘ (one of var temp : WATER_TEMP; … them); can be used as in Pascal; CAN be input if temp > Warm ... and output Enumeration in Languages (2) Utility (of enumerations) • C and C++ • Aid to readability--e.g. no need to code a color – like Pascal, except they can be input and output as as a number integers • Aid to reliability--e.g. compiler can check • Java does not include an enumeration type operations and ranges of values – E.g. Don’t have to worry about bad indices: • int dailyHighTemp[MONDAY] vs. • int dailyHighTemp[1] Subrange Type Examples of Subrange Types • An ordinal type representing an ordered contiguous subsequence of another ordinal – Ada type • Subtypes are not new types, just constrained existing types (so they are compatible); can be used as in Pascal, plus case • Examples: constants – Pascal • Ex: • Subrange types behave as their parent types; can be used subtype POS_TYPE is INTEGER range 0 ..INTEGER'LAST; as for variables and array indices e.g. type pos = 0 .. MAXINT; Implementating user-defined Utility of subrange types: ordinal types • Aid to readability • Enumeration types are implemented as integers • Aid to reliability - restricted ranges add error • Subrange types are the parent types with code detection inserted (by the compiler) to restrict assignments to subrange variables Arrays Indexing • A homogeneous aggregate of data elements in • Def: mapping from indices to elements which an individual element is identified by its map(array_name, index_value_list) : an element position in the aggregate, relative to the first. • Syntax • Design Issues: – FORTRAN, PL/I, Ada use parentheses – What types are legal for subscripts? – Most others use brackets – Are subscripting expressions in element references range checked? – When are subscript ranges bound? • Subscript Types: – When does allocation take place? – FORTRAN, C - int only – What is the maximum number of subscripts? – Pascal - any ordinal type (int, boolean, char, enum) – Can array objects be initialized? – Ada - int or enum (includes boolean and char) – Are any kind of slices allowed? – Java - integer types only Categories of Arrays Static and Fixed Stack • Static • (based on subscript binding and binding to – range of subscripts and storage bindings are static storage) – e.g. FORTRAN 77, some arrays in Ada, static and – Static global C arrays – Fixed stack dynamic – Advantage: execution efficiency (no allocation or – Stack-dynamic deallocation) – Heap-dynamic • Fixed stack dynamic – range of subscripts is statically bound, but storage is bound at elaboration time – e.g. Pascal locals and, C locals that are not static – Advantage: space efficiency Stack-Dynamic Heap-dynamic • Subscript range and storage bindings are • Range and storage are dynamic, but fixed from dynamic and not fixed then on for the variable’s lifetime • e.g. (FORTRAN 90) • e.g. Ada declare blocks: INTEGER, ALLOCATABLE, ARRAY (:,:) :: MAT declare (Declares MAT to be a dynamic 2-dim array) ALLOCATE (MAT (10, NUMBER_OF_COLS)) STUFF : array (1..N) of FLOAT; (Allocates MAT to have 10 rows and begin NUMBER_OF_COLS columns) ... DEALLOCATE MAT end; (Deallocates MAT’s storage) • Advantage: flexibility - size need not be • APL & Perl: arrays grow and shrink as needed known until the array is about to be used • In Java, all arrays are objects (heap-dynamic) Subscripts Array Initialization • Usually just a list of values that are put in the • Number of subscripts array in the order in which the array elements – FORTRAN I allowed up to three are stored in memory – FORTRAN 77 allows up to seven • Examples: – C, C++, and Java allow just one, but elements can – FORTRAN - uses the DATA statement, or put the be arrays values in / ... / on the declaration – Others - no limit – C and C++ - put the values in braces; can let the compiler count them • e.g. int stuff [] = {2, 4, 6, 8}; – Ada - positions for the values can be specified SCORE : array (1..14, 1..2) := (1 => (24, 10), 2 => (10, 7), 3 =>(12, 30), others => (0, 0)); Slices Implementing Arrays • 1. FORTRAN 90 • - Access function maps subscript expressions to • INTEGER MAT (1 : 4, 1 : 4) • an address in the array • MAT(1 : 4, 1) - the first column • - Row major (by rows) or column major order (by • MAT(2, 1 : 4) - the second row • columns) • 2. Ada - single-dimensioned arrays only • LIST(4..10) Chapter 5 Associative Arrays • - An associative array is an unordered collection of • data elements that are indexed by an equal • number of values called keys • - Design Issues: • 1. What is th eform of references to elements? • 2. Is the size static or dynamic? Chapter 5 Chapter 5 Record Definition Syntax - Structure and Operations in Perl - COBOL uses level numbers to show nested - Names begin with % records; others use recursive definitions - Literals are delimited by parentheses e.g., Record Field References %hi_temps = ("Monday" => 77, 1. COBOL "Tuesday" => 79,…); field_name OF record_name_1 OF ..
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages9 Page
-
File Size-