Data Types simple and compound abstraction and implementation.

Slides:



Advertisements
Similar presentations
Names and Bindings.
Advertisements

Chapter 7:: Data Types Programming Language Pragmatics
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
Chapter 9 Imperative and object-oriented languages 1.
CS 355 – Programming Languages
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
ISBN Lecture 06 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Lecture 06 Topics Introduction Primitive Data.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
ISBN Chapter 6 Data Types: Structured types.
G. Levine Chapter 6 Chapter 6 Encapsulation –Why do we want encapsulation? Programmer friendliness- programmer need not know about these details –Easier.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Definitions A data type defines a collection of data.
Copyright © 1995 by Addison-Wesley Publishing Co. 1 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case sensitive?
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Elementary Data Types Scalar Data Types Numerical Data Types Other
Primitive Data Types: Numbers Strings Ordinal Types Pointers
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.
CS 330 Programming Languages 10 / 23 / 2008 Instructor: Michael Eckmann.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.
College of Computer Science and Engineering
ISBN 0-321— Chapter 6 Data Types. Corrected and improved by Assoc. Prof. Zeki Bayram, EMU, North Cyprus. Original Copyright © 2007 Addison-Wesley.
ISBN 0-321— Chapter 6 sections 1-4, 9 Primitive Data Types Numbers Strings Ordinal Types Pointers.
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
1 Data Types In Text: Chapter 5. 2 Chapter 5: Data Types Outline What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Basic Semantics Associating meaning with language entities.
ISBN 0-321— Chapter 6 Structured Data Types Arrays Associated Arrays Records Unions.
1 Intro to Data Structures and ADTs Chapter 2. 2 Goal of Data Structures Organize data Facilitate efficient … –storage –retrieval –manipulation Select.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
ISBN Chapter 6 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Chapter 6 Topics Introduction Primitive Data.
CS 330 Programming Languages 10 / 30 / 2007 Instructor: Michael Eckmann.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
C H A P T E R S I X Data Types.
CS 363 Comparative Programming Languages Data Types.
ISBN Chapter 6 Data Types Pointer Types Reference Types Memory Management.
1 CS Programming Languages Class 08 September 19, 2000.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved. 6-2 Chapter 6 Topics Introduction Primitive Data Types.
1 Data Types Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types Union Types Pointer Types.
Data Types W E E K F O U R. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character String.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
Chapter 6 © 2002 by Addison Wesley Longman, Inc Introduction - Evolution of Data Types: FORTRAN I (1957) - INTEGER, REAL, arrays … Ada (1983) -
CS 330 Programming Languages 11 / 01 / 2007 Instructor: Michael Eckmann.
Programming Languages Pragmatics Data Types. Simple types integer floating pointbinary-coded decimal character boolean user-defined types usually in hardware.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.2 Primitive Data Types Almost all programming languages.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 5 Evolution of Data Types: FORTRAN I (1956) - INTEGER, REAL, arrays … Ada (1983) - User can.
Names, Scope, and Bindings Programming Languages and Paradigms.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Data Types In Text: Chapter 6.
Chapter 6 – Data Types CSCE 343.
Chapter 6 Data Types.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
6.1 Introduction 6.2 Primitive Data Types - Evolution of Data Types:
Chapter 6: Data Types Lectures # 10.
Chapter 6 Data Types.
Complex Data Types One very important measure of the “goodness” of a PL is the capability of its data types to model the problem space variables Design.
Introduction to Abstract Data Types
Chapter 6 Data Types.
Intro to Data Structures and ADTs
Presentation transcript:

Data Types simple and compound abstraction and implementation

A brief history  simple types  arrays - compounds of same type  strings  records – compounds of different types  pointers and references  user defined types  abstract data types  objects

Simple types integer floating pointbinary-coded decimal character boolean user-defined types usually in hardware usually in software  not composed of other types  hardware or software implemented

Integer  2’s complement  unsigned  operations exact within range  range depends on size of virtual cell - typical size: 1, 2, 4, 8 bytes

Floating Point  based on scientific notation  representations and operations are approximate  range and precision depend on size of virtual cell (usually 4 or 8 bytes) bits

