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

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

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?

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

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. Memory Management and Usage

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

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

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

Lectures 13 & 14. memory management

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

CS61C : Machine Structures

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

CS61C : Machine Structures

CS61C : Machine Structures

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

More On Memory (mis)-management

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

HW1 due Monday by 9:30am Assignment online, submission details to come

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

Arrays and Memory Management

Reference slides! Garcia, Fall 2011 UCB! CS61C L04 Introduction to C (pt 2) (1)!

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = "abc";! How do you tell how long a string is?!

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

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

CS61C : Machine Structures

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

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

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

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

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

ECE 15B COMPUTER ORGANIZATION

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

CS61C : Machine Structures

CS61C : Machine Structures

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

More C Pointer Dangers

CS 11 C track: lecture 5

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

CS 61c: Great Ideas in Computer Architecture

CS61C : Machine Structures

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

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

C Arrays, Strings, More Pointers Instructor: Steven Ho

Lecture 7: Procedures and Program Execution Preview

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

CSC 1600 Memory Layout for Unix Processes"

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

Lecture 8 Dynamic Memory Allocation

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

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

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

CS61, Fall 2012 Section 2 Notes

Memory Analysis tools

Dynamic memory allocation (malloc)

How about them A s!! Go Oaktown!! CS61C - Machine Structures. Lecture 4 C Structures Memory Management Dan Garcia.

Introduction to Computer Architecture

Lecture 10: Program Development versus Execution Environment

CS61C : Machine Structures

Memory (Stack and Heap)

Programs in memory. The layout of memory is roughly:

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

Intermediate Programming, Spring 2017*

Memory Management. CS449 Fall 2017

Has there been an update to ANSI C? Lecture #3 C Pointers Yes! It s called the C99 or C9x std

IT 252 Computer Organization and Architecture Introduction to the C Programming Language

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

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

DAY 3. CS3600, Northeastern University. Alan Mislove

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

Debugging (Part 2) 1

CS 222: Pointers and Manual Memory Management

Lecture 2: C Programm

Pointers and Memory Management

C Structures & Dynamic Memory Management

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

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

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

Kurt Schmidt. October 30, 2018

CS61C Midterm Review on C & Memory Management

Dynamic Allocation in C

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

Heap Arrays. Steven R. Bagley

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

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

Instructor: Randy H. Katz hbp://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #4. Agenda

Arrays and Pointers. CSE 2031 Fall November 11, 2013

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

Quick review pointer basics (KR ch )

Scientific Programming in C IV. Pointers

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Fall 2018 Discussion 2: September 3, 2018

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

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Dynamic Allocation in C

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

Transcription:

Agenda CS 61C: Great Ideas in Computer Architecture (Machine Structures) Memory Management and Malloc Instructors: Michael Greenbaum http://inst.eecs.berkeley.edu/~cs61c/su11 Pointers Review C Memory Management Administrivia Malloc and Free Break Common Memory Problems 1 2 Pointer Arithmetic pointer + number, pointer number E.g., pointer + 1 adds 1 something to a pointer (in both examples, pretend variable a sits at address 100) char *p; char a; p = &a; Adds 1*sizeof(char) to the memory address What does the following line yield? printf( %u %u\n,p,p+1); Pointer arithmetic should be used cautiously int *p; int a; p = &a; 100 101 100 104 Adds 1*sizeof(int) to the memory address Pointer Arithmetic p+1 correctly computes a ptr to the next array element automatically depending on sizeof(type) What if we have an array of large structs (objects)? C takes care of it in the same way it handles basic types. 6/23/2011 Spring 2011 -- Lecture #4 3 6/23/2011 Spring 2011 -- Lecture #4 4 Pointer Arithmetic to Copy Memory We can use pointer arithmetic to walk through memory: void copy(int *from, int *to, int n) int i; for (i=0; i<n; i++) *to++ = *from++; Note we had to pass size (n) to copy Pointer Arithmetic: Peer Instruction Question How many of the following are invalid? I. pointer + integer II. integer + pointer III. pointer + pointer IV. pointer integer V. integer pointer VI. pointer pointer VII. compare pointer to pointer VIII. compare pointer to integer IX. compare pointer to 0 X. compare pointer to NULL #invalid a: 1 b: 2 c: 3 d: 4 e: 5 6/23/2011 Spring 2011 -- Lecture #4 5 6/23/2011 Spring 2011 -- Lecture #4 6 1

