Review. You Are Here! Recap: The Stack 11/8/12. CS 61C: Great Ideas in Computer Architecture C Memory Management. Recap: C Memory Management

Similar documents
Instructor: Randy H. Katz Fall Lecture #22. Warehouse Scale Computer

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

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

Agenda. Recap: C Memory Management. Agenda. Recap: Where are Variables Allocated? Recap: The Stack 11/15/10

CS 110 Computer Architecture. Lecture 4: Introduction to C, Part III. Instructor: Sören Schwertfeger.

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part III

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part III

Agenda. Pointer Arithmetic. Pointer Arithmetic pointer + number, pointer number 6/23/2011. Pointer Arithmetic: Peer Instruction Question

Review of Last Lecture

CS 61C: Great Ideas in Computer Architecture. Lecture 4: Memory Management. Bernhard Boser & Randy Katz

An example Request R1 for 100 bytes Request R2 for 1 byte Memory from R1 is freed Request R3 for 50 bytes What if R3 was a request for 101 bytes?

More On Memory (mis)-management

CS 61C: Great Ideas in Computer Architecture Redundant Arrays of Inexpensive Disks and C Memory Management

CS 61C: Great Ideas in Computer Architecture. Memory Management and Usage

CS61C : Machine Structures

Lectures 13 & 14. memory management

CS 110 Computer Architecture Lecture 5: Intro to Assembly Language, MIPS Intro

CS61C : Machine Structures

CS61C : Machine Structures

Review! Lecture 5 C Memory Management !

Arrays and Memory Management

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

Lecture 8 Dynamic Memory Allocation

CS 11 C track: lecture 5

Programs in memory. The layout of memory is roughly:

ECE 15B COMPUTER ORGANIZATION

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

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

Heap Arrays. Steven R. Bagley

Dynamic memory allocation (malloc)

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

Dynamic Memory Allocation: Basic Concepts

C Structures & Dynamic Memory Management

Dynamic Memory Allocation

Dynamic Memory: Alignment and Fragmentation

Memory Management. CS449 Fall 2017

Dynamic Memory Allocation: Basic Concepts

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

Memory (Stack and Heap)

Lecture 10: Program Development versus Execution Environment

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

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

CSC 1600 Memory Layout for Unix Processes"

Fall 2018 Discussion 2: September 3, 2018

DAY 3. CS3600, Northeastern University. Alan Mislove

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

Memory Allocation. General Questions

Lecture 7: Procedures and Program Execution Preview

Dynamic Allocation of Memory

Memory Organization. The machine code and data associated with it are in the code segment

CS61C Midterm Review on C & Memory Management

CS24 Week 2 Lecture 1

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

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

Dynamic Memory Allocation

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

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

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

Dynamic Memory Allocation I

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

CS61C : Machine Structures

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Heap Arrays and Linked Lists. Steven R. Bagley

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

Dynamic Memory Allocation I Nov 5, 2002

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

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

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

Linux and C Programming Language. Department of Computer Science and Engineering Yonghong Yan

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

Kurt Schmidt. October 30, 2018

C Programming Basics II

Introduction to Computer Systems /18 243, fall th Lecture, Oct. 22 th

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

Low-Level C Programming. Memory map Pointers Arrays Structures

Dynamic Data Structures. CSCI 112: Programming in C

Dynamic Memory Alloca/on: Basic Concepts

Quick review pointer basics (KR ch )

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Dynamic Memory Allocation

Lecture XX: Linux and C Programming Language CSCE 790: Parallel Programming Models for Multicore and Manycore Processors

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

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

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

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Memory Management. CSC215 Lecture

Dynamic Memory & ADTs in C

Dynamic Memory Management! Goals of this Lecture!

CS61C : Machine Structures

Dynamic Memory Allocation. Gerson Robboy Portland State University. class20.ppt

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

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

Lecture 14. Dynamic Memory Allocation

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

CS61, Fall 2012 Section 2 Notes

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

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Dynamic Memory & ADTs in C. The heap. Readings: CP:AMA 17.1, 17.2, 17.3, The primary goal of this section is to be able to use dynamic memory.

Transcription:

