Вы находитесь на странице: 1из 50

Data Types

Ng Eu Jinn 1071112650 Click to edit Master subtitle style Tan Chin Tong 1081106274 Ng Cong Jie 1071117296 Mostafa Abedi 1091108989 Tan Joanne 1071115484

5/4/12

6.2 Primitive Data Types


6.2.1 Numeric Types
Integer
Common integral data types

Bit 8 16 32

Name

byte short int and long

Decimal Digits 3 5 10

Store negative integers

Twos complement
5/4/12

6.2 Primitive Data Types


6.2.1 Numeric Types
Floating Point
Represented as fractions and exponents Types
float double

Complex
ordered pairs of floating point values

Decimal
store fixed number of decimal digits with the

5/4/12 decimal point at a fixed position in the value.

6.2 Primitive Data Types


6.2.2 Boolean Types
true or false represented by a single bit(cant access

efficiently)
stored in the smallest efficiently addressable

cell of memory

(1 byte)

6.2.3 Character Types


stored as numeric codings (computer) ASCII > UCSS-2 >UCS-4
5/4/12

6.3 Character String Types


String operations
Assignment &Comparison
complicated(string operands of different lengths)

Catenation Substring reference(slices)


reference to a substring of a given string

Pattern matching
supported directly in the language or provided

library
5/4/12

6.3 Character String Types


String Length Options
Static length strings Limited dynamic length strings
varying length up to a declared and fixed maximum

set by the variables definition


store any number of character between 0 and

maximum

Dynamic length strings


varying length with no maximum overhead of dynamic storage allocation and

deallocation (maximum flexibility)

5/4/12

6.4 User-Defined Ordinal Types


range of possible value can be associated

with the set of positive integers


Enumeration

all of the possible values which are named constants

are provided in the definition

Sub range
contiguous subsequence of an ordinal type

5/4/12

6.5 Array Types


Homogeneous Reference aggregate name & subscript

e.g. arrayName [10]


Element type & Subscript type Subscript range checking Subscript lower binding

5/4/12

6.5.2 Arrays and Indices


Ada :
e.g. name (10) : Array? Element? Ordinal Type subscript : e.g. day(Monday)

Perl
Sign @ for array : e.g. @list Sign e.g.$list[1] +ve $ for scalars : 2 0 1 3 4 subscript subscript Negative Arrays 0 1 2 3 4 Element -ve -5 -4 -3 -2 -1

5/4/12

6.5.3 Array Categories


Cannot change size Array Binding Binding Storage Advanta Disadva Categoryto to allocatio ges ntages Bound subscriptstorage n range Static Static Static Stack (before (before runtime) runtime) Efficient Bound in entire executio n Space Allocatio efficient n &dealloc ation 5/4/12 time

Fixed Static stackChangeable dynamic

During Stack declarati on elaborati on

6.5.4 Array Initialization


Langua Example ge Fortran Integer, Dimension(3) :: List = (/0, 5, 5/) 95 C int list [ ] = {4, 5, 6, 83}; C, C++ char name [ ] = freddie; char *name [ ] = (Bob, Jake, Darcie); //pointer Java String[ ] names Python List Comprehensions of= [Bob, Jake, Darcie]; Syntax : List : array (1..5) of Integer := (1, 3, 4, 7,if [expression for iterate_varin array Ada condition 8); ] Example4,Bunch 8, array (1..5) of Integer :=% 3=> 0 [ x : 9, [0, 1, 2, 3, 5, :6, 7, * xfor xin range (12) if x (1 == [0, 9, 5/4/12 ] 11, 12] 17, 3 => 34, others => 0); 36, 81, 10, 144] [0, 3, 6, 9,

6.5.5 Array Operations


C -> through Java, C++ and C# Perl -> Array assignments, comparisons Ada -> Array assignments, catenation,

equality and inequality operators


Python -> Array catenation, membership,

comparisons

Tuples : immutable and use parentheses

Fortran 95 -> Elemental operations Array-Processing Language (APL)


5/4/12

Rectangular & Jagged Array


Rectangular Array Jagged Array

Fortran, Ada, C# myArray [3, 7]

C, C++, Java, C# myArray [3] [7]

5/4/12

6.5.7 Slices
Python:
mat 0 1 2 0 1 4 7 1 2 5 8 2 3 6 9

mat [1] mat [0] [0 : 2]

-> [4, 5, 6] -> [1 ,2]


4 5 6 7 8

vect 0 1 2 3 or 0 2 6 vector [0 4: 8 : 2] 8

10->12 ,6, [2 14

16 17 10, 14]

5/4/12

6.5.9 Implementation of Array Types


Array :

