Submit Search
chapter 7: DATA TYPES - IN PROGRAMMING LANGUAGES
Download as PPT, PDF
0 likes
12 views
F
fargovagra
chapter 7: DATA TYPES - IN PROGRAMMING LANGUAGES
Education
Read more
1 of 62
Download now
Download to read offline
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
More Related Content
PPT
14-types.ppt
DhineshS45
PDF
Compiler Construction | Lecture 7 | Type Checking
Eelco Visser
PPTX
Type Systems
Jordan Parmer
PDF
12TypeSystem.pdf
MdAshik35
PPT
Ch6.ppt
daniloalbay1
PPTX
Chapter -5.pptx chapter five compiler design
gadisaAdamu
PPTX
Type checking compiler construction Chapter #6
Daniyal Mughal
PPTX
Unit 3 Compiler Design Regulation 2021.pptx
jeevitha404389
14-types.ppt
DhineshS45
Compiler Construction | Lecture 7 | Type Checking
Eelco Visser
Type Systems
Jordan Parmer
12TypeSystem.pdf
MdAshik35
Ch6.ppt
daniloalbay1
Chapter -5.pptx chapter five compiler design
gadisaAdamu
Type checking compiler construction Chapter #6
Daniyal Mughal
Unit 3 Compiler Design Regulation 2021.pptx
jeevitha404389
Similar to chapter 7: DATA TYPES - IN PROGRAMMING LANGUAGES
(20)
PPTX
Compiler Design Notes for rgpv 6tth sem students
MedhanshAgrawal
PPT
Type Checking(Compiler Design) #ShareThisIfYouLike
United International University
PPTX
Lecture 02 Programming C for Beginners 001
MahmoudElsamanty
PDF
Type Checking
A. S. M. Shafi
PPT
Lecture 21 22
Najmul Hassan
PDF
Can functional programming be liberated from static typing?
Vsevolod Dyomkin
PPT
pl10ch6_datatypesprinciplesof programing.ppt
vimalgaur7
PPT
Ch6
kinnarshah8888
PDF
PL Lecture 03 - Types
Schwannden Kuo
PPT
pl12ch6.ppt
BrandonGarcia294013
PPT
Theory of programming language chapter 6
memonrizwan053
PPT
Data types and it's usage in c languages
BalaKrishnan466
PDF
Typeful programming
mustafa sarac
PPT
ch6-Short.ppt eee cse www rrr www qqq rrr ttt
winimag331
PPT
6 data types
Munawar Ahmed
PDF
DTS s03e04 Typing
Tuenti
PPTX
Ds12 140715025807-phpapp02
Salman Qamar
PPTX
Data structures using C
Pdr Patnaik
PDF
C Plus Data Structures Subsequent Dale Nell B
noyzdapat39
PPTX
8. data types
Zambales National High School
Compiler Design Notes for rgpv 6tth sem students
MedhanshAgrawal
Type Checking(Compiler Design) #ShareThisIfYouLike
United International University
Lecture 02 Programming C for Beginners 001
MahmoudElsamanty
Type Checking
A. S. M. Shafi
Lecture 21 22
Najmul Hassan
Can functional programming be liberated from static typing?
Vsevolod Dyomkin
pl10ch6_datatypesprinciplesof programing.ppt
vimalgaur7
Ch6
kinnarshah8888
PL Lecture 03 - Types
Schwannden Kuo
pl12ch6.ppt
BrandonGarcia294013
Theory of programming language chapter 6
memonrizwan053
Data types and it's usage in c languages
BalaKrishnan466
Typeful programming
mustafa sarac
ch6-Short.ppt eee cse www rrr www qqq rrr ttt
winimag331
6 data types
Munawar Ahmed
DTS s03e04 Typing
Tuenti
Ds12 140715025807-phpapp02
Salman Qamar
Data structures using C
Pdr Patnaik
C Plus Data Structures Subsequent Dale Nell B
noyzdapat39
8. data types
Zambales National High School
Ad
More from fargovagra
(7)
PPTX
THIS IS ABOUT THE START UPS AND WE PRESENT INDAIN STARTUPS
fargovagra
PPTX
FIM FINANCIAL INCLUSION MECHANISM IN SPORTS
fargovagra
PPTX
business_incubators_startup_india. AND pptx
fargovagra
PPTX
Fairness and inclusion mechanism in sports
fargovagra
PPTX
RPE RETRACTION AND RELATED POLICIES ABOUT RESEARCH PAPER
fargovagra
PPT
Compiler-Design-Intermediate-code-generation.ppt
fargovagra
PPT
mendeley with word and other softwares for maintaining the references
fargovagra
THIS IS ABOUT THE START UPS AND WE PRESENT INDAIN STARTUPS
fargovagra
FIM FINANCIAL INCLUSION MECHANISM IN SPORTS
fargovagra
business_incubators_startup_india. AND pptx
fargovagra
Fairness and inclusion mechanism in sports
fargovagra
RPE RETRACTION AND RELATED POLICIES ABOUT RESEARCH PAPER
fargovagra
Compiler-Design-Intermediate-code-generation.ppt
fargovagra
mendeley with word and other softwares for maintaining the references
fargovagra
Ad
Recently uploaded
(20)
PPTX
History, Philosophy and sociology of education (1).pptx
tmdth1
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
mathiasmelwyn7
PPTX
Lesson notes of climatology university.
sgladness67
PDF
1_English_Language_Set_2.pdf probationary
emailjagmeetsingh9
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
ElishamichaelSamwel
PDF
IGGE1 Understanding the Self1234567891011
rhea22labucay
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
NielDestora
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
S LS
PDF
A systematic review of self-coping strategies used by university students to ...
CleeveMoralde1
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
Medpopz
PPTX
Cell Types and Its function , kingdom of life
ClaireMangundayao1
PPTX
Final Presentation General Medicine 03-08-2024.pptx
ZaheerAhmad228692
PDF
Weekly quiz Compilation Jan -July 25.pdf
Nitin Suresh
PDF
What if we spent less time fighting change, and more time building what’s rig...
Derek Wenmoth
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
kawisojames10
PDF
Hazard Identification & Risk Assessment .pdf
estagiarioprotegesms
PDF
Computing-Curriculum for Schools in Ghana
abigailanane203
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
EricaRamos80
PDF
RMMM.pdf make it easy to upload and study
sajedul2
PPTX
Final Presentation General Medicine 03-08-2024.pptx
ZaheerAhmad228692
History, Philosophy and sociology of education (1).pptx
tmdth1
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
mathiasmelwyn7
Lesson notes of climatology university.
sgladness67
1_English_Language_Set_2.pdf probationary
emailjagmeetsingh9
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
ElishamichaelSamwel
IGGE1 Understanding the Self1234567891011
rhea22labucay
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
NielDestora
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
S LS
A systematic review of self-coping strategies used by university students to ...
CleeveMoralde1
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
Medpopz
Cell Types and Its function , kingdom of life
ClaireMangundayao1
Final Presentation General Medicine 03-08-2024.pptx
ZaheerAhmad228692
Weekly quiz Compilation Jan -July 25.pdf
Nitin Suresh
What if we spent less time fighting change, and more time building what’s rig...
Derek Wenmoth
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
kawisojames10
Hazard Identification & Risk Assessment .pdf
estagiarioprotegesms
Computing-Curriculum for Schools in Ghana
abigailanane203
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
EricaRamos80
RMMM.pdf make it easy to upload and study
sajedul2
Final Presentation General Medicine 03-08-2024.pptx
ZaheerAhmad228692
chapter 7: DATA TYPES - IN PROGRAMMING LANGUAGES
1.
Copyright © 2009
Elsevier Chapter 7:: Data Types Programming Language Pragmatics Michael L. Scott
2.
Copyright © 2009
Elsevier Data Types • We all have developed an intuitive notion of what types are, but what's behind the intuition? – collection of values from a "domain" (the denotational approach) – internal structure of a bunch of data, described down to the level of a small set of fundamental types (the structural approach) – equivalence class of objects (the implementor's approach) – collection of well-defined operations that can be applied to objects of that type (the abstraction approach)
3.
Copyright © 2009
Elsevier Data Types • Denotational are a more formal, mathematical view of things. • We generally combine a structural approach with the abstraction approach in object oriented programming: a set of data plus operations defined on that data.
4.
Copyright © 2009
Elsevier Data Types • What are types good for? – implicit context – checking - make sure that certain meaningless operations do not occur • type checking cannot prevent all meaningless operations • but it catches enough of them to be useful • Polymorphism results when the compiler finds that it doesn't need to know certain things
5.
Copyright © 2009
Elsevier Data Types • STRONG TYPING has become a popular buzz-word – like structured programming – informally, it means that the language prevents you from applying an operation to data on which it is not appropriate • STATIC TYPING means that the compiler can do all the checking at compile time
6.
Copyright © 2009
Elsevier Type Systems • Examples – Pascal is almost statically typed – Java is strongly typed, with a non-trivial mix of things that can be checked statically and things that have to be checked dynamically – C is not strongly typed, but it is statically typed – Python is strongly typed, but is dynamic and not static
7.
Copyright © 2009
Elsevier Type Systems • Common terms: – discrete types – countable • integer • boolean • char • enumeration • subrange – Scalar types - one-dimensional • discrete • real
8.
Copyright © 2009
Elsevier Type Systems • Composite types: – records (or structures) – arrays • strings are perhaps the most useful here – sets – pointers – lists: defined recursively – files
9.
Copyright © 2009
Elsevier Type Systems • ORTHOGONALITY is a useful goal in the design of a language, particularly its type system – A collection of features is orthogonal if there are no restrictions on the ways in which the features can be combined (analogy to vectors)
10.
Copyright © 2009
Elsevier Type Systems • For example – Pascal is more orthogonal than Fortran, (because it allows arrays of anything, for instance), but it does not permit variant records as arbitrary fields of other records (for instance) • Orthogonality is nice primarily because it makes a language easy to understand, easy to use, and easy to reason about
11.
Copyright © 2009
Elsevier Type Checking • A TYPE SYSTEM has rules for – type equivalence (when are the types of two values the same?) – type compatibility (when can a value of type A be used in a context that expects type B?) – type inference (what is the type of an expression, given the types of the operands?)
12.
Copyright © 2009
Elsevier Type Checking • Type compatibility / type equivalence – Compatibility is the more useful concept, because it tells you what you can DO – The terms are often (incorrectly, but we do it too) used interchangeably.
13.
Copyright © 2009
Elsevier Type Checking • Certainly format does not matter: struct { int a, b; } is the same as struct { int a, b; } We certainly want them to be the same as struct { int a; int b; } Be
14.
Copyright © 2009
Elsevier Structural Equivalence • But should: struct { int a; int b; } • Be the same as: struct { int b; int a; } • Most languages say no, but some (like ML) say yes.
15.
Copyright © 2009
Elsevier Type Checking • Two major approaches: structural equivalence and name equivalence – Name equivalence is based on declarations – Structural equivalence is based on some notion of meaning behind those declarations – Name equivalence is more fashionable these days
16.
Copyright © 2009
Elsevier Name Equivalence • There are at least two common variants on name equivalence – The differences between all these approaches boils down to where you draw the line between important and unimportant differences between type descriptions – In all three schemes described in the book, we begin by putting every type description in a standard form that takes care of "obviously unimportant" distinctions like those above
17.
Copyright © 2009
Elsevier Name Equivalence • For example, the issue of aliasing. Should aliased types be considered the same? • Example: TYPE cel_temp = REAL; faren_temp = REAL; VAR c : cel_temp; f : faren_temp; … f := c; (* should this raise an error? *) • With strict name equivalence, the above raises an error. Loose name equivalence would allow it.
18.
Copyright © 2009
Elsevier Structural Equivalence • Structural equivalence depends on simple comparison of type descriptions substitute out all names – expand all the way to built-in types • Original types are equivalent if the expanded type descriptions are the same
19.
Copyright © 2009
Elsevier Type Casting • Most programming languages allow some form of type conversion (or casting), where the programmer can change one type of variable to another. • We saw a lot of this in Python: – Every print statement implicitly cast variables to be strings. We even coded this in our own classes. – Example from Point class: def __init__(self): return ‘<‘ + str(self._x) + ‘,’ + str(self._y) + ‘>’
20.
Copyright © 2009
Elsevier Type Casting • Coercion – When an expression of one type is used in a context where a different type is expected, one normally gets a type error – But what about var a : integer; b, c : real; ... c := a + b;
21.
Copyright © 2009
Elsevier Type Casting • Coercion – Many languages allow things like this, and COERCE an expression to be of the proper type – Coercion can be based just on types of operands, or can take into account expected type from surrounding context as well – Fortran has lots of coercion, all based on operand type
22.
Copyright © 2009
Elsevier Type Coercion • C has lots of coercion, too, but with simpler rules: – all floats in expressions become doubles – short int and char become int in expressions – if necessary, precision is removed when assigning into LHS
23.
Copyright © 2009
Elsevier Type Checking • In effect, coercion rules are a relaxation of type checking. – Recent thought is that this is probably a bad idea. – Languages such as Modula-2 and Ada do not permit coercions. – C++, however, goes crazy with them, and they can be one of the hardest parts of the language to understand.
24.
Copyright © 2009
Elsevier Type Checking • There are some hidden issues in type checking that we usually ignore. • For example, consider x + y. If one of them is a float and one an int, what happens? – Cast to float. What does this mean for performance? • What type of runtime checks need to be performed? Can they be done at compile time? Is precision lost? • In some ways, there are good reasons to prohibit coercion, since it allows the programmer to completely ignore these issues.
25.
Copyright © 2009
Elsevier Type Checking • Make sure you understand the difference between – type conversions (explicit) – type coercions (implicit) – sometimes the word 'cast' is used for conversions (C is guilty here)
26.
Copyright © 2009
Elsevier Records (Structures) and Variants (Unions) • Records – usually laid out contiguously – possible holes for alignment reasons – smart compilers may re-arrange fields to minimize holes (C compilers promise not to) – implementation problems are caused by records containing dynamic arrays • we won't be going into that in any detail
27.
Copyright © 2009
Elsevier Records (Structures) and Variants (Unions) • Unions (variant records) – overlay space – cause problems for type checking • Lack of tag means you don't know what is there • Ability to change tag and then access fields hardly better – can make fields "uninitialized" when tag is changed (requires extensive run-time support) – can require assignment of entire variant, as in Ada
28.
Copyright © 2009
Elsevier Records (Structures) and Variants (Unions) • Memory layout and its impact (structures) Figure 7.1 Likely layout in memory for objects of type element on a 32-bit machine. Alignment restrictions lead to the shaded “holes.”
29.
Copyright © 2009
Elsevier Records (Structures) and Variants (Unions) • Memory layout and its impact (structures) Figure 7.2 Likely memory layout for packed element records. The atomic_number and atomic_weight elds are nonaligned, and can only be read or written (on most machines) via fi multi- instruction sequences.
30.
Copyright © 2009
Elsevier Records (Structures) and Variants (Unions) • Memory layout and its impact (structures) Figure 7.3 Rearranging record elds to minimize holes. fi By sor ting elds according to fi the size of their alignment constraint, a compiler can minimize the space devoted to holes, while keeping the elds aligned. fi
31.
Copyright © 2009
Elsevier Records (Structures) and Variants (Unions) • Memory layout and its impact (unions) Figure 7.15 (CD) Likely memory layouts for element variants. The value of the naturally occurring eld (shown here with a double border) determines which of the interpretations of the remaining fi space is valid. Type string_ptr is assumed to be represented by a (four-byte) pointer to dynamically allocated storage.
32.
Copyright © 2009
Elsevier Arrays • Arrays are the most common and important composite data types, dating from Fortran • Unlike records, which group related fields of disparate types, arrays are usually homogeneous • Semantically, they can be thought of as a mapping from an index type to a component or element type
33.
Copyright © 2009
Elsevier Array requirements • Most languages require that the index be discrete; some (such as Fortran) require it to be an integer; some scripting languages even allow non-discrete types – With non-discrete, need some kind of mapping such as a hash table to get into the actual array • Generally, the element type can be anything, although some (again such as early Fortran) require it to be a scalar. • A slice or section is a rectangular portion of an array (See figure on next slide.) – Usually indicated with (i) (eg Fortran and Ada) or [i] (eg C or Pascal)
34.
Copyright © 2009
Elsevier Arrays Figure 7.4 Array slices(sections) in Fortran90. Much like the values in the header of an enumeration- controlled loop (Section6.5.1), a: b: c in a subscript indicates positions a, a+c, a+2c, ...through b. If a or b is omitted, the corresponding bound of the array is assumed. If c is omitted, 1 is assumed. It is even possible to use negative values of c in order to select positions in reverse order. The slashes in the second subscript of the lower right example delimit an explicit list of positions.
35.
Copyright © 2009
Elsevier • In C: char upper[26]; • In Pascal: Var upper : array [‘a’..’z’] of char; • In Fortran: character, dimension (1:26) :: upper character (26) upper ! shorthand • Note that in C, we start counting at 0, but that is NOT true in Fortran. Arrays: Declaration
36.
Copyright © 2009
Elsevier • Dimensions, Bounds, and Allocation – global lifetime, static shape — If the shape of an array is known at compile time, and if the array can exist throughout the execution of the program, then the compiler can allocate space for the array in static global memory – local lifetime, static shape — If the shape of the array is known at compile time, but the array should not exist throughout the execution of the program, then space can be allocated in the subroutine’s stack frame at run time. – local lifetime, shape bound at elaboration time Arrays
37.
Copyright © 2009
Elsevier Arrays Figure 7.6 Elaboration-time allocation of arrays in Ada or C99.
38.
Copyright © 2009
Elsevier Array layouts • Contiguous elements (see Figure 7.7) – column major: A[2,4] is followed by A[3,4] • only in Fortran • reason seems to be to accommodate idiosyncrasies of IBM computer it was created on – row major: so A[2,4] is followed by A[2,5] in memory • used by everybody else • makes array [a..b, c..d] the same as array [a..b] of array [c..d]
39.
Copyright © 2009
Elsevier Arrays Figure7.7 Row- and column-major memory layout for two-dimensional arrays. In row-major order, the elements of a row are contiguous in memory; in column-major order, the elements of a column are contiguous. The second cache line of each array is shaded, on the assumption that each element is an eight-byte oating-point number, that fl cache lines are 32 bytes long (a common size), and that the array begins at a cache line boundary. If the array is indexed from A[0,0] to A[9,9], then in the row-major case elements A[0,4] through A[0,7] share a cache line; in the column-major case elements A[4,0] through A[7,0] share a cache line.
40.
Copyright © 2009
Elsevier Array layouts: why we care • Layout makes a big difference for access speed - one trick in high performance computing is simply to set up your code to go in row major order • (Bad) Example: for i = 1 to n for j = 1 to n A[j][i] = value
41.
Copyright © 2009
Elsevier Arrays • Two layout strategies for arrays (see next slide): – Contiguous elements – Row pointers • Row pointers – an option in C – allows rows to be put anywhere - nice for big arrays on machines with segmentation problems – avoids multiplication – nice for matrices whose rows are of different lengths • e.g. an array of strings – requires extra space for the pointers
42.
Copyright © 2009
Elsevier Arrays Figure 7.8 Contiguous array allocation v. row pointers in C. The declaration on the left is a true two- dimensional array. The slashed boxes are NUL bytes; the shaded areas are holes. The declaration on the right is a ragged array of pointers to arrays of character s. In both cases, we have omitted bounds in the declaration that can be deduced from the size of the initializer (aggregate). Both data structures permit individual characters to be accessed using double subscripts, but the memory layout (and corresponding address arithmetic) is quite different.
43.
Copyright © 2009
Elsevier Arrays: Address calculations • Example: Suppose we have a 3d array: A : array [L1..U1] of array [L2..U2] of array [L3..U3] of elem; D1 = U1-L1+1 D2 = U2-L2+1 D3 = U3-L3+1 Let S3 = size of elem S2 = D3 * S3 (* size of a row *) S1 = D2 * S2 (* size of a plane *) • Then calculating A[i,j,k] each time is: (Address of A) + (i-L1)*S1 + (j-L2)*S2 + (k-L3)*S3
44.
Copyright © 2009
Elsevier Arrays Figure 7.9 Virtual location of an array with nonzero lower bounds. By computing the constant portions of an array index at compile time, we effectively index into an array whose starting address is offset in memory, but whose lower bounds are all zero.
45.
Copyright © 2009
Elsevier Arrays • Example (continued) We could compute all that at run time, but we can make do with fewer subtractions: == (i * S1) + (j * S2) + (k * S3) + address of A - [(L1 * S1) + (L2 * S2) + (L3 * S3)] The stuff in square brackets is compile-time constant that depends only on the type of A
46.
Copyright © 2009
Elsevier Strings • Strings are really just arrays of characters • They are often special-cased, to give them flexibility (like polymorphism or dynamic sizing) that is not available for arrays in general – It's easier to provide these things for strings than for arrays in general because strings are one-dimensional and (more important) non- circular
47.
Copyright © 2009
Elsevier Sets • We learned about a lot of possible implementations • Bit sets are what usually get built into programming languages • Things like intersection, union, membership, etc. can be implemented efficiently with bitwise logical instructions • Some languages place limits on the sizes of sets to make it easier for the implementor, since even bit vectors get large for large sets – There is really no need for this, since generally these sets are sparse, so can use hash tables to implement effectively.
48.
Copyright © 2009
Elsevier Pointers And Recursive Types • Pointers serve two purposes: – efficient (and sometimes intuitive) access to elaborated objects (as in C) – dynamic creation of linked data structures, in conjunction with a heap storage manager • Several languages (e.g. Pascal) restrict pointers to accessing things in the heap • Pointers are used with a value model of variables – They aren't needed with a reference model, like Python. Why?
49.
Copyright © 2009
Elsevier Pointers And Recursive Types Figure 7.11 Implementation of a tree in Lisp. A diagonal slash through a box indicates a null pointer. The C and A tags serve to distinguish the two kinds of memory blocks: cons cells and blocks containing atoms.
50.
Copyright © 2009
Elsevier Pointers And Recursive Types Figure 7.12 Typical implementation of a tree in a language with explicit pointers. As in Figure 7.11, a diagonal slash through a box indicates a null pointer.
51.
Copyright © 2009
Elsevier Pointers And Recursive Types in C • C pointers and arrays int *a == int a[] int **a == int *a[] • BUT equivalences don't always hold – Specifically, a declaration allocates an array if it specifies a size for the first dimension – otherwise it allocates a pointer int **a, int *a[]; pointer to pointer to int int *a[n]; n-element array of row pointers int a[n][m]; 2-d array
52.
Copyright © 2009
Elsevier Pointers And Recursive Types in C • Given this initialization: int n; int *a; int b[10]; a = b; • Note that the following are equivalent: n = a[3]; n = *(a+3);
53.
Copyright © 2009
Elsevier Pointers And Recursive Types • Compiler has to be able to tell the size of the things to which you point – So the following aren't valid: int a[][] bad int (*a)[] bad – C declaration rule: read right as far as you can (subject to parentheses), then left, then out a level and repeat int *a[n], n-element array of pointers to integer int (*a)[n], pointer to n-element array of integers
54.
Copyright © 2009
Elsevier Pointers And Recursive Types • Problems with dangling pointers are due to – explicit deallocation of heap objects • only in languages that have explicit deallocation – implicit deallocation of elaborated objects • Two implementation mechanisms to catch dangling pointers – Tombstones – Locks and Keys
55.
Copyright © 2009
Elsevier Pointers And Recursive Types Figure 7.17 (CD) Tombstones. A valid pointer refers to a tombstone that in turn refers to an object. A dangling reference refers to an “expired” tombstone.
56.
Copyright © 2009
Elsevier Pointers And Recursive Types Figure 7.18 (CD) Locks and Keys. A valid pointer contains a key that matches the lock on an object in the heap. A dangling reference is unlikely to match.
57.
Copyright © 2009
Elsevier Pointers And Recursive Types • Problems with garbage collection – many languages leave it up to the programmer to design without garbage creation - this is VERY hard – others arrange for automatic garbage collection – reference counting • does not work for circular structures • works great for strings • should also work to collect unneeded tombstones
58.
Copyright © 2009
Elsevier Pointers And Recursive Types • Garbage collection with reference counts Figure 7.13 Reference counts and circular lists. The list shown here cannot be found via any program variable, but because it is circular, every cell contains a nonzero count.
59.
Copyright © 2009
Elsevier Pointers And Recursive Types • Mark-and-sweep – commonplace in Lisp dialects – complicated in languages with rich type structure, but possible if language is strongly typed – achieved successfully in Cedar, Ada, Java, Modula-3, ML – complete solution impossible in languages that are not strongly typed – conservative approximation possible in almost any language (Xerox Portable Common Runtime approach)
60.
Copyright © 2009
Elsevier Pointers And Recursive Types Figure 7.14 Heap exploration via pointer reversal.
61.
Copyright © 2009
Elsevier Lists • A list is defined recursively as either the empty list or a pair consisting of an object (which may be either a list or an atom) and another (shorter) list – Lists are ideally suited to programming in functional and logic languages • In Lisp, in fact, a program is a list, and can extend itself at run time by constructing a list and executing it – Lists can also be used in imperative programs
62.
Copyright © 2009
Elsevier Files and Input/Output • Input/output (I/O) facilities allow a program to communicate with the outside world – interactive I/O and I/O with files • Interactive I/O generally implies communication with human users or physical devices • Files generally refer to off-line storage implemented by the operating system • Files may be further categorized into – temporary – persistent
Download