Review CS 61C: Great Ideas in Computer Architecture C Memory Management Instructors: Krste Asanovic, Randy H. Katz hcp://inst.eecs.berkeley.edu/~cs61c/fa12 Direct- mapped caches suffer from conflict misses 2 memory blocks mapping to same block knock each other out as program bounces from 1 memory locauon to next One way to solve: set- associauvity Memory block maps into more than 1 cache block N- way: n possible places in cache to hold a memory block N- way Cache of 2 N+M blocks: 2 N ways x 2 M sets MulU- level caches OpUmize first level to be fast! OpUmize 2 nd and 3 rd levels to minimize the memory access penalty Fall 2012 - - Lecture #32 1 2 Parallel Requests Assigned to computer e.g., Search Katz Parallel Threads Assigned to core e.g., Lookup, Ads So.ware Parallel InstrucUons >1 instrucuon @ one Ume e.g., 5 pipelined instrucuons Parallel Data >1 data item @ one Ume e.g., Add of 4 pairs of words Hardware descripuons All gates @ one Ume Programming Languages You Are Here! Harness Parallelism & Achieve High Performance Hardware Warehouse Scale Computer Today s Lecture Memory Input/Output InstrucUon Unit(s) Main Memory Computer (Cache) FuncUonal Unit(s) A 0 +B 0 A 1 +B 1 A 2 +B 2 A 3 +B 3 Smart Phone Logic Gates 3 Recap: C Memory Management Program s address space contains 4 regions: stack: local variables, grows downward heap: space requested for pointers via malloc(); resizes dynamically, grows upward stauc data: variables declared outside main, does not grow or shrink code: loaded when program starts, does not change ~ FFFF FFFF hex stack heap stauc data code ~ 0 hex OS prevents accesses between stack and heap (via virtual memory) 4 Recap: Where are Variables Allocated? If declared outside a procedure, allocated in stauc storage If declared inside procedure, int myglobal; allocated on the stack main() { and freed when procedure int mytemp; returns main() is treated like a procedure Recap: The Stack Stack includes: Return instrucuon address Parameters Space for other local variables Stack s conuguous blocks of memory; stack pointer indicates top of stack When procedure ends, stack is tossed off the stack; frees memory for future stack s SP 5 6 1