Pointer Arithmetic: Peer Instruction Answer How many of the following are invalid? I. pointer + integer II. integer + pointer III. pointer + pointer IV. pointer integer V. integer pointer VI. pointer pointer VII. compare pointer to pointer VIII. compare pointer to integer IX. compare pointer to 0 X. compare pointer to NULL ptr + 1 1 + ptr ptr + ptr ptr - 1 1 - ptr ptr - ptr ptr1 == ptr2 ptr == 1 ptr == NULL ptr == NULL #invalid a: 1 b: 2 c: 3 d: 4 e: 5 Pointer Arithmetic What is valid pointer arithmetic? Add an integer to a pointer Subtract 2 pointers (in the same array) Compare pointers (<, <=, ==,!=, >, >=) Compare pointer to NULL (indicates that the pointer points to nothing) Everything else is illegal since it makes no sense: Adding two pointers Multiplying pointers Subtract pointer from integer 6/23/2011 Spring 2011 -- Lecture #4 7 6/23/2011 Spring 2011 -- Lecture #4 8 Pointers and Functions (1/4) Sometimes you want to have a procedure increment a variable What gets printed? void AddOne(int x) x = x + 1; int y = 5; AddOne(y); printf( y = %d\n, y); y = 5 Pointers and Functions (2/4) Solved by passing in a pointer to our subroutine. Now what gets printed? void AddOne(int *p) *p = *p + 1; int y = 5; AddOne(&y); printf( y = %d\n, y); y = 6 6/23/2011 Spring 2011 -- Lecture #4 9 6/23/2011 Spring 2011 -- Lecture #4 10 Pointers and Functions (3/4) But what if the thing you want changed is a pointer? What gets printed? void IncrementPtr(int p = p + 1; int A[3] = 50, 60, 70; int *q = A; IncrementPtr(q); printf( *q = %d\n, *q); *p) *q = 50 A q 50 60 70 Pointers and Functions (4/4) Solution! Pass a pointer to a pointer, declared as **h Now what gets printed? void IncrementPtr(int **h) *h = *h + 1; int A[3] = 50, 60, 70; int *q = A; IncrementPtr(&q); printf( *q = %d\n, *q); *q = 60 A q q 50 60 70 6/23/2011 Spring 2011 -- Lecture #4 11 6/23/2011 Spring 2011 -- Lecture #4 12 2

Agenda Pointers Review C Memory Management Administrivia Malloc and Free Break Common Memory Problems 13 C Memory Layout Program s address space contains 4 regions: stack: local variables, grows downward heap: space requested for pointers via malloc(); resizes dynamically, grows upward static data: variables declared outside main, does not grow or shrink code: loaded when program starts, does not change. ~ FFFF FFFF hex stack heap static data code ~ 0 hex OS prevents accesses between stack and heap (via virtual memory) 14 Where are Variables Allocated? The Stack If declared outside a procedure, allocated in static 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 15 Stack frame includes: Location of who called the function Parameters Space for local variables Stack frames contiguous blocks of memory; stack pointer tells where bottom stack frame is When procedure ends, stack frame is tossed off the stack; frees memory for future stack frames SP frame frame frame frame 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 Stack Pointer grows down Stack Pointer Stack Pointer Stack Pointer Stack Pointer stack What s Wrong with this Code? int *getptr() int y; y = 3; return &y; ; main () int *stackaddr,content; stackaddr = getptr(); content = *stackaddr; printf("%d", content); /* 3 */ content = *stackaddr; printf("%d", content); /*13451514 */ ; 17 18 3

