Memory Allocation Alan L. Cox Objectives Be able to recognize the differences between static and dynamic memory allocation Be able to use.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Chapter 6 Data Types
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
CS 536 Spring Run-time organization Lecture 19.
Run time vs. Compile time
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
Run-time Environment and Program Organization
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Arrays and Pointers in C Alan L. Cox
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Outline Midterm results Static variables Memory model
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
Introduction to Data Structures Systems Programming.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Stack and Heap Memory Stack resident variables include:
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Introduction to Data Structures Systems Programming Concepts.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
ECE Application Programming
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Memory-Related Perils and Pitfalls in C
CSE 220 – C Programming malloc, calloc, realloc.
Dynamic Allocation in C
Stack and Heap Memory Stack resident variables include:
CSCI206 - Computer Organization & Programming
Memory Management III: Perils and pitfalls Mar 13, 2001
Dynamic Memory Allocation
Memory Allocation CS 217.
Dynamic Memory – A Review
Presentation transcript:

Memory Allocation Alan L. Cox

Objectives Be able to recognize the differences between static and dynamic memory allocation Be able to use malloc() and free() to manage dynamic memory in your programs Be able to analyze programs for memory management related bugs CoxMemory Allocation2

CoxMemory Allocation3 Big Picture C gives you access to underlying data representations & layout  Needed for systems programming  Dangerous for application programming  Necessary to understand Memory is a finite sequence of fixed-size storage cells  Most machines view storage cells as bytes “byte-addresses” Individual bits are not addressable  May also view storage cells as words

CoxMemory Allocation4 Process Memory Unused User Stack Shared Libraries Heap Read/Write Data Read-only Code and Data Unused 0x7FFFFFFFFFFF 0x Loaded from the executable Created at runtime Shared among processes 47 bits of address space

CoxMemory Allocation5 Allocation For all data, memory must be allocated  Allocated = memory space reserved Two questions:  When do we know the size to allocate?  When do we allocate? Two possible answers for each:  Compile-time (static)  Run-time (dynamic)

CoxMemory Allocation6 How much memory to allocate? Sometimes obvious: Sometimes not:  How will these be used??? Will they point to already allocated memory (what we’ve seen so far)? Will new memory need to be allocated (we haven’t seen this yet)? char c; int array[10]; One byte 10 * sizeof(int) (= 40, usually) char *c; int *array; Is this going to point to one character or a string? How big will this array be?

CoxMemory Allocation7 malloc() Allocate memory dynamically  Pass a size (number of bytes to allocate) Finds unused memory that is large enough to hold the specified number of bytes and reserves it  Returns a void * that points to the allocated memory No typecast is needed for pointer assignment  Essentially equivalent to new in Java and C++ #include int *array = malloc(num_items * sizeof(int)); Won’t continually remind you of this

CoxMemory Allocation8 Using malloc() i and array are interchangeable  Arrays  pointers to the initial (0th) array element  i could point to an array, as well  May change over the course of the program Allocated memory is not initialized!  calloc zeroes allocated memory (otherwise, same as malloc ; details to come in lab) int *i; int *array; i = malloc(sizeof(int)); array = malloc(num_items * sizeof(int)); *i = 3; array[3] = 5; Statically allocates space for 2 pointers Dynamically allocates space for data

CoxMemory Allocation9 Using malloc() Always check the return value of system calls like malloc() for errors  For brevity, won’t in class Tutorial examples will  Textbook uses capitalization convention Capitalized version of functions are wrappers that check for errors and exit if they occur (i.e. Malloc ) May not be appropriate to always exit on a malloc error, though, as you may be able to recover memory int *a = malloc(num_items * sizeof(int)); if (a == NULL) { fprintf(stderr,“Out of memory.\n”); exit(1); } Terminate now! And, return 1.

CoxMemory Allocation10 When to Allocate? Static time  Typically global variables:  Only one copy ever exists, so can allocate at compile-time Dynamic time  Typically local variables:  One copy exists for each call – may be unbounded # of calls, so can’t allocate at compile-time int value; int main(void) { … } int f(…) { int value; … } int main(void) { … }

CoxMemory Allocation11 When to Allocate? Static time  Some local variables: int f(…) { static int value; … } int main(void) { … } Confusingly, local static has nothing to do with global static ! One copy exists for all calls – allocated at compile-time

CoxMemory Allocation12 Allocation in Process Memory Stack Shared Libraries Heap Read/Write Data Read-only Code and Data 0x7FFFFFFFFFFF 0x Dynamic size, dynamic allocation Static size, dynamic allocation Static size, static allocation Global variables (and static local variables) Local variables Programmer controlled (variable-sized objects) 0x x39A

