CSC 533: Programming Languages Spring 2019

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Dynamic Memory Management
PZ10B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10B - Garbage collection Programming Language Design.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Run-time organization  Data representation  Storage organization: –stack –heap –garbage collection Programming Languages 3 © 2012 David A Watt,
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
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.
1 CSC 533: Organization of Programming Languages Spring 2005 Data types  primitive types (integer, float, boolean, char, pointer)  heap management, garbage.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Primitive Data Types: Numbers Strings Ordinal Types Pointers
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
SPL – Practical Session 2 Topics: – C++ Memory Management – Pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Pointers and Dynamic Memory Allocation. Declaring a pointer.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 CSC 533: Programming Languages Spring 2012 Data types  primitive types (integer, float, boolean, char, pointer)  heap management, garbage collection.
Copyright Curt Hill Variables What are they? Why do we need them?
Heap storage & Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001.
ISBN Chapter 6 Data Types Pointer Types Reference Types Memory Management.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 CSC 533: Programming Languages Spring 2014 Data types  primitive types (integer, float, boolean, char, pointer)  heap management, garbage collection.
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Records type city is record -- Ada Name: String (1..10); Country : String (1..20); Population: integer; Capital : Boolean; end record; struct city { --
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Pointers and Dynamic Memory Allocation
Yan Shi CS/SE2630 Lecture Notes
Dynamic Allocation in C
CSC 533: Programming Languages Spring 2016
Object Lifetime and Pointers
Dynamic Storage Allocation
Data Types In Text: Chapter 6.
Computer Organization and Design Pointers, Arrays and Strings in C
CSC 533: Organization of Programming Languages Spring 2010
CSC 533: Programming Languages Spring 2015
Chapter 6 – Data Types CSCE 343.
Chapter 6: Data Types Lectures # 10.
Dynamic Memory Allocation
Concepts of programming languages
C Basics.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
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.
Object Oriented Programming COP3330 / CGS5409
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Heap storage & Garbage collection
Data Structures & Algorithms
Heap storage & Garbage collection
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

CSC 533: Programming Languages Spring 2019 Data types primitive types (integer, float, boolean, char, pointer) heap management reference counts vs. garbage collection partition & copy vs. mark & sweep We will focus on C, C++, and Java as example languages

Primitive types: integer languages often provide several sizes/ranges in C/C++/Java short (2 bytes in Java) int (4 bytes in Java) long (8 bytes in Java) absolute sizes are implementation dependent in C/C++ TRADEOFFS? Java has a byte type (1 byte) in C/C++, char is considered an integer type most languages use 2’s complement notation for negatives 1 = 00…000001 -1 = 11…111111 2 = 00…000010 -2 = 11…111110 3 = 00…000011 -3 = 11…111101

Primitive types: floating-point again, languages often provide several sizes/ranges in C/C++/Java float (4 bytes in Java) double (8 bytes in Java) C/C++ also have a long double type historically, floating-points have been stored in a variety of formats same basic components: sign, fraction, exponent in 1985, IEEE floating-point formats were standardized (sign)fraction x 2exponent special bit patterns represent: infinity NaN other number types: decimal, fixed-point, rational, …

Primitive types: Boolean introduced in ALGOL 60 C does not have a boolean type, conditionals use zero (false) and nonzero (true) C++ has bool type really just syntactic sugar, automatic conversion between int and bool Java has boolean type no conversions between int and boolean implementing Booleans could use a single bit, but not usually accessible use smallest easily-addressable unit (e.g., byte)

Primitive types: character stored as numeric codes, e.g., ASCII (C/C++) or UNICODE (Java) in C/C++, char is an integer type can apply integer operations, mix with integer values char ch = ’A’; char ch = ’8’; ch = ch + 1; int d = ch – ’0’; cout << ch << endl cout << d << endl; in Java, char to int conversion is automatic but must explicitly cast int to char char next = (char)(ch + 1);

Primitive types: pointer a pointer is nothing more than an address (i.e., an integer) useful for: dynamic memory management (allocate, dereference, deallocate) indirect addressing (point to an address, dereference) PL/I was the first language to provide pointers pointers were not typed, could point to any object  no static type checking for pointers ALGOL 68 refined pointers to a specific type in many languages, pointers are limited to dynamic memory management e.g., Pascal, Ada, Java, …

Primitive types: pointer (cont.) C/C++ allows for low-level memory access using pointers * dereferencing operator & address-of operator int x = 6; int * ptr1 = &x; int * ptr2 = ptr1; *ptr2 = 3; in C/C++, the 0 (NULL) address is reserved, attempted access  ERROR Java does not provide explicit pointers, but every object is really a pointer String str = “foo”;

Heap management pointers access memory locations from the heap (a dynamically allocated storage area) the heap is divided into equal-size cells, each with a pointer the pointer fields are used initially to organize the heap as a linked list keep pointer to head of free list to allocate space, take from front of free list to deallocate, put back at front

Heap example String str1 = "foo"; String str2 = "bar"; HEAD OF FREE . stack heap str1 str2 "foo" "bar" Heap example HEAD OF FREE . stack heap str1 str2 "foo" "bar" String str1 = "foo"; String str2 = "bar"; /* CHECKPOINT 1 */ str1 = str2; /* CHECKPOINT 2 */ String str3 = str1+"n"; /* CHECKPOINT 3 */ HEAD OF FREE . stack heap str1 str2 "barn" "bar" str3

Pointer problems returning memory to the free list is easy, but when do you do it? dangling reference: memory is deallocated, but still have a pointer to it int * Foo() { a problem in C/C++, since the & operator allows access to stack int x = 5; memory that has already been reclaimed return &x; } not a problem in Java since no equivalent to the & operator garbage reference: pointer is destroyed, but memory has not been deallocated void Bar() { a problem in both C/C++ and Java Date today = new Date(); when today's lifetime ends, its dynamic memory is inaccessible … (in C/C++, must explicitly deallocate dynamic memory w/ delete) } would like to automatically and safely reclaim heap memory 2 common techniques: reference counts, garbage collection

Reference counts along with each heap element, store a reference count indicates the number of pointers to the heap element when space is allocated, its reference count is set to 1 each time a new pointer is set to it, increment the reference count each time a pointer is lost, decrement the reference count provides a simple method for avoiding garbage & dangling references if result of an operation leaves reference count at 0, reclaim memory can even double check explicit deallocations

Reference counts example String str1 = "foo"; String str2 = "bar"; /* CHECKPOINT 1 */ str1 = str2; /* CHECKPOINT 2 */ if (str1.equals(str2)) { String temp = "biz"; str2= temp; /* CHECKPOINT 3 */ } String str3 = "baz"; /* CHECKPOINT 4 */ HEAD OF FREE HEAP STACK stack ptr ????

Reference counts (cont.) unfortunately, reference counts are very costly must update & check reference counts for each assignment, end of lifetime String str1; String str2; … str1 = str2;  1) dereference str1, decrement count 2) if count = 0, deallocate 3) copy str1 reference to str2 4) dereference str1, increment count reference counts are popular in parallel programming work is spread evenly

