Memory Management. CS31 Pascal Van Hentenryck CS031. Lecture 19 1

Similar documents
In Java we have the keyword null, which is the value of an uninitialized reference type

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

BBM 201 DATA STRUCTURES

Class Information ANNOUCEMENTS

CS 11 C track: lecture 5

Lecture 8 Dynamic Memory Allocation

Dynamic Memory Management

Dynamic Allocation of Memory

Lecture Notes on Memory Management

[0569] p 0318 garbage

Lecture Notes on Memory Management

CS201- Introduction to Programming Current Quizzes

Dynamic Memory Allocation and Linked Lists

CS 161 Exam II Winter 2018 FORM 1

Dynamic Memory Allocation II October 22, 2008

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Dynamic Allocation of Memory

ECE 15B COMPUTER ORGANIZATION

Dynamic memory allocation (malloc)

CA341 - Comparative Programming Languages

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Dynamic Data Structures. CSCI 112: Programming in C

o Code, executable, and process o Main memory vs. virtual memory

Heap Arrays and Linked Lists. Steven R. Bagley

Low-Level C Programming. Memory map Pointers Arrays Structures

Dynamic Memory Management! Goals of this Lecture!

Dynamic memory. EECS 211 Winter 2019

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

CS24 Week 2 Lecture 1

Understanding Pointers

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Internal Fragmentation

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

CS 2630 Computer Organization. Meeting 10/11: data structures in MIPS Brandon Myers University of Iowa

CSC 1600 Memory Layout for Unix Processes"

Implementing Abstractions

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

CSCI-UA /2. Computer Systems Organization Lecture 19: Dynamic Memory Allocation: Basics

Dynamic Data Structures (II)

CSCI 171 Chapter Outlines

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

Pointers, Dynamic Data, and Reference Types

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

CS61 Section Notes. Section 5 (Fall 2011) Topics to be covered Common Memory Errors Dynamic Memory Allocation Assignment 3: Malloc

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

Short Notes of CS201

Memory Allocation III

However, in C we can group related variables together into something called a struct.

Arrays and Memory Management

Kurt Schmidt. October 30, 2018

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Dynamic Memory Allocation

CS201 - Introduction to Programming Glossary By

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

Structures and Pointers

CS 33. Intro to Storage Allocation. CS33 Intro to Computer Systems XXVI 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Pointers, Arrays, Memory: AKA the cause of those Segfaults

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

Memory and C++ Pointers

Pointers. Memory. void foo() { }//return

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Dynamic Memory Allocation: Advanced Concepts

Run Time Environment

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011


CS61, Fall 2012 Section 2 Notes

Data Structures and Algorithms for Engineers

Dynamic Allocation in C

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

16. Dynamic Data Structures

CSE 307: Principles of Programming Languages

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

CS32 Discussion Week 3

CSE351 Winter 2016, Final Examination March 16, 2016

CS558 Programming Languages

Midterm Review. CS 211 Fall 2018

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

CMSC 341 Lecture 2 Dynamic Memory and Pointers

Heap Arrays. Steven R. Bagley

DAY 3. CS3600, Northeastern University. Alan Mislove

CS 241 Data Organization Binary Trees

EL2310 Scientific Programming

Lecture 2, September 4


COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

COSC Software Engineering. Lectures 14 and 15: The Heap and Dynamic Memory Allocation

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

ALL ABOUT POINTERS C/C++ POINTERS

Run-time Environments - 3

Today s lecture. Pointers/arrays. Stack versus heap allocation CULTURE FACT: IN CODE, IT S NOT CONSIDERED RUDE TO POINT.

Transcription:

Memory Management CS31 Pascal Van Hentenryck CS031 Lecture 19 1

Memory Management What is it? high-level languages abstract many aspects of memory management Support varies with the language Java (ML/Prolog/Lisp/Smalltalk) C++ C Highest level of abstraction Explicit destruction of objects High-level assembly language CS031 Lecture 19 2

Memory Management Two types of memory allocation Stack allocation Heap allocation Stack allocation Parameters Return address Local variables Heap Allocation Objects (Java, C++) Dynamic data in C Nothing to do with the heap data structure: it is a real heap CS031 3

Memory Management 3 types of memory allocation Stack allocation Heap allocation Static allocation Stack allocation Parameters & Local variables Heap Allocation Objects (Java, C++) Static Allocation Static data in your program CS031 4

MM in Java Memory allocation new allocates memory for an object Memory deallocation No explicit deallocation Objects are deallocated whenever they are not needed How do you know that? Garbage collection CS031 5

Simple Lists Conventions A list is a pointer to a list node A null reference/pointer indicates an empty list List Node A key (e.g., an int) A pointer to the next element in the list The pointer/reference is null when there are no further element Close to lists in Lisp/Scheme CS031 6

