Checking Memory Management

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.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
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:
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
Informática II Prof. Dr. Gustavo Patiño MJ
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
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.
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.
C and Data Structures Baojian Hua
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
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
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
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. This is.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
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,
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Hank Childs, University of Oregon April 15th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CSE 333 – SECTION 2 Memory Management. Questions, Comments, Concerns Do you have any? Exercises going ok? Lectures make sense? Homework 1 – START EARLY!
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Yan Shi CS/SE2630 Lecture Notes
Memory-Related Perils and Pitfalls in C
Dynamic Allocation in C
Object Lifetime and Pointers
Dynamic Storage Allocation
Memory allocation & parameter passing
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
Stack and Heap Memory Stack resident variables include:
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Debugging Memory Issues
CSE 374 Programming Concepts & Tools
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Lesson One – Creating a thread
Programmazione I a.a. 2017/2018.
Pointers Revisited What is variable address, name, value?
Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Hank Childs, University of Oregon
Dynamic Memory Allocation
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Lecture 4: Process Memory Layout
Memory Management III: Perils and pitfalls Mar 13, 2001
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Dynamic Memory Allocation
Memory Allocation CS 217.
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Dynamic Memory A whole heap of fun….
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Pointers and Arrays Beyond Chapter 16
CETS: Compiler-Enforced Temporal Safety for C
Dynamic Memory A whole heap of fun….
Data Structures and Algorithms Memory allocation and Dynamic Array
Dynamic Memory CSCE 121.
Dynamic Memory – A Review
Makefiles, GDB, Valgrind
Pointers, Dynamic Data, and Reference Types
Run-time environments
Chapter 16 Pointers and Arrays
Presentation transcript:

Checking Memory Management Valgrind Checking Memory Management

Outline Pointers Memory Model in C Multidimensional Arrays Valgrind

Pointers Basic Concepts and Operations

Recap: Concepts A pointer is a type of variable that contains a memory address With that memory address you can view/change the data stored there The type of a pointer is the type of data that is stored at that address void *ptr is an unspecified type Pointers can be cast to a different type just like other variables int *myIntPtr = (int *) floatPtr;

Recap: Operation There are two main operations that deal with pointers: Dereference (*ptr): get the value stored at the memory location of the pointer Address of (&var): get the address of any variable int *myIntPtr = &myInt; You can have a pointer to a pointer char *argv[]; // argv is now a pointer to list of pointers **argv;

Pointer Arithmetic Directly manipulate a pointer’s content to access other locations Memory location is changed by the size of the pointer type You can also set a value to an offset of a pointer with something like this: *(ptr + 4) = 28;

Memory Model in C Stack, Heap, Malloc, and Free

The Stack The stack is a memory structure managed by the compiler Each function call made creates a stack frame that is put on the top of the stack The stack frame contains the collection of data associated with that call (like the return address and the argument variables) The stack frame is destroyed when returning from a function

Local Variables Local variables are stored on the stack Returning from a function deallocates the memory for those variables Returning a pointer to a local variable is almost always a bug

What can go wrong? Run out of stack stack space Unintentionally change values on the stack Values in a frame belonging to another function Accidentally change the return address from a function Access memory even after the frame is deallocated

The Heap You can use space in a part of memory separate from the stack known as the heap Variables or regions that are stored in the heap are not deallocated when a function returns C never puts variables on the heap automatically, you must request storage space on the heap

Malloc – Memory allocate malloc is a library function in stdlib.h Requests a memory region of a specified size void *malloc(int size) Remember that void * is a generic pointer You must explicitly free memory after using it

What can go wrong? Run out of heap space (malloc returns a null pointer) Unintentionally change other heap data just like with the stack Access memory after you have called free on it Free the same memory twice

Valgrind Basic usage and common errors

Basic Usage Valgrind is a UNIX program that allows you to detect errors in your code that occur from using malloc and free improperly. These errors can include things like: uninitialized memory, invalid read/write, invalid free, memory leak, etc. Compilation is done by adding debugging info just like with gdb $ gcc –g myProgram.c

Uninitialized Memory

INVALID READ OR WRITE

Invalid Free

Memory Leak