Programming Languages Chapter 6: Other Types

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Definition of Records in COBOL • COBOL uses level numbers to show nested records; others use recursive definition 01 EMP-REC. 02 EMP-NAME. 05 FIRST PIC X(20). 05 MID PIC X(10). 05 LAST PIC X(20). 02 HOURLY-RATE PIC 99V99. • EMP-REC record consists of the EMP-NAME record and the HOURLY-RATE field

...

Garbage Collection Algorithms • Whose responsibility is garbage collection? • C and C++ default the garbage collection to the programmer - Provide explicit tools, such as the delete function - Recent versions provide more options • Java, Lisp, and other languages provide automatic garbage collection and take the responsibility for heap management out of the hands of programmers

...

List Types • F# Lists - Like those of ML, except elements are separated by semicolons and hd and tl are methods of the List class • Python Lists - The list data type also serves as Python's arrays - Unlike Scheme, Common Lisp, ML, and F#, Python's lists are mutable - Elements can be of any type - Create a list with an assignment myList = [3, 5.8, "grape"

...

List Types • Haskell's List Comprehensions - The original [n * n | n <- [1..10]] • F#'s List Comprehensions let myArray = [|for i in 1 .. 5 -> [i * i) |] • Both C# and Java supports lists through their generic heap-dynamic collection classes, List and ArrayList, respectively

...

List Types • List Operations in ML - Lists are written in brackets and the elements are separated by commas - List elements must be of the same type - The Scheme CONS function is a binary operator in ML, :: 3 :: [5, 7, 9] evaluates to [3, 5, 7, 9] - The Scheme CAR and CDR functions are named hd and tl, respectively

...

List Types • List Operations in Scheme - CAR returns the first element of its list parameter (CAR ′(A B C)) returns A - CDR returns the remainder of its list parameter after the first element has been removed (CDR ′(A B C)) returns (B C) - CONS puts its first parameter into its second parameter, a list, to make a new list (CONS ′A (B C)) returns (A B C) - LIST returns a new list of its parameters (LIST ′A ′B ′(C D)) returns (A B (C D))

...

List Types • Python Lists (continued) - List elements are referenced with subscripting, with indices beginning at zero x = myList[1] Sets x to 5.8 - List elements can be deleted with del del myList[1] - List Comprehensions - derived from set notation [x * x for x in range(7) if x % 3 == 0] range(7) creates [0, 1, 2, 3, 4, 5, 6] Constructed list: [0, 9, 36]

...

Look at slides 24 - 26 JonJon for Unions in F#

...

