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

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

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

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

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

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

Review of Last Lecture

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?

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

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

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

More On Memory (mis)-management

CS61C : Machine Structures

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

CS61C : Machine Structures

Review! Lecture 5 C Memory Management !

Arrays and Memory Management

CS61C : Machine Structures

Lectures 13 & 14. memory management

Memory Management. CS449 Fall 2017

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

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

ECE 15B COMPUTER ORGANIZATION

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

CS 11 C track: lecture 5

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

Lecture 7: Procedures and Program Execution Preview

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

Lecture 10: Program Development versus Execution Environment

Lecture 8 Dynamic Memory Allocation

DAY 3. CS3600, Northeastern University. Alan Mislove

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

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

Heap Arrays. Steven R. Bagley

Pointers and Memory Management

Dynamic memory allocation (malloc)

Debugging (Part 2) 1

CS61, Fall 2012 Section 2 Notes

Key C Topics: Tutorial Pointers, Dynamic Memory allocation, Valgrind and Makefile CS370

C Structures & Dynamic Memory Management

Memory (Stack and Heap)

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

Dynamic Memory Alloca/on: Basic Concepts

Lecture 2: Memory in C

Memory Analysis tools

Programs in memory. The layout of memory is roughly:

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

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

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

Fall 2018 Discussion 2: September 3, 2018

CSC 1600 Memory Layout for Unix Processes"

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

CS61C : Machine Structures

Dynamic Memory Alloca/on: Basic Concepts

6.S096: Introduction to C/C++

Princeton University Computer Science 217: Introduction to Programming Systems. Data Structures

Memory Allocation. General Questions

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

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

CS24 Week 2 Lecture 1

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

Scientific Programming in C IV. Pointers

Dynamic Memory Allocation

CS61C : Machine Structures

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

Kurt Schmidt. October 30, 2018

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

Dynamic Memory Alloca/on: Advanced Concepts

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

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

Dynamic Memory Alloca/on: Advanced Concepts

CSE 333 Midterm Exam Sample Solution 7/28/14

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

TI2725-C, C programming lab, course

Arrays and Pointers. CSE 2031 Fall November 11, 2013

valgrind overview: runtime memory checker and a bit more What can we do with it?

Creating a String Data Type in C

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

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

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

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

Quick review pointer basics (KR ch )

Programming. Pointers, Multi-dimensional Arrays and Memory Management

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

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

CSCI-1200 Data Structures Fall 2015 Lecture 6 Pointers & Dynamic Memory

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Lecture 14. Dynamic Memory Allocation

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

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

Linked data structures. EECS 211 Winter 2019

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.

Dynamic Memory & ADTs in C

Memory Allocation III

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Dynamic Memory & ADTs in C

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

Transcription:

CS 61C: Great Ideas in Computer Architecture (Machine Structures) Dynamic Memory Management Instructors: Randy H. Katz David A. PaGerson hgp://inst.eecs.berkeley.edu/~cs61c/fa10 1 2 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 sta\c data: variables declared outside main, does not grow or shrink code: loaded when program starts, does not change ~ FFFF FFFF hex stack heap sta\c 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 sta\c 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 frame includes: SP frame Return instruc\on address Parameters Space for other local variables Stack frames con\guous blocks of memory; stack pointer indicates top of stack frame frame frame frame When procedure ends, stack frame is tossed off the stack; frees memory for future stack frames 5 6 1