Garbage collection approach: allow garbage to accumulate, only collect if out of space as program executes, no reclamation of memory (thus, no cost) when out of memory, take the time to collect garbage (costly but rare) e.g., toothpaste tube analogy 2 common approaches to garbage collection Partition & Copy Mark & Sweep

Partition & Copy approach divide the memory space into 2 partitions: current + backup when the current partition is full, sweep through all active objects (from the stack) copy each active object to the backup partition (contiguously) when done, make that the current partition HEAP CURRENT PARTITION BACKUP STACK HEAP BACKUP PARTITION CURRENT STACK when current partition is full copy to backup & make it current

Partition & Copy example String str1= "foo"; String str2= "bar"; /* CHECKPOINT 1 */ str1 = str2; /* CHECKPOINT 2 */ if (str1.equals(str2)) { String temp = "biz"; str2 = temp; /* CHECKPOINT 3 */ } String str3 = "baz"; /* CHECKPOINT 4 */ HEAD OF FREE stack ptr ???? ???? STACK HEAP

Mark & Sweep approach mark all active objects sweep through all active objects (from the stack) mark each memory cell associated with an active object sweep through the heap and reclaim unmarked cells traverse the heap sequentially add each unmarked cell to the FREE list HEAP STACK FREE HEAD HEAP STACK FREE HEAD ACTIVE HEAP STACK FREE HEAD ACTIVE traverse the stack and mark active objects traverse the heap and reclaim unmarked cells

Mark & Sweep example String str1= "foo"; String str2= "bar"; /* CHECKPOINT 1 */ str1 = str2; /* CHECKPOINT 2 */ if (str1.equals(str2)) { String temp = "biz"; str2 = temp; /* CHECKPOINT 3 */ } String str3 = "baz"; /* CHECKPOINT 4 */ HEAD OF FREE HEAP STACK stack ptr ????

Mark & Sweep & Compactify note: not all memory allocations are the same size C/C++/Java: double bigger than float, array elements must be contiguous, … HEAD OF FREE heap ACTIVE as memory is allocated & deallocated, fragmentation occurs e.g., suppose wish to allocate a 3 element array previous allocations/deallocations have left 3 free cells, but not contiguously  must garbage collect (even though free space exists) using Partition & Copy, not a big problem simply copy active objects to other partition – this automatically coalesces gaps using Mark & Sweep, must add another pass to defragment the space once active objects have been identified, must shift them in memory to remove gaps COSTLY!

Partition & Copy vs. Mark & Sweep & Compactify wastes memory by maintaining the backup partition but quick (especially if few active objects) and avoids fragmentation Mark & Sweep & Compactify able to use the entire memory space for active objects but slow (2 complete passes through heap to reclaim and compactify) Java takes a hybrid approach to provide automatic garbage collection memory is divided into two types: new objects and old objects the new objects partition is optimized for objects with short lifetimes garbage collection happens relatively frequently uses Partition & Copy, since it is expected that few active objects will remain eventually, persistent new objects are moved to the old objects partition garbage collections happens relatively infrequently uses Mark & Sweep & Compactify, since many active objects will persist