Top Banner
Data Types
27

Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Data Types

Page 2: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Primitives

• Integer

• Float

• Character

• Boolean

• Pointers

Aggregates

• Strings

• Records

• Enumerated

• Arrays

• Objects

Page 3: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Strings

• Fixed length.

• Null terminated.

• Length field.

• Heap allocated.

B e t s y b b b

B e t s y 0

5 B e t s y

B e t s y

Page 4: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

String Allocation

• Static length.– Blank fill as in Fortran, Pascal, etc.

• Limited Dynamic length– grows to a limit

• Dynamic length– no length restriction– reallocates from heap

char x[4];strcpy(x,”abc”); //OKstrcat(x,”def”); // NO

x=“abc”;x=“abcdefghij”;

Page 5: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Implementation

• Can be viewed as primitive type– some machine language supports string

operations at a level which treats them as primitives even though operations are slower

• Sometimes requires both– compile-time descriptors– run-time descriptors– know the difference

Page 6: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Enumerated types

• Usually implemented as integers.

• Implied size limitation which is not a problem– (red, green, blue) red is 0, green is 1, etc

• Strong typing sometimes creates ambiguity– desire types to be distinguished but for

• weekday = (Mon, Tue, Wed, Thur, Fri);

• classday = (Mon, Wed, Fri)

– assignment ok one direction, but not other

• I/O sometimes allowed, others not

Page 7: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Subrange

• Sequence of an ordinal type– Mon..Fri

• Used for tighter restriction of values than primitive types provide– subtype age is integer 0..150;

• Sometimes compatible, others not– EXAMPLE: is age compatible with integer?– type age is new integer range 1..150; NO– type age is integer range 1..150; YES

Page 8: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Array Operations

• ARRAY operations are infrequent except APL

• Examples– elements (common)– entire array (as parameters/pointers)– slice (a row, column, or series of rows/columns)

• APL– matrix multiplcation– vector dot product– add a scalar to each element

Page 9: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Allocation strategies

• Static array

• Fixed stack-dynamic– int x[20]; compile-time decision of size allocation

• Stack dynamic– int x[n]; once allocated, size can’t change, but determined by n

• Heap dynamic– array can grow dynamically and change subscript– ever been frustrated by the MAX size of array?

Page 10: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Subscript/subrange errors

• Subscript bounds problems for arrays are one of our biggest programming nuisances

• Checking for them at run-time is expensive• Even if within the range -> no assurance they

are correct• Some languages such as c do NO checking• Consequence in programs is

difficult/impossible to trace

Page 11: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Addressing

• Storage is row-major or column-major order

(1,2)(1,1)

(2,2)(2,1)

(3,2)(3,1)

(1,2)

(1,1)

(2,2)

(2,1)

(3,2)

(3,1)

(1,2)

(1,1)

(2,2)

(2,1)

(3,2)

(3,1)

int A[2,3];

Page 12: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Determining location

Location (a[I]) = base address (a)+ (I- lowerbound)*element size

[1]

[2]

[3]

[4]

[5]

[6]

Assume size 4 bytes each starting at 100

100

104

108

112

116

120

integer a[6];

Loc(a[3])= 100 + (3-1)*4 = 108

Most of this is compile-time!

Page 13: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

2-d arrays (column major)

(1,2)

(1,1)

(2,2)

(2,1)

(3,2)

(3,1)

Loc (a[I,J]) = base address (a) (I-lb1)*size element + (J-lb2)*size of column

size of column=number rows allocated * size element

100

104

108

112

116

120

Loc (a[1,2]) = 100 + (1-1)*4 + (2-1)*3*4 = 100 + 0 + 12 = 112

Page 14: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Passing 2-d arrays as parameters

• The receiving procedure needs to have DIMENSION information

• Some languages are tightly bound and force that .. Pascal by requiring it to be a declared type

• Others have strange rules– Fortran (column major)

Caller:INTEGER A(10,20)CALL PROCESS(A,10)

Called:SUBROUTINE PROCESS(A,N)INTEGER A(N,1)

Page 15: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Associative arrays

• Not common… in perl

• Uses a hash function

• Stores Key and Value

cedric

perry

mary

gary 4785057000

5575075000

%salaries{“gary”} -> 47850

%salaries

hash“gary” 47850

In math class: hash(key) = value or hash(“gary”)=47850

Page 16: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Arrays as pointers in c

• Use of array name in c is the same as a pointer to the beginning element

• Incrementing the associated pointer increments by the true memory size– integers are 4 bytes– int * j;– j++; // increments j by 4.. assuming byte addressable

Page 17: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Example code in c

int c[10], *j;

for (j=c; j<&c[10]; j++)

{ *j = 0; }

Assign j to be the address of c[0]

As long as the addressof j is within the bounds of c

Increment j by size of integer

Set the element to 0

for (int j=0; j<10; j++) { c[j] = 0; }

Page 18: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Records

• Record operations– assignment– comparison– block operations without respect to fields

• Strange syntax in c

• Unions

Page 19: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Record pointers in c

Struct person{ int weight; int age; char name[20]; }; // not exact format

person teacher;

In declaring routine:

teacher.age=35;

When passing to functionand inside function:

teacher->age=35;

Page 20: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Unions

• Free unions– two names for the same place– it’s up to you to keep them straight– no support for checking

• Discriminated unions– a value in the record indicates how to interpret

the associated data.– Not always easy to check.. Sometimes not done

Page 21: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Ada example (p.231)

filled

color

Discriminant(form)

triangle:leftside, rightside, angle

circle:diameter

rectangle:side1,side2

Page 22: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Sets

• Bit fields implemented as binary values (below)

• fast implementation

• set operations are easy binary operations– try set union

• limit to size of set related to binary ops

Type colors = (red,blue,green,yellow,orange,white,black); colorset = set of colors;var set1 : colorset;set1 := [red,orange,blue];

implemented as ( 1 1 0 0 1 0 0 )

Page 23: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Pointers

• Lots of flexibility

• Data from heap

• Difficult to manage what you are pointing at

• Many languages strongly manage the types to which the pointers point– c doesn’t care– c++ does

• Real problems are programmer management

Page 24: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Pointer problems

Dangling reference:

int *p1, *p2;p1 = new (int);p2=p1;delete(p1);

p1

p2

Lost heap-dynamic:

int *p1, *p2;p1 = new (int);p1 = p2;

p1

p2

(lost)

Page 25: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Handling Pointer Problems

• Tombstones– always stays even after memory deallocated– never have a variable pointing at deallocated data

cell

Before

null cell

After

tombstone

Page 26: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Handling Pointer Problems

REFERENCE COUNTERS

cell 3

3 pointers at same cell

cell 2

2 pointers at same cell

Delete cell when reference count is 0

Other than efficiency, trick is with circular lists

Page 27: Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.

Handling Pointer Problems

GARBAGE COLLECTION

Initial scenario Mark all w/0 Mark all pointed at w/1

0

0

0

0

0

0

1

1

1

1