Observa\ons Code, Sta\c storage are easy: they never grow or shrink Stack space is rela\vely easy: stack frames are created and destroyed in last- in, first- out (LIFO) order Managing the heap is tricky: memory can be allocated / deallocated at any \me 7 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 8 Peer Instruc\on: What s Wrong with this Code?!int *ptr () {!int y;!y = 3;!return &y; ;! Green: prinh modifies stackaddr Yellow: prinh bug modifies content Red: prinh overwrites stack frame main () {!int *stackaddr,content;!stackaddr = ptr();!printf("%d", content); /* 3 */!printf("%d", content); /*13451514 */ ;! 9 Peer Instruc\on: What s Wrong with this Code?!int *ptr () {!int y;!y = 3;!return &y; ;! SP main ptr() (y==3) main main () {!int *stackaddr,content;!stackaddr = ptr();!printf("%d", content); /* 3 */!printf("%d", content); /*13451514 */ ;! 10 SP main prinh() (y==?) SP Red: prin6 overwrites stack frame Never return pointers to local variable from func5ons Your compiler will warn you about this don t ignore such warnings! C supports five func\ons for heap management: malloc(), calloc(), free(), cfree(), realloc()! malloc(n): Allocate a block of unini\alized memory NOTE: Subsequent calls need not yield blocks in con\nuous sequence n is an integer, indica\ng 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 loca\on; NULL return indicates no more memory Think of ptr as a handle that also describes the allocated block of memory; Addi\onal control informa\on stored in the heap around the allocated block! Example: ip = malloc(sizeof(int)); struct treenode *tp;! tp = malloc(sizeof(struct tnode));! 11 free(p): Releases memory allocated by malloc()! p is pointer containing the address originally returned by malloc()!!!!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 excep\on! 12 2

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 ini\alized to zero E.g.: allocate an array of 10 elements!!!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): cfree() releases the memory allocated by calloc(); E.g.: cfree(ip);! 13 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, dealloca\ng 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!!!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) */! 14 Example Administrivia Project 4: Single Cycle Processor in Logisim Due Sat 27 Nov (fine to submit early!) Face- to- Face grading in Lab, Th 2 Dec EC: Performance Improvements to Proj #3 (due Tu 29 Nov) Final Review Sessions: Mon 6 Dec, 1400-1700, 10 Evans Final: Mon 13 Dec 0800-1100 (220 Hearst Gym) Like midterm: T/F, M/C, short answers Whole Course: readings, lectures, projects, labs, hw Emphasize 2 nd half of 61C + midterm mistakes 15 11/15/10 Fall 2010 - - Lecture #32 16 17 18 3

Common Memory Problems Using unini\alized 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) 11/15/10 Fall 2010 - - Lecture #33 19 Run\me analysis tools for finding memory errors Dynamic analysis tool: collects informa\on on memory management while program runs Contrast with sta\c analysis tool like lint, which analyzes source code without compiling or execu\ng it No tool is guaranteed to find ALL memory bugs this is a very challenging programming language research problem You will be introduced in Valgrind in Lab #6! Debugging Tools hgp://valgrind.org 20 Using Unini\alized Values Using Unini\alized Values!void foo(int *pi) {!!! int j;!!! *pi = j;!!!void bar() {!! int i=10;!! foo(&i);!! printf("i = %d\n", i);! 21!void foo(int *pi) {!!! int j;!!! *pi = j;!!/* j is uninitialized, copied into *pi */!!!void bar() {!! int i=10;!! foo(&i);!! printf("i = %d\n", i);!!/* Using i, which now has junk value */! 22 Valgrind Output (Highly Abridged!) ==98863== Memcheck, a memory error detector ==98863== Using Valgrind- 3.6.0 and LibVEX; rerun with - h for copyright info ==98863== Condi\onal jump or move depends on unini\alised value(s) ==98863== at 0x100031A1E: vfprinh (in /usr/lib/libsystem.b.dylib) ==98863== by 0x100073BCA: vfprinh_l (in /usr/lib/libsystem.b.dylib) ==98863== by 0x1000A11A6: prinh (in /usr/lib/libsystem.b.dylib) ==98863== by 0x100000EEE: main (slide21.c:13) ==98863== Unini5alised value was created by a stack alloca5on ==98863== at 0x100000EB0: foo (slide21.c:3) ==98863== Valgrind Output (Highly Abridged!) ==98863== HEAP SUMMARY: ==98863== in use at exit: 4,184 bytes in 2 blocks ==98863== total heap usage: 2 allocs, 0 frees, 4,184 bytes allocated ==98863== ==98863== LEAK SUMMARY: ==98863== definitely lost: 0 bytes in 0 blocks ==98863== indirectly lost: 0 bytes in 0 blocks ==98863== possibly lost: 0 bytes in 0 blocks ==98863== s\ll reachable: 4,184 bytes in 2 blocks ==98863== suppressed: 0 bytes in 0 blocks ==98863== Reachable blocks (those to which a pointer was found) are not shown. ==98863== To see them, rerun with: - - leak- check=full - - show- reachable=yes 23 24 4

Following a NULL pointer to mem addr 0! typedef struct node {!! struct node* next;!! int val;! Node;! int findlastnodevalue(node* head) {!! while (head->next!= NULL) {!!! head = head->next;!!! return head->val;! typedef struct node {!! struct node* next;!! int val;! Node;! int findlastnodevalue(node* head) {!! while (head->next!= NULL) { /* What if head happens to be NULL? */!!! head = head->next;!!! return head->val; /* What if head is NULL? */! 25 26!void ReadMem() {!! *ipr = malloc(4 * sizeof(int));!! int i, j;!! i = *(ipr - 1000); j = *(ipr + 1000);!! free(ipr);! void WriteMem() {!! *ipw = malloc(5 * sizeof(int));!! *(ipw - 1000) = 0; *(ipw + 1000) = 0;!! free(ipw);! Using pointers beyond the range that had been malloc d May look obvious, but what if mem refs had been result of pointer arithme\c that erroneously took them out of the allocated range?!void ReadMem() {!! *ipr = malloc(4 * sizeof(int));!! int i, j;!! i = *(ipr - 1000); j = *(ipr + 1000);!! free(ipr);! void WriteMem() {!! *ipw = malloc(5 * sizeof(int));!! *(ipw - 1000) = 0; *(ipw + 1000) = 0;!! free(ipw);! 27 28 int* init_array(int *ptr, int new_size) {!!ptr = realloc(ptr, new_size*sizeof(int));!!memset(ptr, 0, new_size*sizeof(int));!!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;! 29 Improper matched usage of mem handles int* init_array(int *ptr, int new_size) {!!ptr = realloc(ptr, new_size*sizeof(int));!!memset(ptr, 0, new_size*sizeof(int));!!return ptr;! Remember: realloc may move en\re block int* fill_fibonacci(int *fib, int size) {!!int i;!!/* oops, forgot: fib = */ 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;! What if array is moved to new loca\on? 30 5

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;! Beyond stack read/write char *append(const char* s1, const char *s2) {!!const int MAXSIZE = 128;!!char result[128];! result is a local array name!int i=0, j=0;! stack memory allocated!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;! Func\on returns pointer to stack memory won t be valid aver func\on returns 11/15/10 Fall 2010 - - Lecture #33 31 11/15/10 Fall 2010 - - Lecture #33 32 Using Memory You Haven t Allocated Using Memory You Haven t Allocated Reference beyond array bounds void StringManipulate() {!!const char *name = Safety Critical";!!char *str = malloc(10);!!strncpy(str, name, 10);!!str[10] = '\0';!!printf("%s\n", str);! 33 void StringManipulate() {!!const char *name = Safety Critical";!!char *str = malloc(10);!!strncpy(str, name, 10);!!str[10] = '\0';! /* Write Beyond Array Bounds */!!printf("%s\n", str);! /* Read Beyond Array Bounds */! 34 int *pi;! void foo() {!!pi = malloc(8*sizeof(int));!!!!free(pi);! void main() {!!pi = malloc(4*sizeof(int));!!foo();! 35 Memory leak: more mallocs than frees int *pi;! void foo() {!!pi = malloc(8*sizeof(int));!!/* Allocate memory for pi */!!/* Oops, leaked the old memory pointed to by pi */!!!!free(pi); /* foo() is done with pi, so free it */! void main() {!!pi = malloc(4*sizeof(int));!!foo(); /* Memory leak: foo leaks it */! 36 6

int *plk = NULL;! void genplk() {!!plk = malloc(2 * sizeof(int));!!!plk++;!!! Poten\al memory leak handle has been changed, do you s\ll have copy of it that can correctly be used in a later free? int *plk = NULL;! void genplk() {!!plk = malloc(2 * sizeof(int));!!!!plk++;!/* Potential leak: pointer variable incremented past beginning of block! */! 37 38 void FreeMemX() {!!int fnh = 0;!!free(&fnh);! void FreeMemY() {!!int *fum = malloc(4 * sizeof(int));!!free(fum+1);!!free(fum);!!free(fum);! Can t free non- heap memory; Can t free memory that hasn t been allocated void FreeMemX() {!!int fnh = 0;!!free(&fnh); /* Oops! freeing stack memory */! void FreeMemY() {!!int *fum = malloc(4 * sizeof(int));!!free(fum+1); /* fum+1 is not a proper handle; points to middle of a block */!!free(fum);!!free(fum); /* Oops! Attempt to free already freed memory */! 39 40 Summary C has three pools of data memory (+ code memory) Sta\c storage: global variable storage, basically permanent, en\re program run The Stack: local variable storage, parameters, return address The Heap (dynamic storage): malloc()/calloc() grabs space from here, free()/cfree() returns it, realloc() resizes space Common (Dynamic) Memory Problems Using unini\alized values Accessing memory beyond your allocated region Improper use of free/realloc by messing with the pointer handle returned by malloc/calloc Memory leaks: mismatched malloc/free pairs 41 7