CSc 352 Freeing Dynamically Allocated Memory Saumya Debray Dept. of Computer Science The University of Arizona, Tucson

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
CSc 352 Programming Hygiene Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Lecture 10: Heap Management CS 540 GMU Spring 2009.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
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.
C Programming - Lecture 5
1 Memory Allocation Professor Jennifer Rexford COS 217.
Linked Lists... An introduction to creating dynamic data structures.
Malloc Recitation Section K (Kevin Su) November 5 th, 2012.
Hastings Purify: Fast Detection of Memory Leaks and Access Errors.
C Programming - Lecture 3 File handling in C - opening and closing. Reading from and writing to files. Special file streams stdin, stdout & stderr. How.
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Linked Lists. Array Limitations Arrays have a fixed size that cannot be changed at run time What if your program had an array to store info regarding.
CPSC 388 – Compiler Design and Construction
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
CSE 451: Operating Systems Section 1. Why are you here? 9/30/102.
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.
Chapter 10 Storage Management Implementation details beyond programmer’s control Storage/CPU time trade-off Binding times to storage.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
CSc 352 C : Arrays, Structs, Pointers
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
CS-2303 System Programming Concepts
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
Memory Allocation CS Introduction to Operating Systems.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
Week 9 Part 1 Kyle Dewey. Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2.
General Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
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.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
Introduction to Data Structures Systems Programming Concepts.
UFS003C3 Lecture 15 Data type in C & C++ Using the STL.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
1 Debugging (Part 2). “Programming in the Large” Steps Design & Implement Program & programming style (done) Common data structures and algorithms Modularity.
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.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Announcements Remember what we talked on Tuesday in terms of Makefiles and phony targets. Don’t lose points for this! BTW, the.PHONY target can appear.
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.
CSE 374 Programming Concepts & Tools
Valgrind Overview What is Valgrind?
University of Washington Computer Programming I
Dynamic Memory Allocation
Storage.
Memory Management III: Perils and pitfalls Mar 13, 2001
Dynamic Memory Allocation
Memory Allocation CS 217.
Hassan Khosravi / Geoffrey Tien
EECE.2160 ECE Application Programming
CSc 352: Testing and Code Coverage
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science
CS5123 Software Validation and Quality Assurance
Implementation of page-replacement algorithms and Belady’s anomaly
Valgrind Overview What is Valgrind?
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Module 13 Dynamic Memory.
CSE 303 Concepts and Tools for Software Development
Dynamic Data Structures
Presentation transcript:

CSc 352 Freeing Dynamically Allocated Memory Saumya Debray Dept. of Computer Science The University of Arizona, Tucson

Motivation Dynamically allocated memory should be freed when no longer needed – The C runtime system does not automatically reclaim memory – memory leaks increase the program’s memory footprint this can lead to slow performance, program crashes This needs to be done with care: – too-aggressive freeing can lead to program errors – too-passive freeing can miss memory allocated “behind the scenes”, e.g., through strdup(). 2

Issues Understanding when dynamic memory allocation happens – explicit (by the programmer) – implicit (by library routines) Through the course of the program: – identifying when memory is no longer needed – being aware of multiple pointers to a block of memory Identifying and fixing memory leaks 3

Understanding dynamic memory allocation Dynamic allocation can be explicit or implicit – explicit: when the programmer invokes malloc, calloc, etc. – implicit: when the memory allocation happens “behind the scenes” in a library routine getline strdup GNU extension to scanf fopen … In general, if memory appears “by magic” at runtime, chances are it’s being allocated implicitly. 4

Freeing up memory Use free(p) when a memory block is no longer needed – p is a pointer to a block of memory previously allocated through malloc or calloc 5 Pitfalls: –accessing a previously- freed block is an error –freeing a block of memory twice is an error pointer malloc’d memory block watch out for multiple ways to reach a memory block

Freeing up memory General approach: – free up memory “hanging off” a block of memory – grab any pointers you need to traverse the rest of the data structure – then free the block itself 6 1 deallocate memory “hanging off” this node 2 grab this pointer 3 free this block

Finding memory leaks 1 7 use valgrind to determine whether the program has memory leaks

Finding memory leaks 2 8 indicates where the lost memory was allocated

Finding memory leaks 2 9 forgot to fclose() after reading the file!

After fclose-ing the input stream 10 less leakage than before, but still quite a bit need to free the linked list of states

Freeing up a linked list 1 11 naïve code to free the nodes in a linked list

Freeing up a linked list 2 12 st0st1stateList

Freeing up a linked list 3 13 no memory errors less memory being leaked but some leakage persists! reason: structures “hanging off” states not being freed // a DFA state typedef struct vertex { char *name; struct edge *transitions; bool isFinal; struct vertex *next; } state; // a transition between states typedef struct edge { char ch; struct vertex *from, *to; struct edge *next; } edge;

Freeing up a linked list 4 14 Solution: free up the edges “hanging off” each state before freeing hat state

Freeing up a linked list 4 15 a lot better! but some leakage still left!

Freeing up a linked list 4 16 // a DFA state typedef struct vertex { char *name; struct edge *transitions; bool isFinal; struct vertex *next; } state;

Freeing up a linked list 5 17

Summary Should free up dynamically allocated memory when done – Use valgrind to identify memory leak sources! Needs to be done carefully – freeing up “live” memory will result in program errors Pay attention to implicitly-allocated memory, e.g.: – file pointers – strings allocated through strdup() – when freeing up a struct, first free up any unused memory “hanging off” the struct 18