Recap: The Stack Last In, First Out (LIFO) data structure main () { a(0); void a (int m) { b(1); void b (int n) { c(2); void c (int o) { d(3); void d (int p) { Stack grows down stack 7 ObservaUons Code, StaUc storage are easy: they never grow or shrink Stack space is relauvely easy: stack s are created and destroyed in last- in, first- out (LIFO) order Managing the heap is tricky: memory can be allocated / deallocated at any Ume 8 C supports five funcuons for heap management: malloc(), calloc(), free(), cfree(), realloc() malloc(n): Allocate a block of uniniualized memory NOTE: Subsequent calls need not yield blocks in conunuous sequence n is an integer, indicaung size of allocated memory block in bytes sizeof determines size of given type in bytes, produces more portable code Returns a pointer to that memory locauon; NULL return indicates no more memory Think of ptr as a handle that also describes the allocated block of memory; AddiUonal control informauon stored in the heap around the allocated block! Example: int *ip; ip = malloc(sizeof(int)); struct treenode *tp; tp = malloc(sizeof(struct treenode)); 9 free(p): Releases memory allocated by malloc() p is pointer containing the address originally returned by malloc() int *ip; ip = malloc(sizeof(int));....... free(ip); /* Can you free(ip) after ip++? */ struct treenode *tp; tp = malloc(sizeof(struct treenode));....... free(tp); When insufficient free memory, malloc() returns NULL pointer; Check for it! if ((ip = malloc(sizeof(int))) == NULL){ printf( \nmemory is FULL\n ); exit(1); When you free memory, you must be sure that you pass the original address returned from malloc() to free(); Otherwise, system excepuon (or worse)! 10 Common Memory Problems Using uniniualized values Using memory that you don t own Deallocated stack or heap variable Out- of- bounds reference to stack or heap array Using NULL or garbage data as a pointer Improper use of free/realloc by messing with the pointer handle returned by malloc/calloc Memory leaks (you allocated something you forgot to later free) Fall 2012 - - Lecture #32 11 Memory Debugging Tools RunUme analysis tools for finding memory errors Dynamic analysis tool: collects informauon on memory management while program runs Contrast with stauc analysis tool like lint, which analyzes source code without compiling or execuung it No tool is guaranteed to find ALL memory bugs this is a very challenging programming language research problem Runs 10X slower hcp://valgrind.org 12 2

int *ipr, *ipw; void ReadMem() { int i, j; *ipr = malloc(4 * sizeof(int)); i = *(ipr - 1000); j = *(ipr + 1000); free(ipr); void WriteMem() { *ipw = malloc(5 * sizeof(int)); *(ipw - 1000) = 0; *(ipw + 1000) = 0; free(ipw); How are Malloc/Free implemented? Underlying operaung system allows malloc library to ask for large blocks of memory to use in heap (using Unix sbrk call) C Malloc library creates data structure inside unused poruons to track free space 13 15 Simple Slow Malloc ImplementaUon Faster malloc implementauons IniUal Empty Heap space from OperaUng System Free Space Malloc library creates linked list of empty blocks (one block iniually) Object 1 Free Keep separate pools of blocks for different sized objects Buddy allocators always round up to power- of- 2 sized chunks to simplify finding correct size and merging neighboring blocks: First allocauon chews up space from start of free space Free Awer many mallocs and frees, have potenually long linked list of odd- sized blocks Frees link block back onto linked list might merge with neighboring free space 16 17 Power- of- 2 Buddy Allocator Malloc ImplementaUons All provide the same library interface, but can have radically different implementauons Uses headers at start of allocated blocks and space in unallocated memory to hold malloc s internal data structures Rely on programmer remembering to free with same pointer returned by alloc Rely on programmer not messing with internal data structures accidentally! 18 19 3

Administrivia int *pi; void foo() { pi = malloc(8*sizeof(int)); free(pi); void main() { pi = malloc(4*sizeof(int)); foo(); 20 21 int *plk = NULL; void genplk() { plk = malloc(2 * sizeof(int)); plk++; void FreeMemX() { int fnh = 0; free(&fnh); void FreeMemY() { int *fum = malloc(4 * sizeof(int)); free(fum+1); free(fum); free(fum); 23 25 Using Memory You Haven t Allocated void StringManipulate() { const char *name = Safety Critical; char *str = malloc(10); strncpy(str, name, 10); str[10] = '\0'; printf(%s\n, str); What s wrong with this code? char *append(const char* s1, const char *s2) { const int MAXSIZE = 128; char result[128]; int i=0, j=0; for (j=0; i<maxsize-1 && j<strlen(s1); i++,j++) { result[i] = s1[j]; for (j=0; i<maxsize-1 && j<strlen(s2); i++,j++) { result[i] = s2[j]; result[++i] = '\0'; return result; 27 Fall 2012 - - Lecture #32 29 4

typedef struct node { struct node* next; int val; Node; int findlastnodevalue(node* head) { while (head->next!= NULL) { head = head->next; return head->val; 31 calloc(n, size): Allocate n elements of same data type; n can be an integer variable, use calloc()to allocate a dynamically size array n is the # of array elements to be allocated size is the number of bytes of each element calloc() guarantees that the memory contents are iniualized to zero E.g.: allocate an array of 10 elements int *ip; ip = calloc(10, sizeof(int));! *(ip+1) refers to the 2 nd element, like ip[1] *(ip+i) refers to the i+1 th element, like ip[i] Beware of referencing beyond the allocated block: e.g., *(ip+10)! calloc() returns NULL if no further memory is available cfree(p) // Legacy function same as free cfree() releases the memory allocated by calloc(); E.g.: cfree(ip);! 33 realloc(p,size): Resize a previously allocated block at p to a new size If p is NULL, then realloc behaves like malloc If size is 0, then realloc behaves like free, deallocaung the block from the heap Returns new address of the memory block; NOTE: it is likely to have moved! E.g.: allocate an array of 10 elements, expand to 20 elements later int *ip; ip = malloc(10*sizeof(int)); /* always check for ip == NULL */!!! ip = realloc(ip,20*sizeof(int)); /* always check for ip == NULL */ /* contents of first 10 elements retained */ realloc(ip,0); /* identical to free(ip) */ 34! int* init_array(int *ptr, int new_size) { ptr = realloc(ptr, new_size*sizeof(int)); memset(ptr, 0, new_size*sizeof(int)); Student RouleCe return ptr; int* fill_fibonacci(int *fib, int size) { int i; init_array(fib, size); /* fib[0] = 0; */ fib[1] = 1; for (i=2; i<size; i++) fib[i] = fib[i-1] + fib[i-2]; return fib; 35 Summary C has three pools of data memory (+ code memory) StaUc storage: global variable storage, ~permanent, enure program run The Stack: local variable storage, parameters, return address The Heap (dynamic storage): malloc()gets space from here, free() returns it Common (Dynamic) Memory Problems Using uniniualized values Accessing memory beyond your allocated region Improper use of free by changing pointer handle returned by malloc Memory leaks: mismatched malloc/free pairs 37 5