CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 11: Managing Memory

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.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
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.
SPLINT STATIC CHECKING TOOL Sripriya Subramanian 10/29/2002.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Programming in C Pointers and Arrays, malloc( ). 7/28/092 Dynamic memory In Java, objects are created on the heap using reference variables and the new.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
1 Memory Allocation Professor Jennifer Rexford COS 217.
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.
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:
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #5 More about.
Informática II Prof. Dr. Gustavo Patiño MJ
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
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.
Run-Time Storage Organization
C strings (Reek, Ch. 9) 1CS 3090: Safety Critical Programming in C.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Memory Layout C and Data Structures Baojian Hua
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 10: *&!%[]++
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Stack and Heap Memory Stack resident variables include:
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
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.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
ECE Application Programming
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Today’s Material Strings Definition Representation Initialization
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 12: Automating Memory Management
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSE 220 – C Programming malloc, calloc, realloc.
Dynamic Allocation in C
Stack and Heap Memory Stack resident variables include:
Introduction to Programming
What the (Pointers in Rust)
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Memory Allocation CS 217.
Chapter 15 Pointers, Dynamic Data, and Reference Types
EECE.2160 ECE Application Programming
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Lecture 18: Deep C Garbage Collection CS201j: Engineering Software
EECE.2160 ECE Application Programming
Dynamic Memory A whole heap of fun….
CS216: Program and Data Representation
Dynamic Memory – A Review
Pointers, Dynamic Data, and Reference Types
EECE.2160 ECE Application Programming
Presentation transcript:

CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 11: Managing Memory

2 UVa CS216 Spring Lecture 11: Managing Memory Levels of Abstraction Python a “hello” C a: 0x80496f8 0x80496f9 h e l l o \0 0x80496fa 0x80496fb 0x80496fc 0x80496fd Digital Abstraction

3 UVa CS216 Spring Lecture 11: Managing Memory Physical Memory Vacuum Tube Capacitor (store charge) DRAM Magnetic Core

4 UVa CS216 Spring Lecture 11: Managing Memory Memory in C Stack Managed by compiler automatically Lifetime is determined by program scope –Cannot outlive procedure return Heap Managed by programmer explicitly Lifetime is controlled by programmer –Lives until freed by program

5 UVa CS216 Spring Lecture 11: Managing Memory Static/Dynamic Allocation Static: space required is known before program starts (at “compile time”) Dynamic: space required is not known before the program starts –Can be placed on either the stack or the heap

6 UVa CS216 Spring Lecture 11: Managing Memory malloc void *malloc (size_t size) Returns an untyped pointer (can point to anything) Size in bytes Malloc returns an address that is the location of at least size bytes of previously unused memory, and reserves that space. Or, returns null is there isn’t enough space.

7 UVa CS216 Spring Lecture 11: Managing Memory Memory Allocators Lifetime ScopedUnlimited Known Unknown malloc alloca – like malloc, but on the stack, not heap (rarely used) Size alloca local variable declarations global, static variable declarations

8 UVa CS216 Spring Lecture 11: Managing Memory Malloc Example char *s = (char *) malloc (sizeof(*s) * n) sizeof operator Takes a type or expression, evaluates to the number of bytes to store it type cast malloc returns void *, cast tells compiler we are using it as a char *

9 UVa CS216 Spring Lecture 11: Managing Memory String Concatenation #include int main (int argc, char **argv) { int i = 0; char *result = (char *) malloc (sizeof (*result)); *result = '\0'; while (i < argc) { char *s = (char *) malloc (sizeof (*s) * (strlen (result) + strlen (argv[i]) + 1)); strcpy (s, result); strcat (s, argv[i]); result = s; i++; } printf ("Concatenation: %s\n", result); return 0; } Some serious problems with this, we’ll discuss soon…

10 UVa CS216 Spring Lecture 11: Managing Memory Concating Strings char *s = (char *) malloc (sizeof (*s) * (strlen (result) + strlen (argv[i]) + 1)); strcpy (s, result); strcat (s, argv[i]); Why is the parameter to malloc what it is?

11 UVa CS216 Spring Lecture 11: Managing Memory string functions int strlen(char *s) – returns number of chars in s (not counting null terminator) char *strcpy(char *s1, const char *s2) – Copies the string pointed to by s2 (including the terminating null byte) into the space pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined. char *strcat(char *s1, const char * s2) - appends a copy of the string pointed to by s2 (including the terminating null byte) to the end of the string pointed to by s1. The initial byte of s2 overwrites the null byte at the end of s1. If copying takes place between objects that overlap, the behavior is undefined.

