CS61C Midterm Review on C & Memory Management

Similar documents
CS61C : Machine Structures

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

Lectures 13 & 14. memory management

CS61C : Machine Structures

CS61C : Machine Structures

CS61C : Machine Structures

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

CS61C : Machine Structures

CS61C : Machine Structures

Heap Arrays. Steven R. Bagley

Run-time Environments - 3

Motivation for Dynamic Memory. Dynamic Memory Allocation. Stack Organization. Stack Discussion. Questions answered in this lecture:

Heap Arrays and Linked Lists. Steven R. Bagley

6.172 Performance Engineering of Software Systems Spring Lecture 9. P after. Figure 1: A diagram of the stack (Image by MIT OpenCourseWare.

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

Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

ECE 598 Advanced Operating Systems Lecture 10

Lecture 8 Dynamic Memory Allocation

Recitation #11 Malloc Lab. November 7th, 2017

Run-time Environments -Part 3

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

CSC 1600 Memory Layout for Unix Processes"

Dynamic Memory Management

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

Dynamic Storage Allocation

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

CS 241 Honors Memory

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Dynamic Memory Management! Goals of this Lecture!

Memory Management. Didactic Module 14 Programming Languages - EEL670 1

Memory Management. Chapter Fourteen Modern Programming Languages, 2nd ed. 1

CS 11 C track: lecture 5

Class Information ANNOUCEMENTS

Memory Management. CSC215 Lecture

Review Pointers and arrays are virtually same C knows how to increment pointers C is an efficient language, with little protection

A.Arpaci-Dusseau. Mapping from logical address space to physical address space. CS 537:Operating Systems lecture12.fm.2

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

CS61, Fall 2012 Section 2 Notes

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

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

ECE 598 Advanced Operating Systems Lecture 12

Dynamic Data Structures. CSCI 112: Programming in C

Data Structures and Algorithms for Engineers

Dynamic Memory Management

Garbage Collection. Steven R. Bagley

Arrays and Memory Management

DAY 3. CS3600, Northeastern University. Alan Mislove

Names, Scope, and Bindings

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

a data type is Types

Malloc Lab & Midterm Solutions. Recitation 11: Tuesday: 11/08/2016

ANITA S SUPER AWESOME RECITATION SLIDES

ECE 15B COMPUTER ORGANIZATION

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

Process s Address Space. Dynamic Memory. Backing the Heap. Dynamic memory allocation 3/29/2013. When a process starts the heap is empty

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

Optimizing Dynamic Memory Management

CPSC 213, Summer 2017, Term 2 Midterm Exam Solution Date: July 27, 2017; Instructor: Anthony Estey

! What is main memory? ! What is static and dynamic allocation? ! What is segmentation? Maria Hybinette, UGA. High Address (0x7fffffff) !

Memory Management: The process by which memory is shared, allocated, and released. Not applicable to cache memory.

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

CS61C : Machine Structures

CMSC 330: Organization of Programming Languages

CS 61C: Great Ideas in Computer Architecture Introduction to C, Part III

Welcome! COMP s1. Programming Fundamentals

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

G Programming Languages - Fall 2012

10.1. CS356 Unit 10. Memory Allocation & Heap Management

Memory Allocation. Copyright : University of Illinois CS 241 Staff 1

BBM 201 DATA STRUCTURES

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

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

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Robust Memory Management Schemes

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

CS61C : Machine Structures

Run-Time Environments/Garbage Collection

Segmentation. Multiple Segments. Lecture Notes Week 6

CA341 - Comparative Programming Languages

Heap Management. Heap Allocation

Common Misunderstandings from Exam 1 Material

Kurt Schmidt. October 30, 2018

Operating System Labs. Yuanbin Wu

CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers

CS24 Week 2 Lecture 1

High Performance Computing and Programming, Lecture 3

CS 61C: Great Ideas in Computer Architecture C Memory Management, Usage Models

Heap Management portion of the store lives indefinitely until the program explicitly deletes it C++ and Java new Such objects are stored on a heap

Names, Scope, and Bindings

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Binghamton University. CS-211 Fall Dynamic Memory

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Part II: Memory Management. Chapter 7: Physical Memory Chapter 8: Virtual Memory Chapter 9: Sharing Data and Code in Main Memory

CMSC 330: Organization of Programming Languages

CS399 New Beginnings. Jonathan Walpole

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

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

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

Programs in memory. The layout of memory is roughly:

Transcription:

CS61C Midterm Review on C & Memory Management Fall 2006 Aaron Staley Some material taken from slides by: Michael Le Navtej Sadhal

Overview C Array and Pointer Goodness! Memory Management The Three Three s!

Pointers in C Pointers A pointer contains an address of a piece of data. & gets the address of a variable * dereferences a pointer int a; /*Declare a*/ int *b = &a; /*get address of A*/ int c = *b; /*dereference B get C*/

Pointer Math Consider int * a = malloc(3*sizeof(int)); int * b = a + 2; This is the same as: int * a = malloc(3*sizeof(int)); int * b = (int*)( ((int)a) + 2*sizeof(*a)); (In other words, b will increase by 8 in this example)

Arrays in C Arrays vs. Pointers -Interchangeable when used in a function: void foo (int * a); IS void foo (int a[]); -array[index] is equivalent to *(array+index) b[i]; IS /*remember pointer math!*/ *(b+i); -Arrays also have a special declaration to allocate stack space. int c[5]; /*creates 5 integers on the stack*/ NOTE: c acts like a special read-only pointer that can t be modified!

A Note about C Declarations Declarations have same syntax as use! -int * a[2]; /*declare this*/ -Now doing *a[1] will return an int! Question: Is int * a[2] declaring an array of 2 pointers to integers or a pointer to arrays of 2 integers?

A Note about C Declarations Declarations have same syntax as use! -int * a[2]; /*declare this*/ -Now doing *a[1] will return an int! Question: Is int * a[2] declaring an array of 2 pointers to integers or a pointer to arrays of 2 integers? IT IS AN ARRAY OF 2 POINTERS TO INTEGERS! This is because a[1] would return an int*!

And Structures/Unions Struct keyword defines new datatypes: struct bintree{ int a; struct bintree * left; struct bintree * right; }; So: sizeof(struct bintree) == 12 Unions allow types to be used interchangeably. Fields all use the same memory. Size is the largest field: union anything{ char a; int b; void * c; }; So: sizeof (union anything) == 4

Pointers How would you create this situation in C without using malloc()? a b C (array) d struct Node { int i; struct Node * next; };

Pointers struct Node { int i; struct Node * next; }; int main() { struct Node a, b, c[5], d; a.next = &b; b.next = c; c[0].next = &d; /* c->next =&d; is also valid*/ return 0; }

Malloc Allocates memory on the heap Data not disappear after function is removed from stack How do you allocate an array of 10 integers?

Malloc Allocates memory on the heap Data not disappear after function is removed from stack How do you allocate an array of 10 integers? int *i= malloc(sizeof(int)*10);

Malloc Allocates memory on the heap Data not disappear after function is removed from stack How do you allocate an array of 10 integers? int *i= malloc(sizeof(int)*10); String of length 80?

Malloc Allocates memory on the heap Data not disappear after function is removed from stack How do you allocate an array of 10 integers? int *i= malloc(sizeof(int)*10); String of length 80? char *str= malloc(sizeof(char)*81); /*Remember: Strings end with \0 */

Malloc Allocates memory on the heap Data not disappear after function is removed from stack How do you allocate an array of 10 integers? int *i= malloc(sizeof(int)*10); String of length 80? char *str= malloc(sizeof(char)*81); If you don t free what you allocate, you memory leak: free (str); /*Do this when done with str*/

Memory Management ~0xFFFF FFFF Stack Stack: local variables, grows down (lower addresses) Heap: malloc and free, grows up (higher addresses) Static: global variables, fixed size Heap Static Code ~0x0000 0000

Pointers & Memory You have a linked list which holds some value. You want to insert in new nodes in before all nodes of a certain value. struct node { int * i; /*pointer to some value in STATIC memory*/ struct node * next; /*next*/ }; typedef struct node Node; /*typedef is for aliasing!*/ void insertnodes(node **lstptr, int oldval, / *something*/) { }

Pointers & Memory struct node { int * i; /*pointer to some value in STATIC memory*/ struct node * next; /*next*/ }; typedef struct node Node; /*NOTE: lstptr is a handle here. We do this in case the HEAD of the list is removed!*/ Which is correct? void insertnodes(node **lstptr, int oldval, int * newval) OR void insertnodes(node **lstptr, int oldval, int newval)

Pointers & Memory struct node { int * i; /*pointer to some value in STATIC memory*/ struct node * next; /*next*/ }; typedef struct node Node; /*NOTE: lstptr is a handle here. We do this in case the HEAD of the list is removed!*/ Which is correct? void insertnodes(node **lstptr, int oldval, int * newval) OR void insertnodes(node **lstptr, int oldval, int newval)

In Pictures List looks like: insertnodes(&head, 1, ptr_to_1); Has no effect insertnodes(&head, 4, ptr_to_1); List becomes: A small hint: &(f.a) will return address of field a (of structure f)

Pointers void insertnodes(node **lstptr, int oldval, int * newval) if ((*lstptr)==null) { /*Base CASE*/ } else if (*((*lstptr)->i) == oldval) { /*Equality*/ /*Insert before this node*/ /*Update *lstptr somehow?*/ } else { /*Inequality.. Resume*/ /*But be careful with lstptr!*/ } } /*Recall that f->a IS (*f).a */