Binary Coded Decimal  ‘exact’ decimal arithmetic  decimal digits in 4 bit code  range and precision depend on size of virtual cell – 2 digits per byte defined decimal point

Character  ASCII – 128 character set – 1 byte  Unicode – 2 byte extension  usually coded as unsigned integer

Boolean  1 bit is sufficient but...  no bit-wise addressability in hardware  store in a byte – space inefficient  store 8 per byte – execution inefficient  c: 0=false, non-zero=true

User-defined types  implemented (like character and boolean usually are) as a coding of unsigned integer  enumerated type: (Pascal example) type suit = (club, diamond, heart, spade); var lead: suit; lead := heart; internally represented as { 0, 1, 2, 3 } operations:

User-defined types  implemented as a restricted range of integer  subrange type: (Ada example) subtype CENTURY20 is INTEGER range ; BIRTHYEAR: CENTURY20; BIRTHYEAR := 1981;

User-defined types  Type compatibility issues: -can two enumerated types contain same constant? -can defined types be coerced with integer, with each other?

Memory management intro  The parser creates a symbol table of identifiers including variables: Some information, name plus more, is bound at this time and as the program is compiled by storage in symbol table: e.g. int x; --> xtype: int addr: offset name type address

Strings  First use: output formatting only  Quasi-primitive type in most languages (not just arrays of character) - operations: initialization, substring, catenation, comparison  The length problem: fixed or varying?  No standard string model

c char *s = “abc”; int len = strlen(s); array of char with terminal: extended syntax library of methods Strings - examples JAVA String s = “abc”+x; s = s.substring(0,2); fixed length array extended syntax class with 70 methods a b c 0

Strings - representations  fixed length and content (static)  fixed length and varying content (FORTRAN)  varying length and content by reallocation (java String)  varying length and content by extension (java StringBuffer)  Varying length and content(c) Static str Length Address Dynamic str MaxLength CurrLength Address char* Address In symbol table

Compound (1) Arrays  collection of elements of one type  access to individual elements is computed at execution time by position, O(1), or O(dim)

Arrays – design decisions  indexing: dimensions – limit? recursive? types – int, other, user defined? first index: 0, 1, variable range checking – no(c), yes(java) lexemes – ‘subscripts’ = (),[]?

Arrays – design decisions  binding times type, index type index range(ie array size), space static fixed stack-dynamic stack-dynamic heap-dynamic initial values of elements at storage allocation? e.g. int[] x = {1,2,3};

Arrays – operations  on elements – based on type  on entire array as variables - - vector and matrix operations e.g.,APL - sub array (~ substring)  subarray dimensions(slices)

Arrays – storage element type, size index type index lower bound index upper bound address lower bound upper bound

Arrays – element access element type, size index type index lower bound index upper bound address lower bound i address of a[i] = address + (i-lower bound)*size

Arrays - multidimensional  contiguous or not  row major, column major order  computed location of element

Jagged arrays  Implemented as arrays of arrays, 4 index type index lower bound index upper bound address, 3 index type index lower bound index upper bound address, 7 index type index lower bound index upper bound address, 4 index type index lower bound index upper bound address, 5 index type index lower bound index upper bound address

(2) Associative Arrays - maps  values accessed by keys,not indices  no order of elements  automatic growth of capacity  operations: add/set, get, remove  fast search for individual data  slower for batch processing than array  Java classes; Perl data structure

Associative Arrays - implementation  hash tables based on key value  most operations ‘near O(1)’  expanding capacity may be O(n) For a java class that combines features of array and associative array, see LinkedHashMap

(3) Records  multiple elements of any type  elements accessed by field name  design issues: -hierarchical definition (records within records) -syntax of naming -scopes for elliptical (incomplete) reference to fields

Records - implementation a element type, size index type index lower bound index upper bound address lower bound upper bound dept array [1..4] of char 0 (offset) code address C OSC3127 dept course integer 4 type course = record dept : array[1..4] of char; code : integer; end

(4) Pascal variant records (unions) type coord = (polar, cart); point = record case rep : coord of polar: ( radians : boolean; radius : real; angle : real); cart: ( x : real; y : real); end; Note: varying space requirements discriminant field is optional (rep) type checking loopholes: Ada has similar variant record but closed these loopholes