12 UVa CS216 Spring Lecture 11: Managing Memory Concating Strings char *s = (char *) malloc (sizeof (*s) * (strlen (result) + strlen (argv[i]) + 1)); strcpy (s, result); strcat (s, argv[i]); We need enough space for s to hold all the chars in result, all the chars in argv[i], and the null terminator.

13 UVa CS216 Spring Lecture 11: Managing Memory Memory Lifetimes #include int main (int argc, char **argv) { int i = 0; char *result = (char *) malloc (sizeof (*result)); *result = '\0'; while (i < argc) { char *s = (char *) malloc (sizeof (*s) * (strlen (result) + strlen (argv[i]) + 1)); strcpy (s, result); strcat (s, argv[i]); result = s; i++; } printf ("Concatenation: %s\n", result); return 0; } When is space allocated by malloc reclaimed?

14 UVa CS216 Spring Lecture 11: Managing Memory Reclaiming Storage Storage allocated by malloc is reserved forever Give it back by passing it to free void free(void *ptr); The free() function shall cause the space pointed to by ptr to be deallocated; that is, made available for further allocation. If ptr is a null pointer, no action shall occur. Otherwise, if the argument does not match a pointer earlier returned by malloc(), … (or a few other allocators) function, or if the space has been deallocated by a call to free(), the behavior is undefined.

15 UVa CS216 Spring Lecture 11: Managing Memory Plugging Memory Leaks #include int main (int argc, char **argv) { int i = 0; char *result = (char *) malloc (sizeof (*result)); *result = '\0'; while (i < argc) { char *s = (char *) malloc (sizeof (*s) * (strlen (result) + strlen (argv[i]) + 1)); strcpy (s, result); strcat (s, argv[i]); result = s; i++; } printf ("Concatenation: %s\n", result); return 0; } free (result);

16 UVa CS216 Spring Lecture 11: Managing Memory Memory Leak There is no reference to allocated storage –It can never be reached –It can never be reclaimed Losing references –Variable goes out of scope –Variable reassigned

17 UVa CS216 Spring Lecture 11: Managing Memory References char *result = (char *) malloc (sizeof (*result)); … while (i < argc) { char *s = (char *) malloc (…); strcpy (s, result); strcat (s, argv[i]); result = s; i++; } scope of s is closed – should we free(s) first? No! result now references same storage

18 UVa CS216 Spring Lecture 11: Managing Memory References char *result = (char *) malloc (sizeof (*result)); … while (i < argc) { char *s = (char *) malloc (…); strcpy (s, result); strcat (s, argv[i]); result = s; i++; } after this assignment, no way to reach storage result previously pointed to

19 UVa CS216 Spring Lecture 11: Managing Memory C vs. Python #include int main (int argc, char **argv) { int i = 0; char *result = (char *) malloc (sizeof (*result)); *result = '\0'; while (i < argc) { char *s = (char *) malloc (sizeof (*s) * (strlen (result) + strlen (argv[i]) + 1)); strcpy (s, result); free (result); strcat (s, argv[i]); result = s; i++; } printf ("Concatenation: %s\n", result); return 0; } import sys res = "" for arg in sys.argv: res += arg print res

20 UVa CS216 Spring Lecture 11: Managing Memory C vs. Python int main (int argc, char **argv) { char result[6] = "hello"; while (strlen(result) < :) { char *s = (char *) malloc (sizeof (*s) * (2 * strlen (result) + 1)); strcpy (s, result); strcat (s, result); free (result); result = s; } return 0; } res = "hello" while len(res) < : res += res > time python concat.py 4.46u 11.70s 9:40.04

21 UVa CS216 Spring Lecture 11: Managing Memory Python’s List Implementation trunk/Objects/listobject.c

22 UVa CS216 Spring Lecture 11: Managing Memory app1 static int app1(PyListObject *self, PyObject *v) { Py_ssize_t n = PyList_GET_SIZE(self); assert (v != NULL); if (n == INT_MAX) { PyErr_SetString(PyExc_OverflowError, "cannot add more objects to list"); return -1; } if (list_resize(self, n+1) == -1) return -1; Py_INCREF(v); PyList_SET_ITEM(self, n, v); return 0; }