Pointer Arithmetic in C and C++ float stuff[100]; float *p; p = stuff; *(p+5) is equivalent to stuff[5] and p[5] *(p+i) is equivalent to stuff[i] and p[i

...

Reference Counting • When a pointer assignment occurs in the program, such as q = p the following book-keeping on the RCs must be done: 1. The RC for p's node is increased by 1 2. The RC for q's node is decreased by 1 3. If the RC for q's node becomes 0, the RC for each of its descendants is decreased by 1, q's node is returned to the free_list, and this step is repeated for each of q's node's descendants 4. The pointer q is assigned the reference value of p

...

References to Records • Record field references 1. In COBOL field_name OF record_name_1 OF ... OF record_name_n MID of EMP-NAME OF EMP-REC 2. In Others (dot notation) record_name_1.record_name_2. ... record_name_n.field_name • Fully qualified references must include all record names • Elliptical references allow leaving out record names as long as the reference is unambiguous, for example in COBOL FIRST, FIRST OF EMP-NAME, and FIRST of EMP-REC are elliptical references to the employee's first name

...

Strong Typing More Language examples: - Scheme and Perl (both dynamically typed) are strongly typed 1. The typing of values themselves, and the run time checking of those values by the interpreter prevent undetected type errors - ML and F# are

...

Structure Type in C • Example: C struct employeeType { int id; char name[26]; int age; float salary; char dept; } struct employeeType employee; ... employee.age = 45;

...

Type Checking 0100 0000 0101 1000 0000 0000 0000 0000 • The floating point number 3.375 • The 32-bit integer 1,079,508,992 • Two 16-bit integers 16472 and 0 • Four ASCII characters: @ X NUL NUL

...

Type Checking • Machine data carries no type information (it's typeless ) • Basically, just a sequence of bits. • Example: 0100 0000 0101 1000 0000 0000 0000 0000 • What does this binary string represent?

...

Union Types • Example: C/C++ union myUnion { int i; float r; } union myUnion u; • Under the u.i variant, the storage location is interpreted as an integer • Under the u.r variant, the storage location is interpreted as a floating point value

...

look a the example on slide 68 JonJon

...

look at picture on slide 38

...

Implementation of Record Type (look on slide 11 JonJon) Individual fields within a structure are usually allocated in a contiguous block. Number of bytes allocated and order of fields is machine and compiler dependent. Offset address relative to the beginning of the records is associated with each field

....

Look at slides 55- 57 JonJon

....

Mark-Sweep Mark-Sweep Algorithm • Makes 2 passes on the heap • Uses a mark bit (MB) which is attached to each heap node and initialized to 0 Pass I (the mark pass): Mark all nodes that are (directly or indirectly) accessible from the stack by setting their MB=1. Pass II (the sweep pass): Sweep through the entire heap and return all unmarked (MB=0) nodes to the free list. • Note: all orphans are detected and returned to the free list.

....

look at slide 40 for JonJon for garbage example

....

Heap Management *Contains value that are dynamically allocated and structured while the program is...... -fixed..... -variable.... -allocated at......

The Heap • Contains values that are dynamically allocated and structured while the program is running, such as strings, arrays, objects, and various dynamic data structures like linked lists - Fixed size - Variable content - Allocated at run-time

Reference Type *C++ includes a special kind of pointer type called a -advantages of both.... *Java extends C++'s reference variables and allows..... -references are references to objects, rather than..... *C# includes both the references of......

• 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 are references to objects, rather than being addresses • C# includes both the references of Java and the pointers of C++

Copy Collection *Called only when.... *Make only one pass on the..... *Partitions the heap into 2 halves, called the ( ) and ( ) -only is one is....... *Requires significantly more memory because only half the entire....... *Does not use a ( ), but maintains a ( )

• Called only when the heap is full • Makes only one pass on the heap, so it is faster than mark-sweep • Partitions the heap into 2 halves, called the from_space and the to_space (only one is active) • Requires significantly more memory because only half the entire heap space is actively available for allocating new memory blocks to the program at any one time • Does not use a free_list, but maintains a pointer free to the end of the allocated area (the active area)

Mark-Sweep *Called only when the heap is..... -Invoked whenever a new mode is requested by the.... *So, several thousand heap block allocations and deallocations can..... *Once mark-sweep is invoked, a more elaborate and.......

• Called only when the heap is full (free_list is empty) - Invoked whenever a new node is requested by the program and the heap is full • So, several thousand heap block allocations and deallocations can take place with no garbagecollection overhead occurring • Once mark-sweep is invoked, a more elaborate and time-consuming process interrupts the program

Problems with Pointers *Dangling pointers (dangerous) -A pointer points to a .... *Lost heap-dynamic variable -An allocated heap-dynamic variable that is no longer.... 1. example.....

• Dangling pointers (dangerous) - A pointer points to a heap-dynamic variable that has been deallocated • Lost heap-dynamic variable - An allocated heap-dynamic variable that is no longer accessible to the user program (often called garbage ) • Pointer p1 is set to point to a newly created heapdynamic variable • Pointer p1 is later set to point to another newly created heap-dynamic variable • The process of losing heap-dynamic variables is called memory leakage

Evaluation of Pointers *Dangling pointers and dangling objects are.... *Pointers are like goto's they widen the..... *Pointers or references are necessary for......

• Dangling pointers and dangling objects are problems as is heap management • Pointers are like goto's--they widen the range of cells that can be accessed by a variable • Pointers or references are necessary for dynamic data structures--so we can't design a language without them

Discriminated vs. Free Unions *Design Issue -should type..... *C and C++ provide union constructs in which there is no language....... -the union in these languages is called.... *Type checking of unions require that each union include a..... -supported by....

• Design issue - Should type checking be required? • C and C++ provide union constructs in which there is no language support for type checking - The union in these languages is called free union • Type checking of unions require that each union include a type indicator called a discriminant - Supported by ML, Haskell, and F#

Pointers in C and C++ *Extremely flexible but.... *Pointers can point at any variable regardless of.... *Used for dynamic..... *Pointer...... *Explicit dereferencing and.... *Domain type need not.....

• Extremely flexible but must be used with care • Pointers can point at any variable regardless of when or where it was allocated • Used for dynamic storage management and addressing • Pointer arithmetic is possible • Explicit dereferencing and address-of operators • Domain type need not be fixed (void *) void * can point to any type and can be type checked (cannot be de-referenced)

Record Types *First appeared in... *First abstract form of a structure originated in both..... *Java omits scutures (why) *C++ has structures.... -why

• First appeared in Cobol and PL/I • First abstract form of a structure originated in both Pascal-style languages and C-style languages • Java omits structures entirely (considered redundant because of classes) • C++ has structures in addition to classes - retains backward compatibility with C

Evaluation of Unions *Free unions are..... -why? *Java and C# do not support.... -Why?

• Free unions are unsafe - Do not allow type checking • Java and C# do not support unions - Reflective of growing concerns for safety in programming language

Heap Management *Garbage is any block in heap memory that.... *Garbage can occur when either: -An allocated block of.... -A reference exists to a block of memory.... Example:......

• Garbage is any block in heap memory that cannot be accessed by the program. • Garbage can occur when either: 1. An allocated block of heap memory has no reference to it (an "orphan"), or 2. A reference exists to a block of memory that is no longer allocated (a "widow"). Example: a dynamically allocated array has its descriptor removed from the stack (when stack frame is popped) before its heap block is returned via a delete

Type Checking *I all type binding are static, nearly all..... *If type bindings are dynamic,....... *A programming language is strongly typed if...... -Advantage of strong typing: allows the detection of....

• If all type bindings are static, nearly all type checking can be static • If type bindings are dynamic, type checking must be dynamic • A programming language is strongly typed if type errors are always detected - Advantage of strong typing: allows the detection of the misuses of variables that result in type errors

Type Equivalence *Important to clearly define type equivalence -To be able to properly.... -To be able to properly implement....

• Important to clearly define type equivalence - To be able to properly implement assignment operation - To be able to properly implement parameter passing of nonbasic types

List Types *List in Lisp and Scheme are delimited by.... *Data and code have the same form -As data, (A,B,C) is literally.... -As code, (A B C) is the.... *The interpreter needs to know which a list is, so if it is data, we......

• Lists in Lisp and Scheme are delimited by parentheses and use no commas (A B C D) and (A (B C) D) • Data and code have the same form As data, (A B C) is literally what it is As code, (A B C) is the function A applied to the parameters B and C • The interpreter needs to know which a list is, so if it is data, we quote it with an apostrophe ′(A B C) is data

Garbage Collection Summary *Modern algorithms are more elaborate -Most are...... -May select between ( ) and ( ) depending on the number..... *In java, garbage collection is built-in -Runs as a low-priority thread- executes whenever....... 1. Reduces the number of.... -Also, the garbage collector ( ) may be explicitly called by the program- invokes..... *Functional languages have.... *C/C++ default garbage collection to the.....

• Modern algorithms are more elaborate. - Most are hybrids/refinements of the above three. - May select between mark-sweep and copy collection depending on the number of active nodes compared to the heap size • In Java, garbage collection is built-in. - Runs as a low-priority thread - executes whenever demand on processing time from other threads is low • Reduces the number of garbage collection calls needed during peak processing times. - Also, the garbage collector System.gc() may be explicitly called by the program - invokes garbage collection immediately • Functional languages have garbage collection built-in. • C/C++ default garbage collection to the programmer

Evaluation and Comparison to Arrays *Records are used when collection of..... *Access to array elements is much slower than access to... *Dynamic subscripts could be used with.....

• Records are used when collection of data values is heterogeneous • Access to array elements is much slower than access to record fields, because subscripts are dynamic (field names are static) • Dynamic subscripts could be used with record field access, but it would disallow type checking and it would be much slower

Reference Counting *Reference counting algorithm is applied dynamically -Whenever a new or delete operation occurs on a pointer,..... -An orphan is a node whose RC becomes 0, or any of.... -When a new operation occurs, the algorithm allocates a.....

• Reference counting algorithm is applied dynamically - Whenever a new or delete operation occurs on a pointer, the algorithm is activated and as many orphans as possible are returned to the free_list - An orphan is a node whose RC becomes 0, or any of its descendants (in the chain) whose RC becomes 0 as a result of a delete operation - When a new operation occurs, the algorithm allocates a heap node from the free_list and initializes its RC to 1

Strong Typing *Strong Typing -promotes more.... -is viewed as a virtue in.... *Union types are hole in the....

• Strong typing - promotes more reliable programs - is viewed as a virtue in programming language design • Union types are a hole in the type system of many languages.

Garbage Collection Algorithms *The heap contains both ( ) and ( ) -Inactive objects are those blocks that had been..... -altogether, inactive objects are..... *Garbage collection is any strategy that reclaims unused heap....

• The heap contains both active and inactive objects - Inactive objects are those blocks that had been allocated during an earlier stage of program execution and then relegated to the status of "no longer needed" - Altogether, inactive objects are known as garbage • Garbage collection is any strategy that reclaims unused heap blocks for later use by the program

Garbage Collection Algorithms *Reference Counting -occurs whenever... -doesn't detect all *Mark-Sweep -occurs only... -detect all garbage, but.... *Copy Collection -faster than mark-sweep, but...

• Three classic garbage collection strategies: - Reference Counting • occurs whenever a heap block is allocated • doesn't detect all garbage. - Mark-Sweep • occurs only on heap overflow • detects all garbage, but makes two passes on the heap. - Copy Collection • faster than mark-sweep, but reduces the size of the heap space.

Pointer Operations *Two fundamental operations: (what) *Assignment is used to..... *Dereferencing yields the value stored at the...... -Dereferencing can be.... -C++ uses an.....

• 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 * j = *ptr sets j to the value located at ptr

Type checking *Type checking is the activity of ensuring that the........ -A compatible type is one that is either.......... 1. the automatic conversion is called.......

• Type checking is the activity of ensuring that the operands of an operator are of compatible types - A compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted, by compiler- generated code, to a legal type • This automatic conversion is called a coercion

Type Equivalence • Under name equivalence, two types are the same if they have the.... look at example on slide 75 JonJon

• Under name equivalence, two types are the same if they have the same name - In the previous example, variables c and d have the same type, because their types share the same type name: struct complex. - a and c are not the same type, since their types have different type names (a is an anonymous struct) - d and e are not the same type either - a and b are the same type under name equivalence

Type Equivalence *Under structural equivalence, two types are the same if.....

• Under structural equivalence, two types are the same if they have the same structure - For records, having the same structure means having the same number and order of fields, as well as the name and type of each field. - Under structural equivalence, a, b, c, and d are the same type - d and e have different types, since the two field names are different (even though both have two floating point fields

Design Issues of Pointers *What are the scope and.... *What is the lifetime of a.... *Are pointers are restricted as to the type.... *Are pointers used for dynamic storage..... *Should the language support.....

• What are the scope of and lifetime of a pointer variable? • What is the lifetime of a heap-dynamic variable? • Are pointers restricted as to the type of value to which they can point? • Are pointers used for dynamic storage management, indirect addressing, or both? • Should the language support pointer types, reference types, or both?

Pointer and Reference Types *A pointer type variable has a range of values that consist of.... *provide the power of..... *provide a way to manage.... *A pointer can be used to access a location in the area where......

• 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)

Record Types *A record is a possibly heterogeneous aggregate of data elements in which the.... -also called...... *Design issues: -What is the syntactic form of references to.... -Are elliptical.....

• A record is a possibly heterogeneous aggregate of data elements in which the individual elements are identified by names - Also called structures • Design issues: - What is the syntactic form of references to the field? - Are elliptical references allowed?

Strong Typing *A strongly typed language can be either..... -Ada and Java are.... -C and C++ are not..... 1. Parameter type checking can be.... -Java and C#...;.

• A strongly typed language can be either statically or dynamically typed. - Ada and Java are strongly typed - C and C++ are not strongly typed (even though they are statically typed) 1. parameter type checking can be avoided; unions are not type checked - Java and C# are, almost (because of explicit type casting)

Tuple Types *A tuple is a data type that is similar to a record, except that..... *Used in Python, ML, and F# to allow functions to.... -python 1. closely related to its list, but..... 2. example....

• A tuple is a data type that is similar to a record, except that the elements are not named • Used in Python, ML, and F# to allow functions to return multiple values - Python • Closely related to its lists, but immutable • Create with a tuple literal myTuple = (3, 5.8, ′apple′) Referenced with subscripts (begin at 1) Catenation with + and deleted with del

Type error *A type error is any error that arises because an operation is...... -Type errors are common in..... -High level languages reduce the....

• A type error is any error that arises because an operation is attempted on a data type for which it is undefined. - Type errors are common in assembly language programming. - High level languages reduce the number of type errors

Type system *A type system provides a basis for..... *A type system is a precise definition of the binding between the......

• A type system provides a basis for detecting type errors. • A type system is a precise definition of the bindings between the type of a variable, its values, and the possible operations on those values.

Unions Types *A union is a type whose variables are allowed to store different type values at..... -two or more different fields share -only one field holds a valid value.... -the other fields will contain..... *C:....... *Pacal:.......

• A union is a type whose variables are allowed to store different type values at different times during execution - Two or more different fields share the same block of memory - Only one field holds a valid value at any given time - The other fields will contain nonsense • C: Union • Pascal: Case-variant record

Reference Counting *Advantage -it occurs dynamically, whenever a pointer assignment or other heap action is triggered by... *Disadvantages -It fails to detect.... -There is storage overhead due to..... -performance overhead is created by the algorithm whenever a......

• Advantage - It occurs dynamically, whenever a pointer assignment or other heap action is triggered by the program; thus, the overhead associated with garbage collection is naturally distributed over the run-time life of the program • Disadvantages - It fails to detect inaccessible circular chains - There is storage overhead due to appending an integer reference count to every node in the heap - Performance overhead is created by the algorithm whenever a pointer is assigned or a heap block is allocated or deallocated

Mark-Sweep *Advantages -reclaims all.... -it is only invoked when.... *Disadvantage -the algorithm is more..... -storage overhead......

• Advantages - Reclaims all garbage in the heap - It is only invoked when the heap is full • Disadvantage - The algorithm is more time-consuming when it does run - Storage overhead from the mark bit

Reference Counting *Assumes the initial heap is a.... *Each node has a reference count(RC)-...... *example of garbage of occuring look at picture slide 44

• Assumes the initial heap is a chain of nodes (the free_list) • Each node has a reference count (RC) - a count of the number of pointers referencing that node (initially 0) • For an assignment, like q = p, garbage can occur


Set pelajaran terkait

Chapter 3 - Role of Project Manager

View Set

Cognition Test #3 Reading Questions

View Set

California Real Estate Principles - Chapter 8 - Financing Real Estate

View Set

Fast Track Chapters 17-20 Study Guide

View Set

II. Task I: High Altitude Operations

View Set