Pointers void insertnodes(node **lstptr, int oldval, int * newval) if ((*lstptr)==null) { return; } else if (*((*lstptr)->i) == oldval) { Node * old = *lstptr; *lstptr = malloc(sizeof(node)); (*lstptr)->i = newval; (*lstptr)->next = old; insertnodes (&(old->next), oldval,newval); } else { insertnodes( &((*lstptr)->next),oldval,newval); } }

A Reminder: Memory Management 0xFFFF FFFF Stack Stack: local variables, grows down (lower addresses) Heap: malloc and free, grows up (higher addresses) Static: global variables, fixed size Heap Static Code 0x0000 0000

Memory (Heap) Management When allocating and freeing memory on the heap, we need a way to manage free blocks of memory. Lecture covered three different ways to manage free blocks of memory. Free List (first fit, next fit, best fit) Slab Allocator Buddy System

Free List Maintains blocks in a (circular) list: struct malloc_block{ }; struct malloc_block * next; int size; uint8_t data[size]; /*WARNING: This is pseudocode*/ head Address returned to caller of malloc() size data size data size data

Free List Fits First Fit Selects first block (from head) that fits! Can lead to much fragmentation, but better locality (you ll learn why this is important) Example: malloc (4*sizeof(char)); head 5 data 4 data 3 data

