Spring, 2011 –– Computational Thinking – Dennis Kafura – CS 2984 Data Structures Mapping complex structures to linear memory.

Slides:



Advertisements
Similar presentations
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Advertisements

Dynamic memory allocation
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Dynamic Memory Management
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Dynamic Memory Allocation I Topics Simple explicit allocators Data structures Mechanisms Policies CS 105 Tour of the Black Holes of Computing.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
Recitation 7 – 3/5/01 Outline Control Flow Memory Allocation –Lab 3 Details Shaheen Gandhi Office Hours: Wednesday.
Lecture 21 Dynamic Memory Allocation:- *Allocation of objects in program ’ s heap _e.g. C ’ s malloc/free or Pascal ’ s new/dispose _e.g. C ’ s malloc/free.
Run-time organization  Data representation  Storage organization: –stack –heap –garbage collection Programming Languages 3 © 2012 David A Watt,
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
CP104 Introduction to Programming Structure II Lecture 32 __ 1 Data Type planet_t and Basic Operations Abstract Data Type (ADT) is a data type combined.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
Mark and Sweep Algorithm Reference Counting Memory Related PitFalls
CS 536 Spring Automatic Memory Management Lecture 24.
CSC321: Programming Languages 11-1 Programming Languages Tucker and Noonan Chapter 11: Memory Management 11.1 The Heap 11.2 Implementation of Dynamic Arrays.
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.
Memory Management Chapter  Memory management: the process of binding values to (logical) memory locations.  The memory accessible to a program.
CS 61C L07 More Memory Management (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
C and Data Structures Baojian Hua
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
Memory Layout C and Data Structures Baojian Hua
CS61C L07 More Memory Management (1) Garcia, Spring 2008 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
CS61C Midterm 1 Review Summer 2004 Pooya Pakzad Ben Huang Navtej Sadhal.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CS61C L07 More Memory Management (1) Garcia, Spring 2010 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
CSC 533: Programming Languages Spring 2016
Garbage Collection What is garbage and how can we deal with it?
CSC 533: Programming Languages Spring 2015
CSE 374 Programming Concepts & Tools
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Dynamic Memory Allocation
Storage Management.
malloc(): Dynamic Memory Management
Concepts of programming languages
Lecture 4: Process Memory Layout
Arrays and Linked Lists
Memory Management and Garbage Collection Hal Perkins Autumn 2011
Simulated Pointers.
Memory Allocation CS 217.
Hassan Khosravi / Geoffrey Tien
Review & Lab assignments
Dynamic Memory.
List Allocation and Garbage Collection
Lecturer PSOE Dan Garcia
Comp 208 Computers in Engineering Yi Lin Fall, 2005
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
CSC 533: Programming Languages Spring 2019
Run-time environments
Garbage Collection What is garbage and how can we deal with it?
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

Spring, 2011 –– Computational Thinking – Dennis Kafura – CS 2984 Data Structures Mapping complex structures to linear memory

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Computer memory A0B 0C0D0E0F address value

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Mapping a binary tree to memory Null305Null A0B 1650E1 0C0D0E0F1011 Null A

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 What tree is this? A0D3NULL A0B NULL C0D0E0F1011 NULL NULL NULL9 2

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Ideas Using an address to refer to a value by the value’s location in memory: Using adjacency to create relationship AA

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 “C” syntax int a, b; int* aPointer; a = 6; aPointer = &a; b = *aPointer; aPointer a 6 b 6

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 “C” syntax struct Node { Node* left; int number; Node* right; }; Nodes node1, node2, node3; right < >< > left <> number node1

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 “C” syntax struct Node { Node* left; int number; Node* right; } Node node1, node2, node3; node1.number = 11; node1.left = NULL; node1.right = NULL; right NULL left NULL number 11 node1

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 “C” syntax struct Node { Node* left; int number; Node* right; } Node node1, node2, node3; node2.number = 9; node2.right = &node1; node2.left = NULL; right NULL left NULL number 11 node1 rightleft NULL number 9 node2

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Dynamic memory allocation Q: Do all the nodes have to be allocated in advance? A: No. They can be dynamically allocated. Node* nodex; nodex = malloc (sizeof (Node)); right left number nodex Note: there is no “name” for the allocated space; only a pointer to the beginning of the space.

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Using dynamic memory Node* nodex; nodex = malloc( sizeof(Node)); (*nodex).number = 4; (*nodex).left = NULL; (*nodex).right = NULL; right NULL left NULL number 4 nodex

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Dynamically created tree right left NULL number 3 right NULL left NULL number 11 right NULL left NULL number 1 right NULL left NULL number 4 rightleft NULL number 9 rightleftnumber 2 rightleftnumber 5 bTree

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Traversal In the dynamically created tree, what would you write to assign to “r” the value in the box assuming “bTree” points to the element containing the value “5”? struct Node { Node* left; int number; Node* right; }; Node* bTree; int r;

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Types of storage Stack (local) storage  Lifetime: only during execution of function/method  Allocation/deallocation: automatic  Problem: violation of lifetime Heap (global) storage  Lifetime: indefinite  Allocation/deallocation: programmed using malloc and free  Problem: memory leaks (memory allocated and not deallocated)

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Stack storage

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Violation of lifetime

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Heap storage allocation: void* malloc (unsigned long size); deallocation: void free (void* block);

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Heap storage

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Heap storage

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Automatic storage management Memory mangement problems  easy to cause  difficult to debug unusual failure modes may be difficult to know what component is responsible for deallocating memory Solutions  C/C++ : tools and libraries for “safe” memory management or debugging assistance  Java: automatic storage management (garbage collection)

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Garbage collection Goal: automatically detect and reclaim allocated but unusable memory Basic approaches  Mark-and-sweep  Copying collectors Costs  Overhead of garbage collector  Acceptable in most cases (esp. in light of advantages)

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Mark and sweep Basic algorithm  Starting from the program variables, mark all memory blocks encountered  Sweep through all memory block in the heap and reclaim the unmarked ones  Unmark all marked memory blocks

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Copying collector Basic algorithm  Divide memory into two regions  Begin allocating from region 1  When region 1 is full Copy all usable blocks from region 1 to region 2 Interchange roles for region 1 and region 2

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Memory hierarcy

Spring, 2011 – Computational Thinking – Dennis Kafura – CS 2984 Memory hierarchy Why does this matter? To algorithm design? MemoryAccess Time Normalized Human Location Register 1 nsec 1 secon desk Cache 20 nsec 20 secin room Main Memory 50 nsec 1 minutenext door Disk10 msec100 daysoff planet