Other unions  Fortran EQUIVALENCE  c union  not inside records  no type checking * unions do not cause type coercion - data is reinterpreted Sebesta’s c example union flextype { int intE1; float floatE1; } union flexType ell; float x; ell.intE1 = 27; x = ell.floatE1; Sebesta’s c example union flextype { int intE1; float floatE1; } union flexType ell; float x; ell.intE1 = 27; x = ell.floatE1;

(5) Sets (Pascal)  defined on one (discrete) base type  implementation imposes maximum size ( set of integer;- not possible) type day = (M, Tu, W, Th, F, Sa, Su); dayset = set of day; var work, wknd : dayset; today : day; today = F; work = [M, Tu, W, Th, F]; wknd = [Sa, Su, F]; if (today in work and wknd)

(6) Pointers and references  references are dereferenced pointers (whatever that means)  primary purpose: dynamic memory access  secondary purpose: indirect addressing as in machine instructions

Pointers (and references)  data type that stores an address in the format of the machine (usually 4 bytes) or a “null”  a pointer must be dereferenced to get the data at the address it contains  a reference is a pointer data type that is automatically dereferenced

Dereferencing example In c++: double x,y; Point p(0.0,0.0); Point *pref; pref = &p; x = p.X; y = (*pref).Y; In Java: Point2D.Double p; p = new Point2D.Double(0.0,0.0); double xCoord = p.x; Dereferencing and field access combined DereferencingField access

Pointers hold addresses  Indirect addressing In c: pointer to statically allocated memory int a,b; int *iptr, *jptr; a = 100; iptr = &a; jptr = iptr; b = *jptr; int x, y, arr[4]; int *iptr; iptr = arr; arr[2] = 33; x = iptr[2]; y = *(iptr + 2);  Security loophole…

Pointer arithmetic  Arithmetic operations on addresses int x; int *iptr; iptr = &x; for (;;){ > iptr++; } Scan through memory starting at x

Basic dynamic memory management model:  Heap manager keeps list of available memory cells  “Allocate” operation transfers cell from list in heap to program  “Deallocate” transfers cell from program back to list in heap  Tradeoffs of fixed or variable sized cells

Problems with pointers and dynamic memory:1  Dangling reference: pointer points to de-allocated memory Point *q; Point *p = new Point(0,0); q = p; delete p; // q is dangling - reference to q should cause // an error - ‘tombstones’ will do error check

Problems with pointers and dynamic memory: 2  Memory leakage: memory cell with no reference to it Point *p = new Point(0,0); p = new Point(3,4); // memory containing Point(0,0) object // is inaccessible - counting references will help

Cause of reference problems  Multiple references to a memory cell  Deallocation of memory cells  Where is responsibility? -automatic deallocation (garbage collection) OR -user responsibility (explicit ‘delete’)

User management of memory  Dangling references can be detected as errors but not prevented - tombstones - lock and key  Memory leakage is a continuing problem int *p =*q = 6; p = null; int *p =*q = 6; p = null; p 6 q p 6 q

Garbage Collection 1.Reference counting:ongoing “eager” -memory cells returned to heap as soon as all references removed. 2.Garbage collection:occasional “lazy” -let unreferenced memory cells ‘leak’ till heap is nearly empty then collect them

Reference counting: 2 p = null; 1 0 q = null; Reference count in cell Count 0 -> return cell to heap Classic problem: circular linked lists int *p = *q = 6;

Garbage Collection: (mark-sweep) 1. All cells in memory marked inaccessible(f) 2. Follow all references in program and mark cells accessible(t); f t t ‘Accessible’ marker in cell 3. Return inaccessible cells to heap f t t Classic problem: effect on program performance

A sloppy java example  from Main (Data Structures) public class ObjectStack { private Object[] data; private int manyItems;.... public Object pop() { if (manyItems==0) throw new EmptyStackException(); return data[--manyItems]; //leaves reference in data }

Managing heap of variable-sized cells  Necessary for objects with different space requirements  Problem: tracking cell size  Problem: heap defragmentation - keep blocks list in size order? - keep blocks list in sequence order?