1 Memory Allocation Professor Jennifer Rexford COS 217.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Dynamic Memory Management
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Dynamically Allocated Memory String CGS 3460, Lecture 33 Apr 3, 2006 Hen-I Yang.
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
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
User-Level Memory Management in Linux Programming
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
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:
CPSC 388 – Compiler Design and Construction
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’;
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
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.
C and Data Structures Baojian Hua
Run-time Environment and Program Organization
1 Inner Workings of Malloc and Free Professor Jennifer Rexford COS 217.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
Memory Layout C and Data Structures Baojian Hua
Pointers Applications
1 Dynamic Memory Management. 2 What’s worse?  50% of instructions/data are cache misses (fetched from RAM)  1% of instructions/data are page faults.
Chapter 3.1:Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
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
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Lecture 13 Static vs Dynamic Memory Allocation
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
Topic 2d High-Level languages and Systems Software
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.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
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.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Dynamic Memory Management Jennifer Rexford 1. 2 Goals of this Lecture Dynamic memory management techniques Garbage collection by the run-time system (Java)
Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of daily. If you me and.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSE 220 – C Programming malloc, calloc, realloc.
Object Lifetime and Pointers
Dynamic Storage Allocation
Stack and Heap Memory Stack resident variables include:
Advanced Programming with C
Day 03 Introduction to C.
Day 03 Introduction to C.
Checking Memory Management
CSCI206 - Computer Organization & Programming
Dynamic Memory Allocation
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Memory Allocation CS 217.
EECE.2160 ECE Application Programming
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Dynamic Memory – A Review
Module 13 Dynamic Memory.
Presentation transcript:

1 Memory Allocation Professor Jennifer Rexford COS 217

2 Goals for Today’s Lecture Behind the scenes of running a program  Code, executable, and process  Main memory vs. virtual memory Memory layout for UNIX processes, and relationship to C  Text: code and constant data  Data: initialized global and static variables  BSS: uninitialized global and static variables  Heap: dynamic memory  Stack: local variables C functions for memory management  malloc : allocate memory from the heap  free : deallocate memory from the heap

3 Code vs. Executable vs. Process C source code  C statements organized into functions  Stored as a collection of files (.c and.h) Executable module  Binary image generated by compiler  Stored as a file (e.g., a.out) Process  Instance of a program that is executing –With its own address space in memory –With its own id and execution state  Managed by the operating system executable C source code process compiling running

4 Main Memory on a Computer What is main memory?  Storage for variables, data, code, etc.  May be shared among many processes CPU Memory Disk Network Video Audio Data Bus

5 Virtual Memory for a Process What is virtual memory?  Contiguous addressable memory space for a single process  May be swapped into physical memory from disk in pages  Let’s you pretend each process has its own contiguous memory 0 0xffffffff Virtual Address Space CPU Memory Disk Network Video Audio Data Bus 32

6 What to Store: Code and Constants Executable code and constant data  Program binary, and any shared libraries it loads  Necessary for OS to read the commands OS knows everything in advance  Knows amount of space needed  Knows the contents of the memory Known as the “text” segment Note: Some systems (e.g., hats) store some constants in “rodata” section 0 0xffffffff Text

7 What to Store: “Static” Data Variables that exist for the entire program  Global variables, and “static” local variables  Amount of space required is known in advance Data: initialized in the code  Initial value specified by the programmer –E.g., “ int x = 97 ;”  Memory is initialized with this value BSS: not initialized in the code  Initial value not specified –E.g., “ int x ;”  All memory initialized to 0 (on most OS’s)  BSS stands for “Block Started by Symbol” 0 0xffffffff Text Data BSS

8 What to Store: Dynamic Memory Memory allocated while program is running  E.g., allocated using the malloc() function –And deallocated using the free() function OS knows nothing in advance  Doesn’t know the amount of space  Doesn’t know the contents So, need to allow room to grow  Known as the “heap”  Detailed example in a few slides  More in programming assignment #4 0 0xffffffff Text Data BSS Heap

9 What to Store: Temporary Variables Temporary memory during lifetime of a function or block  Storage for function parameters and local variables Need to support nested function calls  One function calls another, and so on  Store the variables of calling function  Know where to return when done So, must allow room to grow  Known as the “stack”  Push on the stack as new function is called  Pop off the stack as the function ends Detailed example later on 0 0xffffffff Text Data BSS Heap Stack

10 Memory Layout: Summary Text: code, constant data Data: initialized global & static variables BSS: uninitialized global & static variables Heap: dynamic memory Stack: local variables 0 0xffffffff Text Data BSS Heap Stack

11 Memory Layout Example char* string = “hello”; int iSize; char* f(void) { char* p; iSize = 8; p = malloc(iSize); return p; } Text Data BSS Stack Heap

12 Memory Layout Example: Text char* string = “hello”; int iSize; char* f(void) { char* p; iSize = 8; p = malloc(iSize); return p; } Text Data BSS Stack Heap

13 Memory Layout Example: Data char* string = “hello”; int iSize; char* f(void) { char* p; iSize = 8; p = malloc(iSize); return p; } Text Data BSS Stack Heap

14 Memory Layout Example: BSS char* string = “hello”; int iSize; char* f(void) { char* p; iSize = 8; p = malloc(iSize); return p; } Text Data BSS Stack Heap

15 Memory Layout Example: Heap char* string = “hello”; int iSize; char* f(void) { char* p; iSize = 8; p = malloc(iSize); return p; } Text Data BSS Stack Heap