23 UVa CS216 Spring Lecture 11: Managing Memory list_resize static int list_resize(PyListObject *self, Py_ssize_t newsize) { PyObject **items; size_t new_allocated; Py_ssize_t allocated = self->allocated; /* Bypass realloc() when a previous overallocation is large enough … */ if (allocated >= newsize && newsize >= (allocated >> 1)) { assert(self->ob_item != NULL || newsize == 0); self->ob_size = newsize; return 0; } … expr1 >> expr2 Value is value of expr1 with bits moved right value of expr2 times => x >> 1 is similar to (x / 2)

24 UVa CS216 Spring Lecture 11: Managing Memory … /* This over-allocates proportional to the list size, making room for additional * growth. The over-allocation is mild, but is enough to give linear-time * amortized behavior over a long sequence of appends() in the presence of a * poorly-performing system realloc(). * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88,... */ new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6) + newsize; if (newsize == 0) new_allocated = 0; items = self->ob_item; if (new_allocated <= ((~(size_t)0) / sizeof(PyObject *))) PyMem_RESIZE(items, PyObject *, new_allocated); else items = NULL; if (items == NULL) { PyErr_NoMemory(); return -1; } self->ob_item = items; self->ob_size = newsize; self->allocated = new_allocated; return 0;

25 UVa CS216 Spring Lecture 11: Managing Memory PyMem_Resize #define PyMem_RESIZE(p, type, n) \ ( (p) = (type *) PyMem_REALLOC((p), (n) * sizeof(type)) ) /* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL for malloc(0), which would be treated as an error. Some platforms would return a pointer with no memory behind it, which would break pymalloc. To solve these problems, allocate an extra byte. */ #define PyMem_MALLOC(n) malloc((n) ? (n) : 1) #define PyMem_REALLOC(p, n) realloc((p), (n) ? (n) : 1) PyMem_RESIZE(items, PyObject *, new_allocated) => items = (PyObject *) realloc (items, new_allocated * sizeof(PyObject *))

26 UVa CS216 Spring Lecture 11: Managing Memory realloc void *realloc(void *ptr, size_t size); The realloc() function changes the size of the memory object pointed to by ptr to the size specified by size. The contents of the object will remain unchanged up to the lesser of the new and old sizes. If the new size of the memory object would require movement of the object, the space for the previous instantiation of the object is freed. If the new size is larger, the contents of the newly allocated portion of the object are unspecified. If size is 0 and ptr is not a null pointer, the object pointed to is freed. If the space cannot be allocated, the object remains unchanged.

27 UVa CS216 Spring Lecture 11: Managing Memory realloc is risky int main (int argc, char **argv) { char *s = (char *) malloc (sizeof(*s) * 6); char *t; t = s; strcpy (s, "hello"); printf ("s = %s [%p] [%p]\n", s, s, t); s = (char *) realloc (s, sizeof(*s) * 7 ); printf ("s = %s [%p] [%p]\n", s, s, t); strcpy (s, "cheers"); printf ("s = %s, t = %s\n", s, t); } s = hello [20a80] [20a80] s = cheers, t = cheers 20 s = hello [20a80] [20a80] s = hello [20e88] [20a80] s = cheers, t = hello

28 UVa CS216 Spring Lecture 11: Managing Memory realloc void *realloc(void *ptr, size_t size); The realloc() function changes the size of the memory object pointed to by ptr to the size specified by size. The contents of the object will remain unchanged up to the lesser of the new and old sizes. If the new size of the memory object would require movement of the object, the space for the previous instantiation of the object is freed. If the new size is larger, … After a realloc, any use of the original location of ptr has undefined behavior!

29 UVa CS216 Spring Lecture 11: Managing Memory Running time of realloc Time to reserve new space: O(1) Time to copy old data into new space: –O(n) where n is the size of the old data So how can Python’s list append be O(1) ?

30 UVa CS216 Spring Lecture 11: Managing Memory static int list_resize(PyListObject *self, Py_ssize_t newsize) { PyObject **items; size_t new_allocated; Py_ssize_t allocated = self->allocated; … /* This over-allocates proportional to the list size, making room for additional * growth. The over-allocation is mild, but is enough to give linear-time * amortized behavior over a long sequence of appends() in the presence of a * poorly-performing system realloc(). The growth pattern is: 0, 4, 8, 16, 25, …*/ new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6) + newsize; if (newsize == 0) new_allocated = 0; items = self->ob_item; if (new_allocated <= ((~(size_t)0) / sizeof(PyObject *))) PyMem_RESIZE(items, PyObject *, new_allocated); else items = NULL; if (items == NULL) { PyErr_NoMemory(); return -1; } self->ob_item = items; self->ob_size = newsize; self->allocated = new_allocated; return 0;

31 UVa CS216 Spring Lecture 11: Managing Memory When to grow Approximately: newsize / 8 + newsize So, we have to do O(n) work every approximately every n/8 calls to append Amortized: O(n / (n / 8)) = O(1) But…some calls will be more expensive than others new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6) + newsize;

32 UVa CS216 Spring Lecture 11: Managing Memory Charge Section: practice with pointers Reading: Chapter 10 Wednesday: –Reference Counting –Garbage Collection –PS4 out