What s Wrong with this Code? int *getptr() int y; y = 3; return &y; ; SP main getptr() (y==3) main main () int *stackaddr,content; stackaddr = getptr(); content = *stackaddr; printf("%d", content); /* 3 */ content = *stackaddr; printf("%d", content); /*13451514 */ ; 19 SP main printf() (y==?) SP Red: printf overwrites stack frame Never return pointers to local variable from functions Your compiler will warn you about this don t ignore such warnings! Arrays not implemented as you d think void foo() int *p, *q, x; int a[4]; p = malloc (sizeof(int)); q = &x; *p = 1; printf("*p:%u, p:%u, &p:%u\n", *p, p, &p); *q = 2; printf("*q:%u, q:%u, &q:%u\n", *q, q, &q); *a = 3; printf("*a:%u, a:%u, &a:%u\n", *a, a, &a);... 0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60... 40? 20? 2? 3?? 1... p q x unnamed-malloc-space? 24 a *p:1, p:40, &p:12 *q:2, q:20, &q:16 *a:3, a:24, &a:24 K&R: An array name is not a variable Agenda Administrivia Pointers Review C Memory Management Administrivia Malloc and Free Break Common Memory Problems Future readings will be added soon. Definitely by tomorrow. Remember, Piazzza s a great resource for questions about assignments, material, or the course. Start HW1 if you haven t already. TA and Instructor response time increase drastically over the weekend! 21 6/23/2011 Fall 2010 -- Lecture #32 22 Agenda Pointers Review C Memory Management Administrivia Malloc and Free Break Common Memory Problems Using the Heap (1/3) C supports five functions for heap management: malloc(), calloc(), free(), cfree(), realloc() malloc(n): Allocate a block of uninitialized memory NOTE: Subsequent calls need not yield blocks in continuous sequence n is an integer, indicating size of allocated memory block in bytes sizeof determines size of given type (or var) in bytes, produces more portable code Returns a pointer to that memory location; NULL return indicates no more memory Example: int *ip; ip = malloc(sizeof(int)); struct treenode *tp; tp = malloc(sizeof(struct tnode)); 23 24 4

Using the Heap (2/3) Using the Heap (3/3) Most often, malloc used to allocate space for an array of items. int *p = malloc (n*sizeof(int)); Allocates space for n integers. Can use pointer or array syntax on this, as usual p[0] = 5; p++; //VERY BAD! Don t lose the original address malloc returned or you can t free it! 25 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); 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 exception! 26 Malloc Internals Many calls to malloc and free with many different size blocks. Where to place them? Want system to be fast, minimal memory overhead. Want to avoid Fragmentation, the tendency of free space on the heap to get separated into small chunks. Some clever algorithms to do this, we will not cover them here. Fragmentation 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? R2 (1 byte) R1 (100 bytes) 27 An example Fragmentation 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? R2 (1 byte) R3? R3? Agenda Pointers Review C Memory Management Administrivia Malloc and Free Break Common Memory Problems 30 5

Agenda Pointers Review C Memory Management Administrivia Malloc and Free Break Common Memory Problems Segmentation Fault vs. Bus Error http://www.hyperdictionary.com/ Bus Error A fatal failure in the execution of a machine language instruction resulting from the processor detecting an anomalous condition on its bus. Such conditions include invalid address alignment (accessing a multi-byte number at an odd address), accessing a physical address that does not correspond to any device, or some other device-specific hardware error. A bus error triggers a processor-level exception which Unix translates into a SIGBUS signal which, if not caught, will terminate the current process. Segmentation Fault An error in which a running Unix program attempts to access memory not allocated to it and terminates with a segmentation violation error and usually a core dump. 31 6/23/2011 Spring 2011 -- Lecture #4 32 Common Memory Problems Using uninitialized 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) 6/23/2011 Fall 2010 -- Lecture #33 33 Runtime analysis tools for finding memory errors Dynamic analysis tool: collects information on memory management while program runs Contrast with static analysis tool like lint, which analyzes source code without compiling or executing it No tool is guaranteed to find ALL memory bugs this is a very challenging programming language research problem You will be introduced to Valgrind in Lab #3! Debugging Tools http://valgrind.org 34 Using Uninitialized Values Using Uninitialized Values void foo(int *pi) int j; *pi = j; void bar() int i=10; foo(&i); printf("i = %d\n", i); 35 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 */ 36 6

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== Conditional jump or move depends on uninitialised value(s) ==98863== at 0x100031A1E: vfprintf (in /usr/lib/libsystem.b.dylib) ==98863== by 0x100073BCA: vfprintf_l (in /usr/lib/libsystem.b.dylib) ==98863== by 0x1000A11A6: printf (in /usr/lib/libsystem.b.dylib) ==98863== by 0x100000EEE: main (slide21.c:13) ==98863== Uninitialised value was created by a stack allocation ==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== still 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 37 38 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? */ 39 40 struct Profile char *name; int age; struct Profile person = malloc(sizeof(struct Profile)); char* name = getname(); person.name = malloc(sizeof(char)*strlen(name)); strcpy(person.name,name); //Do stuff (that isn t buggy) free(person); free(person.name); struct Profile char *name; int age; strlen only counts # of characters in a string. Need to add 1 for null terminator! struct Profile person = malloc(sizeof(struct Profile)); char* name = getname(); person.name = malloc(sizeof(char)*(strlen(name)+1)); strcpy(person.name,name); //Do stuff (that isn t buggy) free(person); Accessing memory after you ve already freed it. These statements should be in the reverse order free(person.name); 41 42 7