Free List Fits First Fit Selects first block (from head) that fits! Can lead to much fragmentation, but better locality (you ll learn why this is important) Example: malloc (4*sizeof(char)); head 5 data 4 data 3 data

Free List Fits Next Fit selects next block (after last one picked) that fits! Tends to be rather fast (small blocks everywhere!) Example: malloc (5*sizeof(char)); head Next to Pick 5 data 4 data 6 data

Free List Fits Next Fit selects next block (after last one picked) that fits! Tends to be rather fast (small blocks everywhere!) = Example: malloc (5*sizeof(char)); head 5 data 4 data Next to Pick 6 data

Free List Fits Best fit picks the smallest block >= requested size. Tries to limit fragmentation, but can be slow (often searches entire list)! Example: malloc (2*sizeof(char)); head 5 data 4 data 3 data

Free List Fits Best fit picks the smallest block >= requested size. Tries to limit fragmentation, but can be slow (often searches entire list)! Example: malloc (2*sizeof(char)); head 5 data 4 data 3 data

The Slab Allocator Only give out memory in powers of 2. Keep different memory pools for different powers of 2. Manage memory pool with bitmaps Revert to free list for large blocks.

The Slab Allocator Example: malloc(24*sizeof(char)); Old: New:

The Slab Allocator Example: malloc(24*sizeof(char)); Old: New:

The Buddy System An adaptive Slab Allocator Return blocks of size n as usual. If not found, find block of size 2*n and split the block (This is recursive)! When block of size n is freed, merge it with its neighbor (if the neighbor is freed) into a block of size 2n (recursive!)