Column Major Order Order 6 2 5 34 7 34 7 6 2 5 1 3 86 2 5 1 3 8 1 3 8 3, 4, 7, 6, 2, 5, 3 46, 1, 4, 2, 3, a 1 2 3, 5 6 7 8 9 1, 3,1 8 7, 5, 8 location(a [i, j]) = 2 adress of a[1, 1] + (( 3 ((# of rows above ith row) 4 * (size of row)) + 5 X (# of columns left of column)) location (a Adress jth 6 9 5 5/4/12 4 ) x [5, 6]) = a[1, 1] + ( ( x + element

3 4 Row Major 7

Associative Arrays
An unordered collection of data elements

that are indexed by an equal number of values called keys. value) Value Key
Eujinn 70 Marry 99 Joanne 55 Johnny 41

Each element is a pair of entities (key ,

=> Dataset[Eujinn] = 70; => Dataset[Marry] = 99; => Dataset[Joanne] = 55; => Dataset[Johnny] = 41; 5/4/12

Associative Array : Structure


Elements are stored and retrieved with

hash functions
Key John

Hashe 61409aa1fd d Key 47d4a Hashed Value Key 61409aa 70 1f Perl => Key must be string 8a31409 99 PHP => Key either integer or string aa.. Ruby => Key can be any object af115391 55 5/4/12 a.. PHP => Both array and associative array HASH FUNCTIO N

Associative Array : Operation


Assignments
$salaries{Perry} = 58850; salaries[Perry] = 58850;

//In Perl //In C++ //In Perl

Delete
delete $salaries{Gary}; salaries.erase(Gary);

//In C++ //In Perl


5/4/12

Exists
If(exists $salaries{Shelly}) If( salaries.find(Shelly))

//In C++

Record Type
Is used to group collection of data with

different (heterogeneous) data-type.

Eg: Struct in C, C++ and C#

struct Employee {
string name; int age; float salary;

};
5/4/12

Record Type : Structure


Fields of records are stored in adjacent

memory locations

Access by offset instead of indices Reference to fields by using identifiers.


printf(%s , employee.name); Identifi Type Size

off set

ers price type

doubl 64 e bit char 8 bit 32 bit

Rec quantit int ord y

Inde TypeSize x 0 int 32 Differ bit ent int 32 Size 1 bit Arr ay 5/4/12 32 2 int

Sa me Siz e

Record Type: Operations


Initialization
Aggregate literals Employee e = {John, 28, 3000.0f};

Assignments Comparison MOVE CORRESPONDING


In COBOL, copies all the field from source record to

the destination field record with the same name.


MOVE CORRESPONDING INPUT-RECORD TO

OUTPUT-RECORD

5/4/12

Union Type
A type whose variable may store different

type values at different times during program execution


In C / C++ => union In Fortran => Equivalence

Specifying Union

5/4/12

Union Type : Design Issue


Type Checking one of the major design

issue

2 ways of implementations
Free Union
Programmers are allowed complete freedom from

type checking in their use

Use in C, C++, Fortran

Discriminated Union
Type indicator(tag) is used for type checking Use in ALGOL 68 , ADA

5/4/12

Union Type : Implementation


Implemented by using the same address

for every variant

Storage is allocated to largest variant

11101 00000 00000 00000 000 011 000 000 C[0] => 232 C[1] = 3 a => 100010
(11111010002)

5/4/12

*32 bits is allocated because int has larger size

Pointer Problems: Dangling Pointer


Dangling

pointer or Dangling reference: occurs when a pointer stores the address of an already deallocated heap-dynamic variable. causing this.

Explicit deallocation is the major reason Location can be reallocated to a new heap-

dynamic variable.

the location now could be temporarily used

by storage management system.

5/4/12

Solution: Tombstone
Tombstones:
Every

heap-dynamic variable includes an extra cell ( tombstone ) that is itself a pointer to the heap dynamic variable. -> tombstone -> heap-dynamic is variable

Pointer When

the heap-dynamic variable deallocated, the tombstone is set to NULL.

Disadvantages :
Costly in memory and time.
5/4/12

Solution: Lock-and-Keys
Pointers instead of only being an address,

also have a key and are made into a pair. (key, address) containing a lock.

Heap-dynamic variables also have a header When allocated, the key(in pointer) and the

lock( in the variable) are set the same. (key=lock) pointer, access is granted only if the lock and key match. Otherwise its an error.
5/4/12

When accessing the variable through the

In deallocating, the lock is cleared to an

Lost Heap-Dynamic Variables


Lost Heap-Dynamic Variables: an allocated

heap-dynamic variable accessible anymore.

that

is

not

This

happens regardless of deallocation happening implicitly or explicitly.


int* p;

p= new int; p= new int;

5/4/12

Pointers
In Ada:
are called access. Diminishes the dangling pointers problem :
Garbage collection (not supported in all compilers)

The lost heap-dynamic

not eliminated.

variable problem is

in C and C++:
Pointers are infinitely flexible, must be used

with care.

Offers no solution to dangling pointers or lost


5/4/12 heap-dynamic variable problems.

Reference Type
a pointer refers to an address, a reference

refers to an object.

In C++, reference is a constant pointer.

Since it is constant, should be initialized and cannot be changed later.


A

reference implicitly.

is

always

dereferenced

int a=1; int& b=a; //reference type // a and b are aliases.


5/4/12

Heap Management: SingleSized


All cells are linked together (linked-list) ,

and make a list of available space.

Allocating is taking a fixed number of cells. Deallocating is more complex.


The reason: hard to know when a variable is

no longer useful.

Two methods:
Reference counters ( eager approach ) Mark-sweep ( lazy approach )
5/4/12

Reference counters
Every cell has a counter. It counts the

number of the pointers pointing to it.

When the counter reaches zero, the cell is

deemed garbage and returned to the list of available space.


problems:
Costly in execution time and space Cells

connected reclaimed.

circularly

are

never

The advantage : the cells are reclaimed

when they are found to be 5/4/12 garbage. the reclaimation is done throughout the

Mark-Sweep
Allocates cell and disconnect them, without

reclaiming. (lets the garbage to build up) mark-sweep process reclaims garbage floating around. all

After all the available space is allocated,

the

Mark-sweep 3 phases:
Set phase Mark phase Sweep phase
5/4/12

Mark-Sweep
The

problem enough.
Costly in time.

is

that

it

isnt

frequent

Incremental mark-sweep:
Occurs more frequently, before the available

space is finished. The time for the process decrease, reducing the delay.

Another solution:
to perform mark-sweep process on specific
5/4/12 parts of the memory rather than the whole

Heap Management: Variablesized


Additional problems arise. In case of mark-

sweep:

Setting all the bits to indicate garbage is

difficult. Scanning of all the different sized cells is a problem.


cells with no pointers. How can you follow a

chain?
maintaining a list of available space.

5/4/12

Type Checking
Process to verify that the operands of an

operator are compatible types.


legal for the operator OR

A compatible type is

allowed under language rules to be

automatically converted by the compilergenerated code to a legal type (also known as coercion).

5/4/12

Example of a compatible type: int x, y, z; x = y + z; Example of coercion in Java where the the int variable is coerced to float: int x; float y, z; y = x + z;
5/4/12

Type Error
Happens when an operator is applied to an

operand with inappropriate type.

Types of type checking:


Static type checking Dynamic type checking

5/4/12

Static type checking


occurs at compile-time allows type errors to be found early languages include Ada, C, C++, C#

Dynamic type checking


occurs at run-time more flexibility languages include JavaScript, PHP, Lisp, Perl

5/4/12

Strong Typing
A strongly typed programming language

has severe restrictions that prevents the compilation of code with data that is used in an invalid way.

Type errors are always detected. Requires that the types of all operands

determined either at compile-time or runtime.

Example:

Integer addition operation may not be used on strings.


Importance of strong typing
5/4/12

The value of strong typing is however

weakened by coercion as it results in the loss of error detection. - Coercion Reliability - Coercion Reliability

Reliability of a language

5/4/12

Strong Typing in Different Languages


Fortran 95, C, C++
Not strongly typed. The use of Equivalence in Fortran allows a

variable of one type to refer to a value of a different type, without the system being able to check the type of the value when one of the Equivalenced variables is referenced or assigned.
C and C++ both include union types which are

not type checked.

5/4/12

Ada, Java, C#
Nearly strongly typed. Ada allows programmers to breach type-

checking rules which can only be done when Unchecked_Conversion is called.

5/4/12

Type Equivalence
Strict form of type compatibility. No coercion. 2 ways to define type equivalence
Name type equivalence Structure type equivalence

5/4/12

Name Type Equivalence


2 variables have equivalent types if they are defined in

the same declaration or in declarations that use the same type name.
Easy to implement. Compare only 2 type names. All types must have names. Restrictive. For example, a variables type with subrange of integers

is not equivalent to an integer type variable.


Sample code from Ada
type Indextype is 1..100; count : Integer; index : Indextype;

5/4/12

Structure Type Equivalence


2 variables have equivalent types if their

types have identical structures. equivalence.

More flexible compared to name type Difficult to implement. Entire structures of 2 types have to be

compared.

5/4/12

Derived Type

Subtype

A new type based on

previously defined type.

Type equivalent with

parent type.

Has identical

structure with parent type, but not equivalent. of parent type.


5/4/12

Inherits all properties

Type Theory
2 branches in computer science
Practical Abstract

Practical branch
Concerned with data types in commercial

programming languages.
Abstract branch
Concerned with typed lambda calculus, an

area of extensive research by theoretical computer scientists.


5/4/12

Data Type
Defines a set of values. Defines a collection of operations on those

values.

Elements are often ordered. Set operations can be used on data types

to define new data types. operators or constructors

Structured data types are defined by type

5/4/12

Thank You!

5/4/12

Вам также может понравиться