CIS 415 – Operating System (Lab) CIS 415 Lab 5 Valgrind (memcheck) Dave Tian.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

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.
COMP 1402 Winter 2009 Tutorial #8 malloc / calloc / realloc.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Dynamic memory allocation in C (Reek, Ch. 11) 1CS 3090: Safety Critical Programming in C.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
CS2110 Dynamic Allocation Chapter 19 Section 19.4 This file is called Mike-Ch19.ppt.
Growing Arrays in C Language. When to Use Do not use Growing Sorted Array –O(n 2 ) Operation –Avoid when n is large Use Keep track of a variable –Few.
Pointers A pointer is a reference to another variable (memory location) in a program –Used to change variables inside a function (reference parameters)
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.
Dynamic Memory Management CAS CS210 Ying Ye Boston University.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
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.
CP104 Introduction to Programming Structure II Lecture 32 __ 1 Data Type planet_t and Basic Operations Abstract Data Type (ADT) is a data type combined.
Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng
Week 7 - Friday.  What did we talk about last time?  Allocating 2D arrays.
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:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #5 More about.
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’;
Pointers Applications
Class 2 CSI2172
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Lecture 13 Static vs Dynamic Memory Allocation
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
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.
Chapter 14 Memory API Chien-Chung Shen CIS, UD
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
ECE Application Programming
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
Carnegie Mellon 1 Malloc Lab : Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.
CSE 333 – SECTION 2 Memory Management. Questions, Comments, Concerns Do you have any? Exercises going ok? Lectures make sense? Homework 1 – START EARLY!
HP-SEE Valgrind Usage Josip Jakić Scientific Computing Laboratory Institute of Physics Belgrade The HP-SEE initiative.
Dynamic Instrumentation - Valgrind  Developed by Julian Seward at/around Cambridge University,UK  Google-O'Reilly Open Source Award for "Best Toolmaker"
Overview of memory management
Stack and Heap Memory Stack resident variables include:
Day 03 Introduction to C.
Introduction to Programming
2016.
Memory Management in C Notes courtesy of Dr. D. Rothe, modified by Dr. R. Hasker.
Day 03 Introduction to C.
Checking Memory Management
CSCI206 - Computer Organization & Programming
Dynamic Memory Allocation
CS157: Dynamic Memory Dynamic Memory 11/9/201804/25/06.
14th September IIT Kanpur
Dynamic Instrumentation - Valgrind
Pointers and dynamic memory
CSC215 Lecture Memory Management.
CS111 Computer Programming
Memory Allocation CS 217.
EECE.2160 ECE Application Programming
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
CS111 Computer Programming
Chien-Chung Shen CIS/UD
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
7. Pointers, Dynamic Memory
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
C Programming Lecture-8 Pointers and Memory Management
Dynamic Memory – A Review
EECE.2160 ECE Application Programming
Presentation transcript:

CIS 415 – Operating System (Lab) CIS 415 Lab 5 Valgrind (memcheck) Dave Tian

Lab/Office Hour Lab: 8:00 ~ 8:50 AM Wed/Thurs Klamath 026 Office: 9:00 ~ 10:00 AM Wed/Thurs DES 224

Memcheck can Use of uninitialized memory 2. Reading/writing memory after it has been free'd 3. Reading/writing off the end of malloc'd blocks 4. Reading/writing inappropriate areas on the stack 5. Memory leaks -- where pointers to malloc'd blocks are lost forever 6. Mismatched use of malloc/new/new [] vs free/delete/delete [] 7. Overlapping src and dst pointers in memcpy() and related functions 8. Some misuses of the POSIX pthreads API

What we are focusing on... NAME malloc, free, calloc, realloc - Allocate and free dynamic memory SYNOPSIS #include void *malloc(size_t size); void free(void *ptr); void *calloc(size_t nmemb, size_t size); void *realloc(void *ptr, size_t size); Gcc -o myBadProgram -g memX.c Valgrind --tool=memcheck --leak-check=yes --show-reachables=yes./myBadProgram

Mem1.c #include int main() { char *p; // Allocation #1 of 19 bytes p = (char *) malloc(19); // Allocation #2 of 12 bytes p = (char *) malloc(12); free(p); // Allocation #3 of 16 bytes p = (char *) malloc(16); return 0; }

Mem2.c #include void get_mem() { char *ptr; ptr = (char *) malloc (10); /* memory not freed */ } int main(void) { int i; char *ptr1, *ptr2; ptr1 = (char *) malloc (512); ptr2 = (char *) malloc (512); ptr2 = ptr1; /* causes the memory leak of ptr1 */ free(ptr2); free(ptr1); for ( i = 0; i < 512; i++ ) { get_mem(); }

Mem3.c #include int main(void) { char *chptr; char *chptr1; int i = 1; chptr = (char *) malloc(512); chptr1 = (char *) malloc (512); for ( i; i <= 513; i++ ) { chptr[i] = '?'; /* error when i = 513 invalid write */ chptr1[i] = chptr[i]; /* error when i = 513 invalid read and write */ } free(chptr1); free(chptr); }

Mem4.c #include void initialize(int *array, int size) { int i; for (i = 0; i <= size; ++i) array[i] = 0; } int main(void) { int *p = malloc(sizeof(int)); int values[10]; *p = 37; initialize(values, 10); printf("*p = %d\n", *p); free(p); return 0; }

Mem5.c #include int main() { char *p1 = (char *)malloc(10); char *p2 = p1 + 5; printf("p1=%p,p2=%p\n", p1, p2); free(p2); return 0; }