The Buddy System Example: 32 bytes free 16 bytes free 16 bytes TAKEN malloc(7*sizeof(char)); /*force request to be 8 bytes*/

The Buddy System Example: 32 bytes free 16 bytes free 16 bytes TAKEN malloc(7*sizeof(char)); /*force request to be 8 bytes*/ 32 bytes free 8 8 16 bytes bytes bytes TAKEN free free

The Buddy System Example: 32 bytes free 16 bytes free 16 bytes TAKEN malloc(7*sizeof(char)); /*force request to be 8 bytes*/ 32 bytes free 8 8 16 bytes bytes TAKEN bytes free TAK- EN

Example: free (a); The Buddy System 32 bytes free 8 8 16 bytes bytes free bytes free TAK- EN A

Example: free (a); The Buddy System 32 bytes free 8 8 16 bytes bytes free bytes free TAK- EN A 32 bytes free 8 8 16 bytes bytes free bytes free free

Example: The Buddy System free (a); 32 bytes free 8 bytes free 8 bytes free 16 bytes free Coalesce: 32 bytes free 16 bytes free 16 bytes free

Example: The Buddy System free (a); 32 bytes free 16 bytes free 16 bytes free Coalesce: 32 bytes free 32 bytes free

Example: The Buddy System free (a); 32 bytes free 32 bytes free Coalesce: 64 bytes free =)

A Word about Fragmentation Internal fragmentation: Wasted space within an allocated block (i.e. I request 30 bytes but get a 32 byte block back) External Fragmentation: Wasted space between allocated blocks (if blocks were compacted, we could have more contiguous memory)

An Old Midterm Question For each of the allocation systems on the left, circle the column that describes its fragmentation: Buddy System Causes internal only Causes external only Causes both types Slab Allocator Causes internal only Causes external only Causes both types K&R (Free List Only) Causes internal only Causes external only Causes both types

An Old Midterm Question For each of the allocation systems on the left, circle the column that describes its fragmentation: Buddy System Causes internal only Causes external only Causes both types Slab Allocator Causes internal only Causes external only Causes both types K&R (Free List Only) Causes internal only Causes external only Causes both types

Garbage Collection Garbage collection is used for automatically cleaning up the heap. We can t do this in C, because of pointer casting, pointer math, etc. Lecture covered three different ways to garbage collect: Reference Count Mark and Sweep Stop and Copy

Reference Count Root Set 1 1 2 1 1 1

Reference Count Root Set 1 1 3 1 1 0

Reference Count Root Set 1 1 3 1 1 Lots of overhead every time a pointer changes, the count changes. Unused cycles are never retrieved! 0

Mark and Sweep Root Set 0 1 1 0 3 1 0 1 0

Mark and Sweep Root Set 0 x 1 0 3 1 0 1 0

Mark and Sweep Root Set 0 x 1 0 3 1 0 1 0

Mark and Sweep Root Set 0 x 1 0 3 1 0 1 0

Mark and Sweep Root Set 0 x 1 0 3 1 0 1 Requires us to stop every so often and mark all reachable objects (mark), then free all unmarked blocks (sweep). Once mark and sweep is done, unmark everything! 0

Root Set Stop and Copy Root Set Stop and Copy Requires us to also stop every so often. But for stop and copy, we move the block to an empty portion of the heap. Pointers must be changed to reflect the change in block location. Forwarding pointers must be used!

An Old Midterm Question Three code gurus are using different garbage collection techniques on three identical machines (heap memory size M). Fill in the table. All answers should be a function of M, e.g., M/7 or 5M. (data = data in heap) What is the most space their data could take before GC least space their data could take after GC? Most space their data could take after GC Most wasted space that GC can t recover? Reference Counting Mark and Sweep Copying

An Old Midterm Question Three code gurus are using different garbage collection techniques on three identical machines (heap memory size M). Fill in the table. All answers should be a function of M, e.g., M/7 or 5M. (data = data in heap) What is the most space their data could take before GC least space their data could take after GC? Most space their data could take after GC Most wasted space that GC can t recover? Reference Counting M (-constant) 0 M (-constant) M (-constant) Mark and Sweep M (-constant) 0 M (-constant) 0 Stop & Copy M/2 (- really small constant) 0 M/2 (- really small constant) 0