Improper matched usage of mem handles int* init_array(int *ptr, int new_size) ptr = realloc(ptr, new_size*sizeof(int)); //Returns new block of memory 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; 43 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 entire 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 location? 44 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; Function returns pointer to stack memory won t be valid after function returns 6/23/2011 Fall 2010 -- Lecture #33 45 6/23/2011 Fall 2010 -- Lecture #33 46 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); 47 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'; /* Write Beyond Array Bounds */ printf("%s\n", str); /* Read Beyond Array Bounds */ 48 8

Faulty Heap Management Faulty Heap Management Memory leak: more mallocs than frees int *pi; void foo() pi = malloc(8*sizeof(int)); free(pi); void main() pi = malloc(4*sizeof(int)); foo(); 49 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 */ 50 Faulty Heap Management int *plk = NULL; void genplk() plk = malloc(2 * sizeof(int)); plk++; Faulty Heap Management Potential memory leak handle has been changed, do you still 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! */ 51 52 Faulty Heap Management void FreeMemX() int fnh = 0; free(&fnh); void FreeMemY() int *fum = malloc(4 * sizeof(int)); free(fum+1); free(fum); free(fum); Faulty Heap Management 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 */ 53 54 9

In Conclusion.. Reference slides C has three pools of data memory (+ code memory) Static storage: global variable storage, basically permanent over entire program run The Stack: local variable storage, parameters, return address The Heap (dynamic storage): malloc() allocates space from here, free() returns it. Common (Dynamic) Memory Problems Using uninitialized 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 You ARE responsible for the material on these slides (they re just taken from the reading anyway) ; we ve moved them to the end and off-stage to give more breathing room to lecture! 55 Using the Heap Using the Heap 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 initialized 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): cfree() releases the memory allocated by calloc(); E.g.: cfree(ip); 57 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, deallocating 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) */ 58 Let s look at an example of using structures, pointers, malloc(), and free() to implement a linked list of strings. /* node structure for linked list */ struct Node char *value; struct Node *next; ; Recursive definition! typedef simplifies the code struct Node char *value; struct Node *next; ; /* "typedef" means define a new type */ typedef struct Node NodeStruct; OR typedef struct Node char *value; struct Node *next; NodeStruct; THEN typedef NodeStruct *List; typedef char *String; String value; 10

/* Add a string to an existing list */ /* Add a string to an existing list, 2nd call */ String s1 =, s2 = "cde"; List thelist = NULL; thelist = cons(s2, thelist); thelist = cons(s1, thelist); /* or, just like (cons s1 (cons s2 nil)) */ thelist = cons(s1, cons(s2, NULL)); node:? list: s: NULL /* Add a string to an existing list, 2nd call */ /* Add a string to an existing list, 2nd call */ node:?? list: s: NULL node:? "????" list: s: NULL /* Add a string to an existing list, 2nd call */ /* Add a string to an existing list, 2nd call */ node:? list: s: NULL node: list: s: NULL 11

/* Add a string to an existing list, 2nd call */ node: s: NULL 12