CSE 333 – SECTION 2 Memory Management. Questions, Comments, Concerns Do you have any? Exercises going ok? Lectures make sense? Homework 1 – START EARLY!

Slides:



Advertisements
Similar presentations
Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
Advertisements

CSCI 171 Presentation 11 Pointers. Pointer Basics.
CIS 415 – Operating System (Lab) CIS 415 Lab 5 Valgrind (memcheck) Dave Tian.
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 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’;
CSSE 332 Functions, Pointers in C. 2 Functions - why and how ? If a problem is large If a problem is large Modularization – easier to: Modularization.
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
C and Data Structures Baojian Hua
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 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
Memory Layout C and Data Structures Baojian Hua
Pointers Applications
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
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
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays 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.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
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.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
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.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
CSCI Rational Purify 1 Rational Purify Overview Michel Izygon - Jim Helm.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
NOVA art. memory leaking Alexey Naumov Lebedev Physical Institute Moscow 1.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
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.
1 C Basics Monday, August 30, 2010 CS 241. Announcements MP1, a short machine problem, will be released today. Due: Tuesday, Sept. 7 th at 11:59pm via.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
Arrays and Pointers (part 2) CSE 2031 Fall March 2016.
CSE 333 – SECTION 1 C, Introduction to and Working with.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
1 Homework Continue with K&R Chapter 5 –Skipping sections for now –Not covering section 5.12 Continue on HW5.
HP-SEE Valgrind Usage Josip Jakić Scientific Computing Laboratory Institute of Physics Belgrade The HP-SEE initiative.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Memory Leaks and Valgrind
Content Coverity Static Analysis Use cases of Coverity Examples
Object Lifetime and Pointers
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
Stack and Heap Memory Stack resident variables include:
CSE 374 Programming Concepts & Tools
Introduction to Programming
Valgrind Overview What is Valgrind?
Structure of C Programs
Advanced course of C/C++
Day 03 Introduction to C.
Checking Memory Management
Programmazione I a.a. 2017/2018.
CSE 333 – SECTION 2 Memory Management
Terminos españoles de ingeneria de computacion.
CSE 333 – SECTION 2 Memory Management
CSE 333 – SECTION 2 Memory Management
Dynamic Memory Allocation
Memory Allocation CS 217.
Dynamic Memory A whole heap of fun….
Introduction to Static Analyzer
Manual Memory Management: Mastering malloc()
Dynamic Memory A whole heap of fun….
Arrays and Pointers (part 2)
Arrays and Pointers (part 2)
Valgrind Overview What is Valgrind?
Dynamic Memory – A Review
Makefiles, GDB, Valgrind
Presentation transcript:

CSE 333 – SECTION 2 Memory Management

Questions, Comments, Concerns Do you have any? Exercises going ok? Lectures make sense? Homework 1 – START EARLY! Common Exercise Errors Comments Program Comments – Author, copyright, problem description at the top Function Comments – Near the prototype/declaration in header files; local functions are a more complex story, but near the prototype works for those too. clint or cpplint errors Valgrind errors

Memory Management Heap -Large pool of unused memory -malloc() allocates chunks of this memory -free() deallocates memory and reclaims space Stack and stack frame Stores temporary/local variables Each function has its own stack frame Lifetime on heap vs. Lifetime on stack

Lecture 5 Wrap Up Cpp review Compiling C file with gcc Linking objects into an executable with gcc Memory diagram

Drawing memory diagrams (Lecture Example with ll.h, ll.c and example_ll_customer.c)

Memory Errors Use of uninitialized memory Reading/writing memory after it has been freed – Dangling pointers Reading/writing to the end of malloc'd blocks Reading/writing to inappropriate areas on the stack Memory leaks where pointers to malloc'd blocks are lost Mismatched use of malloc/new/new[] vs free/delete/delete[] Valgrind is your friend!!

Demo: buggy code buggy.c demo + code fix

Some buggy code 1. #include 2. #include 3. //Returns an array containing [n, n+1,..., m-1, m]. If n>m, then the 4. //array returned is []. If an error occurs, NULL is returned. 5. int *RangeArray(int n, int m) { 6. int length = m-n+1; //Heap allocate the array needed to return int *array = (int*) malloc(sizeof(int)*length); //Initialize the elements 13. for(int i=0;i<=length; i++) 14. array[i] = i+n; 15. return array; 16. } 17. //Accepts two integers as arguments 18. int main(int argc, char *argv[]) { 19. if(argc != 3) return EXIT_FAILURE; 20. int n = atoi(argv[1]), m = atoi(argv[2]); //Parse cmd-line args 21. int *nums = RangeArray(n,m); 22. //Print the resulting array 23. for(int i=0; i<= (m-n+1); i++) 24. printf(“%d”, nums[i]); 25. puts(“”); 26. return EXIT_SUCCESS; 27. }

Valgrind output ==22891== Command:./warmup 1 10 ==22891== ==22891== Invalid write of size 4 ==22891== at 0x400616: RangeArray (warmup.c:14) ==22891== by 0x400683: main (warmup.c:22) ==22891== Address 0x51d2068 is 0 bytes after a block of size 40 alloc'd ==22891== at 0x4C2A93D: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==22891== by 0x4005EC: RangeArray (warmup.c:10) ==22891== by 0x400683: main (warmup.c:22) ==22891== ==22891== Invalid read of size 4 ==22891== at 0x4006A5: main (warmup.c:26) ==22891== Address 0x51d2068 is 0 bytes after a block of size 40 alloc'd ==22891== at 0x4C2A93D: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==22891== by 0x4005EC: RangeArray (warmup.c:10) ==22891== by 0x400683: main (warmup.c:22) ==22891== ==22891== ==22891== HEAP SUMMARY: ==22891== in use at exit: 40 bytes in 1 blocks ==22891== total heap usage: 1 allocs, 0 frees, 40 bytes allocated ==22891== ==22891== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==22891== at 0x4C2A93D: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==22891== by 0x4005EC: RangeArray (warmup.c:10) ==22891== by 0x400683: main (warmup.c:22) ==22891== ==22891== LEAK SUMMARY: ==22891== definitely lost: 40 bytes in 1 blocks ==22891== indirectly lost: 0 bytes in 0 blocks ==22891== possibly lost: 0 bytes in 0 blocks ==22891== still reachable: 0 bytes in 0 blocks ==22891== suppressed: 0 bytes in 0 blocks ==22891== ==22891== For counts of detected and suppressed errors, rerun with: -v ==22891== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 3 from 3)

Compiling separate source files (Make preview) Header files (*.h) Source files (*.c) Makefile

Section exercise Handouts. Work with a partner, if you wish. Look at the expandable vector code in imsobuggy.c. First, try to find all the bugs by inspection. Then try to use Valgrind on the same code. Code is located at