CoxMemory Allocation13 Deallocation Space allocated via declaration (entering scope) is deallocated when exiting scope  Can’t refer to y or array outside of f(), so their space is deallocated upon return … f(void) { int y; int array[10]; … }

CoxMemory Allocation14 Deallocation malloc() allocates memory explicitly  Must also deallocate it explicitly (using free() )!  Not automatically deallocated (garbage collected) as in Python, Scheme, and Java  Forgetting to deallocate leads to memory leaks & running out of memory Must not use a freed pointer unless reassigned or reallocated int *a = malloc(num_items * sizeof(int)); … free(a); … a = malloc(2 * num_items * sizeof(int));

CoxMemory Allocation15 Deallocation Space allocated by malloc() is freed when the program terminates  If data structure is used until program termination, don’t need to free  Entire process’ memory is deallocated

CoxMemory Allocation16 Back to create_date Date * create_date3(int month, int day, int year) { Date *d; d->month = month; d->day = day; d->year = year; return (d); } Date * create_date3(int month, int day, int year) { Date *d; d = malloc(sizeof(Date)); d->month = month; d->day = day; d->year = year; return (d); }

CoxMemory Allocation17 Pitfall void foo(void) { Date *today; today = create_date3(9, 1, 2005); /* use “today” */... return; } Memory is still allocated for “today”! Will never be deallocated (calling function doesn’t even know about it) Potential problem: memory allocation is performed in this function (may not know its implementation)

CoxMemory Allocation18 Possible Solutions void foo(void) { Date *today; today = create_date3(…); /* use “today” */... free(today); return; } void foo(void) { Date *today; today = create_date3(…); /* use “today” */... destroy_date(today); return; } Explicitly deallocate memory – specification of create_date3 must tell you to do this Complete the abstraction – “create” has a corresponding “destroy”

Common Memory Management Mistakes Cox19Memory Allocation

CoxMemory Allocation20 What’s Wrong With This Code? Consider j = *f() Leads to referencing deallocated memory  Never return a pointer to a local variable! Behavior depends on allocation pattern  Space not reallocated (unlikely)  works  Space reallocated  very unpredictable int *make_array(…) { int array[10]; … return (array); } int *f(…) { int i; … return (&i); }

CoxMemory Allocation21 One Solution Allocate with malloc(), and return its pointer  Upon return, space for local pointer variable is deallocated  But the malloc -ed space isn’t deallocated until it is free -d  Potential memory leak if caller is not careful, as with create_date3… int *f(…) { int *i_ptr = malloc(sizeof(int)); … return (i_ptr); } int *make_array(…) { int *array = malloc(10 * sizeof(int)); … return (array); }

CoxMemory Allocation22 What’s Wrong With This Code? malloc -ed & declared space is not initialized!  i, j, y[i] initially contain unknown data – garbage  Often has zero value, leading to seemingly correct results /* return y = Ax */ int *matvec(int **A, int *x) { int *y = malloc(N * sizeof(int)); int i, j; for (; i<N; i+=1) for (; j<N; j+=1) y[i] += A[i][j] * x[j]; return (y); } i=0 j=0 Initialization loop for y[]

CoxMemory Allocation23 What’s Wrong With This Code? Allocates wrong amount of memory  Leads to writing unallocated memory char **p; int i; /* Allocate space for M*N matrix */ p = malloc(M * sizeof(char)); for (i = 0; i < M; i++) p[i] = malloc(N * sizeof(char)); char *

Explanation CoxMemory Allocation24 Heap region in memory (each rectangle represents one byte) Assume M = N = 2, a memory address is 64 bit or 8 byte ` ` p = malloc(M * sizeof(char)); p[0] for (i = 0; i < M; i++) p[i] = malloc(N * sizeof(char));

Corrected code CoxMemory Allocation25 Heap region in memory (each rectangle represents one byte) Assume M = N = 2, a memory address is 64 bit or 8 byte ` ` p = malloc(M * sizeof(char *)); for (i = 0; i < M; i++) p[i] = malloc(N * sizeof(char)); p[0] p[1]

CoxMemory Allocation26 What’s Wrong With This Code? Off-by-1 error  Uses interval 0…M instead of 0…M-1  Leads to writing unallocated memory Be careful with loop bounds! char **p; int i; /* Allocate space for M*N matrix */ p = malloc(M * sizeof(char *)); for (i=0; i<=M; i+=1) p[i] = malloc(N * sizeof(char)); <