Simple Lists in Java class List { ListNode _head; List() { _head = null; Class ListNode { int _key; ListNode _next; ListNode(int key,listnode n) { _key = key; _next = n; int getkey() { return _key; ListNode getnext() { return _next; void setnext(listnode n) { _next = n; CS031 7

MM in Java class List { ListNode _head; Memory List() { _head = null; allocation void insert(int key) { _head = new ListNode(key,_head); void remove(int key) { Class ListNode { int _key; ListNode _next; ListNode(int key,listnode n) { _key = key; _next = n; int getkey() { return _key; ListNode getnext() { return _next; void setnext(listnode n) {_next = n; ListNode remove(int key) { CS031 8

MM in Java class ListNode { ListNode remove(int key) { if (key == _key) return _next; Possible memory else { deallocation _next = _next.remove(key); return this; Possible memory class List { deallocation void remove(int key) { _head = _head.remove(key); CS031 9

MM in Java class ListNode { ListNode remove(int key) { if (key == _key) return _next; else { _next = _next.remove(key); return this; Possible memory class List { deallocation void remove(int key) { _head = _head.remove(key); Space complexity? Linear in the worst case Possible memory deallocation Possible memory deallocation CS031 10

MM in Java Why is it linear in space? Each recursive call takes space on the stack There are as many recursive calls as they are elements in the list in the worst case This is a terrible implementation from an efficiency standpoint! How to improve it? Do not use the stack! Use iterations/loops CS031 11

MM in Java void remove(int key) { ListNode cur = _head; ListNode prev = null; while (cur!= null) { if (cur.getkey() == key) break; prev = cur; cur = cur.getnext(); if (cur!= null) { if (prev!= null) prev.setnext(cur.getnext()); else _head = cur.getnext(); CS031 12

MM in Java ListNode remove(int key) { ListNode cur = _head; ListNode prev = null; while (cur!= null) { if (cur.getkey() == key) break; prev = cur; cur = cur.getnext(); if (curr!= null) { if (prev!= null) prev.setnext(cur.getnext()); else _head = cur.getnext(); What happens to the removed element? Java will garbage-collect it at some point. How do we figure out what is garbage? We do not need it any more CS031 13

MM in C++ High-level View Similar to Java Explicit memory deallocation No garbage collection Programmers are responsible to take their garbage out Main Support Destructors for deallocation This lecture Java with the C++ model C++ is much more compicated pointers, superset of C CS031 14

MM in C++ C++ automates many aspects Memory allocation Memory deallocation Main abstraction No need to know how memory is allocated/deallocated Main responsibility Programmers must specify which objects are no longer needed Nasty bugs Segmentation faults Bus error Memory leaks CS031 15

MM in C++ class ListNode { int _key; ListNode _next; ListNode(int key,listnode n) { _key = key, _next = n; ~ListNode() { Terminator CS031 16

MM in C++ When do I need to delete? a node which is deleted the list and all its nodes when the list is deleted CS031 17

MM in C++ void remove(int key) { ListNode cur = _head; ListNode prev = null; while (cur!= null) { if (cur.getkey() == key) break; prev = cur; cur = cur.getnext(); if (curr!= null) { if (prev!= null) prev.setnext(cur.getnext()); else _head = cur.getnext(); delete curr; Memory deallocation CS031 18

MM in C++ class List { ListNode _head; List() { ~List() { ListNode n; ListNode c = _head; while (c!=null) { n = c; c = c.getnext(); delete n; CS031 19

MM in C++ class List { ListNode _head; List() { ~List() { ListNode c = _head; while (c!=null) { delete c; c = c.getnext(); CS031 20

MM in C++ Memory bugs Using a freed object The data will become incorrect Freeing an object twice The space may be allocated twice later Forgetting to release an object Your program eats up the whole memory Nasty bugs Time discrepancy between the bug and the symptoms Things generally look fine until the space is reallocated to another object, at which time values may start being overwritten. The new values may have no meaning in the original context. CS031 21

MM in C C is a high-level assembly language Mixture of Pascal and Assembly Predecessor was B Successor D never saw the light Basic features Explicit memory management Bits and bytes and words Casting Pointer manipulation Pointers to any type (int, ) Structures on the stack No objects Only functions CS031 Lecture 19 22

MM in C struct ListNode { ; int _key; void test() { ListNode n; n._key = 5; ListNode* p = &n; free(p); Structure declaration Structure instance Observations ListNode is allocated on the stack It does not exist after the function call We need pointers to structures CS031 23

Pointers in C Religious wars Two syntax(es) The type-based syntax int* a; The dereference-based syntax int *a; Dereferencing a pointer int v = *a; CS031 24

MM in C struct ListNode { int _key; ListNode* _next; ; struct List { ; ListNode* _head; ListNode* search(list* l,int key) { ListNode* c = l->_head; while (c) { if (c->_key == key) else return c; c = c->_next; Pointer to a ListNode We are manipulating pointers! CS031 25

MM in C ListNode* search(list* l,int key) { ListNode* c = l->_head; while (c) { if (c->_key == key) return c; else c = c->_next; Dereferencing again! c = c->_next; Dereferencing operator is equivalent to c = (*c)._next; CS031 26

MM in C How to allocate memory? char* malloc(int sizeb) returns a pointer to sizeb bytes of memory space sizeof can be used to know the size of structures and other types In C, you are responsible of allocating memory space How to deallocate memory? free(void* ptr) frees the space allocated to ptr CS031 27

MM in C ListNode* createnode(int key,listnode* next) { ListNode* n; n = (ListNode*) malloc(sizeof(listnode)); n->_key = key; n->_next = next; return n; More bugs Allocate the wrong space Change the pointers n = n + 345; frees the wrong pointers Frees/returns objects from the stack CS031 28

MM in C++/C Assume that you want an array a ranging over 18..99 int* a = new int[82];! a -= 18;! for(int i = 18; i <= 99; i++)! a[i] = 0;! a += 18;! delete[] a;! CS031 29