16 Memory Layout Example: Stack char* string = “hello”; int iSize; char* f(void) { char* p; iSize = 8; p = malloc(iSize); return p; } Text Data BSS Stack Heap

17 Memory Allocation & Deallocation How, and when, is memory allocated?  Global and static variables: program startup  Local variables: function call  Dynamic memory: malloc() How is memory deallocated?  Global and static variables: program finish  Local variables: function return  Dynamic memory: free() All memory deallocated when program ends  It is good style to free allocated memory anyway Text Data BSS Stack Heap

18 Memory Allocation Example char* string = “hello”; int iSize; char* f(void) { char* p; iSize = 8; p = malloc(iSize); return p; } BSS: 0 at startup Data: “hello” at startup Stack: at function call Heap: 8 bytes at malloc

19 Memory Deallocation Example char* string = “hello”; int iSize; char* f(void) { char* p; iSize = 8; p = malloc(iSize); return p; } Available till termination Deallocate on return from f Deallocate on free() Available till termination

20 Memory Initialization Local variables have undefined values int count; Memory allocated by malloc() has undefined values char* p = (char *) malloc(8); Must explicitly initialize if you want a particular initial value int count = 0; p[0] = ’\0’; Global and static variables are initialized to 0 by default static int count = 0; is the same as static int count; It is bad style to depend on this

21 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1

22 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p2

23 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p2 p3

24 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p2 p3

25 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p2 p3 p4

26 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p2 p3 p4

27 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p5, p2 p3 p4

28 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p5, p2 p3 p4

29 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p5, p2 p3 p4

30 Heap: Dynamic Memory #include void *malloc(size_t size); void free(void *ptr); 0 0xffffffff Text Data BSS Stack } Heap char *p1 = malloc(3); char *p2 = malloc(1); char *p3 = malloc(4); free(p2); char *p4 = malloc(6); free(p3); char *p5 = malloc(2); free(p1); free(p4); free(p5); p1 p5, p2 p3 p4

31 How Do Malloc and Free Work? Simple answer  Doesn’t matter  Good modularity means you can use it without understanding it Real answer malloc(s) n =  s / sizeof(int)  n words of user data n1 word of overhead free(p) put p into linked list of free objects n

32 Using Malloc and Free Types  void* : generic pointer to any type (can be converted to other types)  size_t : unsigned integer type returned by sizeof() void* malloc(size_t size)  Returns a pointer to space of size size  … or NULL if the request cannot be satisfied  E.g., int* x = (int *) malloc(sizeof(int)); void* calloc(size_t nobj, size_t size)  Returns a pointer to space for array of nobj objects of size size  … or NULL if the request cannot be satisfied  Bytes are initialized to 0 void free(void* p)  Deallocate the space pointed to by the pointer p  Pointer p must be pointer to space previously allocated  Do nothing if p is NULL

33 Using realloc and (never) alloca void* realloc(void* ptr, size_t size)  “Grows” the allocated buffer  Moves/copies the data if old space insufficient  … or NULL if the request cannot be satisfied void* alloca(size_t size)  Not guaranteed to exist (not in any official standard)  Allocates space on local stack frame  Space automatically freed when function exits  Particularly useful for following: int calc(int numItems) { int items[numItems]; int *items = alloca(numItems * sizeof(int)); }

34 Examples Motivating Realloc() Example programming tasks  Read text consisting of multiple lines (ending in ‘\n’)  Print the lines in alphabetical order Suppose you don’t know the maximum line size in advance  Could pick a maximum line size in advance  E.g., #define MAXCHAR 200  But, what if you need to read and store a longer line? And you don’t know the number of lines in advance  Could pick a maximum number of lines in advance  E.g., #define MAXLINE  But, what if the input has more lines than this? Better to (re)allocate more memory as you go along

35 Printing Chars in Reverse Order #define MAXCHAR 1000 int main(void) { char a[MAXCHAR]; int i, c; for (i=0; i<MAXCHAR && ((c=getchar()) != EOF); i++) a[i] = c; while (i > 0) putchar(a[--i]); putchar('\n'); }

36 And Again With Realloc() #define INIT_SIZE 5 int main(void) { char* a; int i, c, size=INIT_SIZE; a = (char *) malloc(size * sizeof(char)); for (i=0; ((c=getchar()) != EOF); i++) { if (i >= size) { size *= 2; a = (char *) realloc(a, size * sizeof(char)); } a[i] = c; } … free(a); }

37 Avoid Leaking Memory Memory leaks “lose” references to dynamic memory int f(void) { char* p; p = (char *) malloc(8 * sizeof(char)); … return 0; } int main(void) { f(); … }

38 Avoid Dangling Pointers Dangling pointers point to data that’s not there anymore char *f(void) { char p[8]; … return p; } int main(void) { char *res = f(); … }

39 Debugging Malloc Problems Symptom: “random” failures, especially on call return  Corrupted the stack frame return info Symptom: calls to malloc/free fail  Corrupted the malloc bookkeeping data Symptom: program magically works if printf inserted  Corrupted storage space in stack frame “Debugging” mallocs exist  Doing “man malloc” on Linux reveals MALLOC_CHECK_  Searching “debug malloc” yields dmalloc, other libraries  Larger problems: valgrind, electric fence, etc.

40 Summary Five types of memory for variables  Text: code, constant data (constant data in rodata on hats)  Data: initialized global & static variables  BSS: uninitialized global & static variables  Heap: dynamic memory  Stack: local variables Important to understand differences between  Allocation: space allocated  Initialization: initial value, if any  Deallocation: space reclaimed Understanding memory allocation is important  Make efficient use of memory  Avoid “memory leaks” from dangling pointers