CoxMemory Allocation27 Using const const int * size  Pointer to a constant integer  Cannot write to *size int * const size  Constant pointer to an integer  Cannot modify the pointer (size)  Can write to *size char * xyz(char * to, const char * from) { char *save = to; for (; (*to = *from); ++from, ++to); return(save); }

CoxMemory Allocation28 What’s Wrong With This Code? t[] doesn’t have space for string terminator  Leads to writing unallocated memory One way to avoid: char *s = “ ”; … char t[7]; strcpy(t, s); char *s = “ ”; … char *t = malloc((strlen(s) + 1) * sizeof(char)); strcpy(t, s); char * strcpy(char * to, const char * from) { char *save = to; for (; (*to = *from); ++from, ++to); return(save); }

CoxMemory Allocation29 What’s Wrong With This Code? Misused pointer arithmetic  Search skips some data, can read unallocated memory, and might not ever see value  Should never add sizeof() to a pointer  Could consider rewriting this function & its uses to use array notation instead /* Search memory for a value. */ /* Assume value is present. */ int *search(int *p, int value) { while (*p > 0 && *p != value) p += sizeof(int); return (p); } p += 1;

CoxMemory Allocation30 What’s Wrong With This Code? Premature free()  Reads and writes deallocated memory Behavior depends on allocation pattern  Space not reallocated  works  Space reallocated  very unpredictable x = malloc(N * sizeof(int)); … free(x); … y = malloc(M * sizeof(int)); for (i = 0; i < M; i++) { y[i] = x[i]; x[i] += 1; }

CoxMemory Allocation31 What’s Wrong With This Code? Memory leak – doesn’t free malloc -ed space  Data still allocated, but inaccessible, since can’t refer to x Slows future memory performance void foo(void) { int *x = malloc(N * sizeof(int)); … return; } free(x);

CoxMemory Allocation32 What’s Wrong With This Code? struct ACons { int first; struct ACons *rest; }; typedef struct ACons *List; List cons(int first, List rest) { List item = malloc(sizeof(struct ACons)); item->first = first; item->rest = rest; return (item); } void foo(void) { List list = cons(1, cons(2, cons(3, NULL))); … free(list); return; } A peek at one way to define lists

Example continued CoxMemory Allocation33 Memory leak – frees only beginning of data structure  Remainder of data structure is still allocated, but inaccessible  Need to write deallocation (destructor) routines for each data structure

Putting it together… string pointer struct malloc() simple I/O simple string operations CoxMemory Allocation34

What does action1() do? CoxMemory Allocation35 struct thing { char *stuff; struct thing *another_thing; }; void action1(struct thing **y, const char *stuff) { struct thing *x = malloc(sizeof(struct thing)); /* Alternatively, x->stuff = strdup(stuff); */ x->stuff = malloc(strlen(stuff) + 1); strcpy(x->stuff, stuff); x->another_thing = *y; *y = x; } int main(void) { struct thing *y = NULL; action1(&y, "Cox"); #include action1() inserts a new node storing the specified string into the linked list

What does action2() do? CoxMemory Allocation36 struct thing { char *stuff; struct thing *another_thing; }; void action2(struct thing **y) { struct thing *x; while ((x = *y) != NULL) { printf("%s ", x->stuff); y = &x->another_thing; } putchar('\n'); } int main(void) { struct thing *y = NULL;... action2(&y); action2() prints the strings stored in the linked list nodes sequentially action2() prints the strings stored in the linked list nodes sequentially

What does action3() do? CoxMemory Allocation37 struct thing { char *stuff; struct thing *another_thing; }; int action3(struct thing **y, const char *stuff) { struct thing *x; while ((x = *y) != NULL) { if (strcmp(x->stuff, stuff) == 0) return (1); else y = &x->another_thing; } return (0); } int main(void) { struct thing *y = NULL;... action3(&y, "Cox"); action3() finds out whether a string is stored in the linked list action3() finds out whether a string is stored in the linked list

What does action4() do? CoxMemory Allocation38 struct thing { char *stuff; struct thing *another_thing; }; void action4(struct thing **y, const char *stuff) { struct thing *x; while ((x = *y) != NULL) { if (strcmp(x->stuff, stuff) == 0) { *y = x->another_thing; free(x->stuff); free(x); return; } else y = &x->another_thing; } int main(void) { struct thing *y = NULL;... action4(&y, "Cox"); style compromised to save space action4() deletes the first list node that stores the specified string action4() deletes the first list node that stores the specified string

CoxMemory Allocation39 Next Time Lab: Debugging Assembly