• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Chapter 6 data types
 

Chapter 6 data types

on

  • 519 views

 

Statistics

Views

Total Views
519
Views on SlideShare
518
Embed Views
1

Actions

Likes
0
Downloads
13
Comments
0

1 Embed 1

http://a0.twimg.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Chapter 6 data types Chapter 6 data types Presentation Transcript

    • Data Types (Chapter 6)Name: ID:AHMAD ARAFAT BIN MOHD ALI 1091105499NUR FAIRUZ BINTI MUHAMED ASRI 1091104133SYUHAIDA AHMAD ARIFFIN 1101108762SURAYA NURAIN BINTI KALID 1101109398
    • • What is data type? int x = 5;
    • BooleanCharacter- Charter types are stored as numeric codingsThey are used to represent switched and flags in programs.- (ASCII / Unicode). Complex Single Precision Numeric Floating Point Types Double Precision DecimalFloating Point: Integer1) Single Precision: Primitive Data Types Boolean Integer Decimal:Complex Type: Bytes Example: (in C#) decimal myMoney = 300.5m; JavaExample (in Phyton):2) Double Precision: Long(7+3j) ASCII Advantage: Greater precision and a smaller range compared to Floating Point Character ShortProgramming Languages that support Complex Type: UnicodeFortran, Phyton
    • Limited Dynamic String Strings Dynamic Length Length Static Length String: Design Issues 1) Static or dynamic?--The length canvarious lengthset whenmaximum. is created Special Type of character String strings can store any number of chars between2) -Allowsvariablesbe static and with no the string zero and the maximum -Requires ‘0’ is used to indicate the end of string’s characters (used primitive types? array or by C)-Delimiter the overhead of dynamic storage allocation and deallocation but provides flexibility Example Fotran-90: Character (LEN=15) name; Comparison AssignmentExample: JavaScript and Perl Operations Concatenation on String Character String Substring Types Copy LengthExample:Example:Assume s=“good”, t=“bye”Assume s=“good”, t=“bye”Assume s=“good”, t=“bye” Static Length Stringstrcpy(x,s); = //return false String lengthif (s.equals(t)) “goo”;strlen(s)=3;,‘hello’-Strcat(s,t)=goodbye Char x *+ =Subs(s1,0,2) (Fixed Length) Options Limited Dyna micx=good Length String Dynamic Length String
    • User-Defined Ordinal Types Enumeration Subrange Types Types2) Enumeration Types - provides a of defining and grouping collections of named1) Subrange Types - provides a way way of defining and grouping collections of namedconstant. constant.Example In Ada: C#:type Days is (Mon, Tue, Wed Thu, Fri, Sat, Sun);enum days {Mon, Tue,Wed, Thu, Fri, Sat, Sun};subtype Weekdays is Days range Mon..Fri;subtype IndexAdvantage: is Integer range 1..100;a) Readibility – Named values are easily recognizedAdvantages:b) Realibility – No arithmetic operations are legal on enumeration typea) Enhance Readibility – variable of subtype can store only certain range.b) Realibility (use integer values to represent enumeration to assignExample in C– because a compiler can detect error if we try type): value to a specifiedsubrange value.int red = 0, blue = 1
    • Categories Of ArrayCategories of Range of Storage Bindings Advantages ExampleArray subscripts1. Static Array Statically bound Static (done Execution Fortran-77, C, C++ before runtime) efficiency2. Fixed Stack Statically bound Done during Storage space C , C++Dynamic Array execution can be reused3. Stack Dynamic Dynamic Dynamic Flexibility AdaArray4. Fixed Heap Fixed after Fixed after Flexibility Fortran 95, C,Dynamic Array storage is storage is C++, Java, C# allocated allocated -done when -allocated from user program the heap requests during execution5. Heap Dynamic Dynamic Dynamic Flexibility C#, Java,Array JavaScript, Python, Ruby
    • Array InitializationProgramming Language How it is initialize?1. Fortran 95 Integer, Dimension (3) :: List = (/0, 5, 5 /)2. C and C++ int list [ ] = {4, 5, 7, 83}; int list [3] = {3, 5, 7}; char name * + = “freddie”;3. Java String[ + names = *“Bob”, “Jake”+; Array OperationProgramming Language Example OperationFortran 95 ( + ) operator Give result sum of element pairs of the two arrays.
    • Rectangular Array Jagged ArrayLanguage supported: Language supported: Fortran  Java Ada  C C#  C++  C#
    • [0] [1] [2][0] 2 6 10[1] 3 5 8 Implementation Of Array Types[2] 12 1 7 Row Major Order Column Major Order Int num[3][3]; Int num[3][3]; [0] [1] [2] [1] [2] [3] [0] 2 6 10 [1] 2 6 10 [1] 3 5 8 [2] 3 5 8 [2] 12 1 7 [3] 12 1 7 2 6 10 3 5 8 12 1 7 2 3 12 6 5 1 10 8 7 Language : C, Pascal, Java Language : Fortran
    • Record typeWhat is record? Record is an aggregate of data elements in which the individual elements are identified by names and accessed through offset from the beginning of the structure.
    • Differences between Record and ArrayRecord ArrayFields are referenced by using Fields are referenced bytheir names indicesUse when the collection of Use when all the datadata objects is heterogeneous elements have same type andand fields are not processed in are processed in same way.same way.Fields of records are not Processing of array elements isprocessed in any particular usually done by in a sequentialsequential order. order
    • Operations on RecordsProgramming OperationLanguagesAda 1. Allow comparison  = (equality)  /= (inequality) 2. Initialize with aggregate literals.COBOL 1. Provide MOVE CORRESPONDING Example: MOVE CORRESPONDING INPUT_RECORD TO OUTPUT_RECORD. 01 INPUT_RECORD. 02 NAME. 05 LAST PICTURE IS X (20). 05 MIDDLE PICTURE IS X (15). 05 FIRST PICTURE IS X (20) . 02 EMPLOYEE_NUMBER PICTURE IS 9 (10). 02 HOURS_WORKED PICTURE IS 99. 01 OUTPUT_RECORD. 02 NAME. 05 LAST PICTURE IS X (20). 05 MIDDLE PICTURE IS X (15). 05 FIRST PICTURE IS X (20) . 02 EMPLOYEE_NUMBER PICTURE IS 9 (10). 02 GROSS_PAY PICTURE IS 999V999. 02 NET_PAY PICTURE IS 999V999.
    • Language Name Type Equivalent Structure Type EquivalentAda Works well More rigidC Works well Works wellC++ Works well Works wellFortran Cannot use Works wellCOBOL Cannot use Works well
    • 6.8 Union TypeIntroduction• A union is a type whose variables are allowed to store different type values at different times during execution.• Example: union foo { union foo x; int a; x.a = 3; // OK char b; x.b = c; } foo; // NO! this affects the value of x.a! //can’t use both a and b at the same time Prints out 99,99 struct bar { struct bar y; int a; y.a = 3; // OK char b; y.b = c; // OK } bar;//can use a and b simultaneously Prints out 387427, 99
    • 6.8.2 Discriminated versus free unions Type of Unions Discriminated Free Unions Unions It has a type checking support for unions Fortran, C, and C++ provide require that each union include a type union constructs in which indicator. The first language to provide discriminated there is no language supportunion was ALGOL 68 which is later supported for type checking therefore it by ADA. is unsafe to use in these languages. Unconstrained Constrained Variant Variable Variant Variable
    • 6.8.3 Ada union type Ada union type Constrained variant Unconstrained variant variable records Allow the values of their variants to change typesAllows the user to specify during execution. The type can only be changed variables of a variant only by assigning the entire record, including the record type that will store discriminant. only one of the possible This disallows inconsisient records because if the type values in the variant. newly assigned record is a constant data In this way, the user can aggregate, the value of the tag and the type of tell the system when the variant can be statically checked for consistency. type checking can be static, because we know If the assigned value is a variable, its consistencythat type checking must be was guaranteed when it was assigned, so the new dynamic. value of the variable now being assigned is sure to be consistent.
    • How does union implemented in At compile time, programming the complete languages? description of each variant must beUse the same address stored by creating afor every variant. case table. In Ada language, the Sufficient storage for exact amount of the largest variant is storage can be used allocated. because there is no variation.
    • The descriptor for this type could have the form shown in thefigure below:
    • 6.9 Pointer and reference typeIntroduction• A pointer type variable has a range of values that consists of memory addresses and a special value, nil• Provide the power of indirect addressing• Provide a way to manage dynamic memory• A pointer can be used to access a location in the area where storage is dynamically created (usually called a heap)
    • 6.9.2 Pointer operations• Language that provide pointer has two fundamental operations: assignment and dereferencing• Assignment is used to set a pointer variable’s value to some useful address• Dereferencing yields the value stored at the location represented by the pointer’s value – Dereferencing can be explicit or implicit – C++ uses an explicit operation via * (asterisk) j = *ptr sets j to the value located at ptr
    • Problems with pointer Dangling lost heap- pointers dynamic (dangerous) variableA pointer points An allocated heap-dynamic to a heap- variable that is no longerdynamic variable accessible to the user program (often called that has been garbage) deallocated. 1. Pointer p1 is set to point to a newly created heap-dynamic variable 2. Pointer p1 is later set to point to another newly created heap-dynamic variable 3. The first heap –dynamic variable is now inaccessible (memory leakage).
    • Pointers Ada C++• It is called access types • Extremely flexible but must be used with care• Some dangling • Pointers can point at any variable regardless of pointers are when it was allocated disallowed because • Used for dynamic storage management and dynamic objects can addressing be automatically de- • Pointer arithmetic is possible allocated at the end of • Explicit dereferencing and address-of operators pointers type scope • Domain type need not be fixed (void *)• The lost heap- • void * can point to any type and can be type dynamic variable checked (cannot be de-referenced) problem is not eliminated by Ada.
    • 6.9.6 Reference types• C++ includes a special kind of pointer type called a reference type that is used primarily for formal parameters – Advantages of both pass-by-reference and pass-by-value• Java extends C++’s reference variables and allows them to replace pointers entirely – References refer to call instances• C# includes both the references of Java and the pointers of C++
    • 6.9.7 Evaluation of pointers• Dangling pointers and dangling objects are the same problems as is heap management• Pointers are like gotos--they widen the range of cells that can be accessed by a variable• Pointers or references are necessary for dynamic data structures--so we cant design a language without them.
    • Implementation of pointers and reference typeRepresentation of Solutions to dangling pointers and Heap management pointers referencces Tombstone: extra heap cell that is a pointer A very complex run-time• Large to the heap-dynamic variable process computers use single The actual pointer variable points only Single-size cells vs. variable- at tombstones size cells values When heap-dynamic variable de-• Intel Two approaches to reclaim allocated, tombstone remains but set microproce garbage to nil ssors use Reference counters segment Costly in time and space (eager approach): and offset Locks-and-keys: Pointer values are represented as (key, address) pairs reclamation is gradual Heap-dynamic variables are Garbage collection (lazy represented as variable plus cell for approach): reclamation integer lock value occurs when the list of When heap-dynamic variable variable space becomes allocated, lock value is created and empty placed in lock cell and key cell of pointer
    • Reference counter• Reference counters: maintain a counter in every cell that store the number of pointers currently pointing at the cell – Disadvantages: space required, execution time required, complications for cells connected circularly – Advantage: it is intrinsically incremental its actions are interleaved with those of the applicasion so it never causes significant delays in the execution of the application.
    • Garbage collection• The run-time system allocates storage cells as requested and disconnects pointers from cells as necessary; garbage collection then begins – Every heap cell has an extra bit used by collection algorithm – All cells initially set to garbage – All pointers traced into heap, and reachable cells marked as not garbage – All garbage cells returned to list of available cells – Disadvantages: when you need it most, it works worst (takes most time when program needs most of cells in heap)
    • Marking algorithm
    • Variable sized cells• All the difficulties of single-size cells plus more• Required by most programming languages• If garbage collection is used, additional problems occur – The initial setting of the indicators of all cells in the heap is difficult – The marking process in nontrivial – Maintaining the list of available space is another source of overhead
    • 6.10 Type Checking
    • Type Checking Activity of ensuring that the Type Erroroperands of an operator are of compatible types Application of an operator to an operand of an inappropriate type
    • Types of Type CheckingStatic type checking  Dynamic type checking•Perform type checking • Perform type checking during before runtime runtime•For static type binding • For dynamic type binding languages languagesExp: Earlier assembler, FORTRAN • Exp: JavaScript, PHPAdvantages: Advantage:-earlier detection of - easier to handle situations thatprogramming mistakes require self-modifying code- increased runtime efficiencyDisadvantage: Disadvantage:-Longer compilation time -longer runtime
    • 6.11 Strong Typing
    • Strong Type Programming language is strongly typed if type errors are always detected.Advantage: Disadvantage:-Ability to detect all misuses of - Weakened by coercionvariables that result in type errors-Also allows the detection at run time Language Range Comments Fortran 95 Not strongly typed Because the use of Equivalence between variables of different types Ada Nearly strong typed Allows the breach of type-checking rules using function Unchecked_Conversion C, C++ Not strongly typed Both include union types, which are not typed checked Java, C# Nearly strongly typed Types can be explicitly cast, which could result in a type error
    • 6.12 Type Equivalence
    • Type Type Compatibility Equivalence An operand of one type in an expressionTypes of operand that are acceptable for is substituted for one of the other typeeach of the operators and thereby specify without coercion – compatibility without the possible type errors of the language coercion
    • Types of Type Equivalence Name type equivalence  Structure type equivalence• Two name have equivalence • Equivalence if their types have type if they are defined under identical structure. the same declaration or declaration that use the same type name. Advantage: - more flexibleAdvantages:- Easy to implement- More restrictive Disadvantage:Disadvantage: - More difficult to implement- Subrange of the integers would not be - Entire structure of the two types equivalent to an integer type variable. must be compared type Indextype is 1..100; type Celsius = Float; count : Integer; Fahrenheit = Float; index : Indextype;
    • Derived Type• New type that is based on previously defined type• Not equivalent although it may have identical structure• Inherit all the properties of their parent types• Can include range constraints on the parent type, while still inheriting all the parent’s properties type Celsius is new Float; type Fahrenheit is new Float;
    • Subtype Type equivalent to its parent typesubtype Small_type is Integerrange 0.99;The type Small_type isequivalent to the type Integer
    • 6.13 Theory & Data Types
    • Theory •Type lambda calculus •Combinators •The Methatheory of Bounded Quantification •Existential Types •Higher-Order Polymorphism Practical  Abstract• Concerned with data • Focuses on type lambda types in commercial calculus programming languages
    • Data Type Type SystemSet of values and a collection of operations on those values. Set of types and the values that govern their use in programs
    • Model Of Type System Formal model Alternative modelSet of types and a • Uses a type map and acollection of functions that collection of functionsdefine the type rules of thelanguage, which are usedto determine the type ofany expression Typed Language Static typed language  Dynamic typed language• Type map need only be • Type map must be maintained during maintained during compilation time execution