ISBN 0-321-19362-8 Chapter 6 Data Types Pointer Types Reference Types Memory Management.

Slides:



Advertisements
Similar presentations
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case.
Advertisements

Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Chapter 6 Data Types
Names and Bindings.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
CS 355 – Programming Languages
ISBN Lecture 06 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Lecture 06 Topics Introduction Primitive Data.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
ISBN Lecture 05 Variable Attributes.
Copyright © 1995 by Addison-Wesley Publishing Co. 1 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case sensitive?
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 13 / 2007 Instructor: Michael Eckmann.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
Primitive Data Types: Numbers Strings Ordinal Types Pointers
Names and Bindings Introduction Names Variables The concept of binding Chapter 5-a.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
College of Computer Science and Engineering
ISBN 0-321— Chapter 6 Data Types. Corrected and improved by Assoc. Prof. Zeki Bayram, EMU, North Cyprus. Original Copyright © 2007 Addison-Wesley.
ISBN 0-321— Chapter 6 sections 1-4, 9 Primitive Data Types Numbers Strings Ordinal Types Pointers.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
ISBN Chapter 5 Names, Bindings, and Scopes.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
Basic Semantics Associating meaning with language entities.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
CS 330 Programming Languages 11 / 11 / 2008 Veterans Day Instructor: Michael Eckmann.
ISBN Chapter 6 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Chapter 6 Topics Introduction Primitive Data.
CS 330 Programming Languages 10 / 30 / 2007 Instructor: Michael Eckmann.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
C H A P T E R S I X Data Types.
CS 363 Comparative Programming Languages Data Types.
1 CS Programming Languages Class 09 September 21, 2000.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.
Chapter 6 © 2002 by Addison Wesley Longman, Inc Introduction - Evolution of Data Types: FORTRAN I (1957) - INTEGER, REAL, arrays … Ada (1983) -
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
ISBN Chapter 10 Implementing Subprograms.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.2 Primitive Data Types Almost all programming languages.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 5 Evolution of Data Types: FORTRAN I (1956) - INTEGER, REAL, arrays … Ada (1983) - User can.
Names, Scope, and Bindings Programming Languages and Paradigms.
Chapter 6 Chapter 6 Data Types. Data Types  A data type defines  a collection of data objects, and  a set of predefined operations on the objects type:
ISBN Chapter 12 Support for Object-Oriented Programming.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
Chapter 6 – Data Types CSCE 343.
CMP 339/692 Programming Languages Day 14 Tuesday, March 20, 2012
Chapter 6 Data Types.
6.1 Introduction 6.2 Primitive Data Types - Evolution of Data Types:
Records Design Issues: 1. What is the form of references?
Concepts of programming languages
Concepts of programming languages
Chapter 6 Data Types.
Complex Data Types One very important measure of the “goodness” of a PL is the capability of its data types to model the problem space variables Design.
Chapter 6 Data Types.
Presentation transcript:

ISBN Chapter 6 Data Types Pointer Types Reference Types Memory Management

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Pointers and Reference Types For a pointer type, range of values is memory addresses and nil. Uses –Indirect addressing –Dynamic storage management

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-3 Design Issues Scope and lifetime of pointer Lifetime of heap dynamic variable Type checking What uses are allowed Support pointer or reference types

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-4 Pointer Operations Assignment –Need to store an address in the variable –Generally part of the allocation process for heap- dynamic variables Address of operation needed if indirect addressing is used Dereferencing –Get the data being pointed to –Can be explicit or implicit

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-5 Pointer Problems 1.Dangling pointers (dangerous) 2.Lost Heap-Dynamic Variables ( wasteful) 1.aka garbage

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-6 Dangling pointers A pointer points to a heap-dynamic variable that has been deallocated Creating one (with explicit deallocation): a)Allocate a heap-dynamic variable and set a pointer to point at it b)Set a second pointer to the value of the first pointer c)Deallocate the heap-dynamic variable, using the first pointer

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-7 Lost Heap-Dynamic Variables A heap-dynamic variable that is no longer referenced by any program pointer Creating one: a)Pointer p1 is set to point to a newly created heap- dynamic variable b)p1 is later set to point to another newly created heap-dynamic variable The process of losing heap-dynamic variables is called memory leakage

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-8 Pointers in Pascal used for dynamic storage management only –Explicit dereferencing (postfix ^ ) –Dangling pointers are possible ( dispose ) –Dangling objects are also possible

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-9 Pointers in Ada A little better than Pascal –Some dangling pointers are disallowed because dynamic objects can be automatically deallocated at the end of pointer's type scope –All pointers are initialized to null –Similar dangling object problem (but rarely happens, because explicit deallocation is rarely done)

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-10 Pointers in C and C++ Used for dynamic storage management and addressing Explicit dereferencing and address-of operator Domain type need not be fixed ( void * ) –void * - Can point to any type and can be type checked (cannot be dereferenced)

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-11 Pointers in C and C++ (continued) Can do address arithmetic in restricted forms, e.g.: 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] (Implicit scaling)

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-12 Pointers The assignment operation j = *ptr

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-13 Pointers in FORTRAN 90 Can point to heap and non-heap variables Implicit dereferencing Pointers can only point to variables that have the TARGET attribute The TARGET attribute is assigned in the declaration, as in: INTEGER, TARGET :: NODE

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-14 Pointers FORTRAN 90 Pointers (continued) A special assignment operator is used for non- dereferenced references, e.g.: REAL, POINTER :: ptr ( POINTER is an attribute) ptr => target (where target is either a pointer or a non- pointer with the TARGET attribute) This sets ptr to have the same value as target

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-15 C++ Reference Types Constant pointers that are implicitly dereferenced –Used for parameters Advantages of both pass-by-reference and pass-by- value

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-16 Pointers in Java Only references –No pointer arithmetic –Can only point at objects (which are all on the heap) –No explicit deallocator (garbage collection is used) –Means there can be no dangling references –Dereferencing is always implicit

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-17 Evaluation of Pointers 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

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-18 Pointer Implementation Representation of pointers and references –Large computers use single values –Intel microprocessors use segment and offset

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-19 Memory Management Avoiding dangling pointer problems 1.Tombstone: extra heap cell that is a pointer to the heap-dynamic variable The actual pointer variable points only at tombstones When heap-dynamic variable deallocated, tombstone remains but set to nil 2.Locks and keys: Pointer values are represented as (key, address) pairs Heap-dynamic variables are represented as variable plus cell for integer lock value When heap-dynamic variable allocated, lock value is created and placed in lock cell and key cell of pointer

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-20 Tombstones

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-21 Heap management How do we avoid the problem of dangling pointers and garbage? Issues –Single-size cells vs. variable-size cells –Reference counters (eager approach) vs. garbage collection (lazy approach)

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-22 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

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-23 Garbage collection Allocate and disconnect until all available cells allocated; then begin gathering all garbage –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)

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-24 Marking Algorithm