data types simple and compound abstraction and implementation
TRANSCRIPT
![Page 1: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/1.jpg)
Data Types
simple and compoundabstraction and implementation
![Page 2: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/2.jpg)
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
![Page 3: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/3.jpg)
Simple types
integer floating point binary-coded decimal
character boolean user-defined types
usually in hardware
usually in software
not composed of other types hardware or software implemented
![Page 4: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/4.jpg)
Integer
2’s complement unsigned operations exact within range range depends on size of virtual cell- typical size: 1, 2, 4, 8 bytes
![Page 5: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/5.jpg)
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)
1 11 52 bits
![Page 6: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/6.jpg)
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
4 4
5 9 0 5 1 8 7 8
defined decimal point
![Page 7: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/7.jpg)
Character
ASCII – 128 character set – 1 byte Unicode – 2 byte extension usually coded as unsigned integer
![Page 8: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/8.jpg)
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
![Page 9: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/9.jpg)
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:
![Page 10: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/10.jpg)
User-defined types
implemented as a restricted range of integer
subrange type: (Ada example)subtype CENTURY20 is INTEGER range 1900..1999;
BIRTHYEAR: CENTURY20;
BIRTHYEAR := 1981;
![Page 11: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/11.jpg)
User-defined types
Type compatibility issues:-can two enumerated types contain
same constant?-can defined types be coerced with
integer, with each other?
![Page 12: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/12.jpg)
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;
--> x type: intaddr: offset
namenametypetype addressaddress
![Page 13: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/13.jpg)
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
![Page 14: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/14.jpg)
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
![Page 15: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/15.jpg)
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 strLengthAddress
Dynamic strMaxLengthCurrLengthAddress
char*Address
In symbol table
![Page 16: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/16.jpg)
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)
![Page 17: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/17.jpg)
Arrays – design decisions
indexing:dimensions – limit? recursive?types – int, other, user defined?first index: 0, 1, variablerange checking – no(c),
yes(java)lexemes – ‘subscripts’ = (),[]?
![Page 18: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/18.jpg)
Arrays – design decisions binding times
type, index typeindex range(ie array size), space
staticfixed stack-dynamicstack-dynamicheap-dynamic
initial values of elementsat storage allocation? e.g. int[] x =
{1,2,3};
![Page 19: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/19.jpg)
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)
![Page 20: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/20.jpg)
Arrays – storage
<array>element type, size
index typeindex lower boundindex upper bound
address
address
lower bound upper bound
![Page 21: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/21.jpg)
Arrays – element access
<array>element type, size
index typeindex lower boundindex upper bound
address
address
lower bound i
address of a[i] =
address + (i-lower bound)*size
![Page 22: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/22.jpg)
Arrays - multidimensional
contiguous or not row major, column major order computed location of element
![Page 23: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/23.jpg)
Jagged arrays
Implemented as arrays of arrays<array>
<array>, 4index type
index lower boundindex upper bound
address
address
<array><array>
<array>, 3<array>, 3index type
index type
index lower boundindex lower bound
index upper boundindex upper bound
addressaddress
<array><array>
<array>, 7<array>, 7index type
index type
index lower boundindex lower bound
index upper boundindex upper bound
addressaddress
<array><array>
<array>, 4<array>, 4index type
index type
index lower boundindex lower bound
index upper boundindex upper bound
addressaddress
<array><array>
<array>, 5<array>, 5index type
index type
index lower boundindex lower bound
index upper boundindex upper bound
addressaddress
![Page 24: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/24.jpg)
(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
![Page 25: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/25.jpg)
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
![Page 26: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/26.jpg)
(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
![Page 27: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/27.jpg)
Records - implementation<array> a
element type, size
index type
index lower bound
index upper bound
address
address
lower bound upper bound
<record>dept
array [1..4] of char 0 (offset)
code
address
Caddress O S C 3127
dept course
integer4
type course =
record dept : array[1..4] of char; code : integer;
end
![Page 28: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/28.jpg)
(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
![Page 29: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/29.jpg)
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;
![Page 30: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/30.jpg)
(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) ...
1 1 0111 0
0 0 1100 1
0 0 0100 0
![Page 31: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/31.jpg)
(6) Pointers and references
references are dereferenced pointers (whatever that means)
primary purpose: dynamic memory access
secondary purpose: indirect addressing as in machine instructions
![Page 32: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/32.jpg)
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
![Page 33: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/33.jpg)
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
Dereferencing Field access
![Page 34: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/34.jpg)
Pointers hold addresses
Indirect addressingIn 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…
![Page 35: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/35.jpg)
Pointer arithmetic
Arithmetic operations on addresses
int x;
int *iptr;
iptr = &x;
for (;;){
<< process loc (*iptr)>>
iptr++;
}
Scan through memory starting at x
![Page 36: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/36.jpg)
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
![Page 37: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/37.jpg)
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
![Page 38: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/38.jpg)
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
![Page 39: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/39.jpg)
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’)
![Page 40: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/40.jpg)
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
![Page 41: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/41.jpg)
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
![Page 42: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/42.jpg)
Reference counting:
2p = null;
1
0q = null;
Reference count in cell
Count 0 -> return cell to heap
Classic problem:
circular linked lists
int *p = *q = 6;
![Page 43: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/43.jpg)
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
![Page 44: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/44.jpg)
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 }}
![Page 45: Data Types simple and compound abstraction and implementation](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bf7c1a28abf838c84198/html5/thumbnails/45.jpg)
Managing heap ofvariable-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?