Memory Leaks and Dangling Pointers Lecture 5 Secs 2.4, 3.4 Mon, Jan 28, 2008.

Slides:



Advertisements
Similar presentations
1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs.
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Data Structures Using C++ 2E
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Chapter 6 Data Types
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Introduction to Java Objects CSIS 3701: Advanced Object Oriented Programming.
Dynamically Allocated Memory String CGS 3460, Lecture 33 Apr 3, 2006 Hen-I Yang.
Classes and Objects: Chapter 8, Slide 1 Object and its encapsulation.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 Pointers (Walls & Mirrors - Beginning of Chapter 4)
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.
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’;
Dynamic Objects. COMP104 Lecture 31 / Slide 2 Static verses Dynamic Objects * Static object n Memory is acquired automatically  int A[10]; n Memory is.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
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.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Review of C++ Programming Part II Sheng-Fang Huang.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Lecture 11 vector and Free Store Bjarne Stroustrup
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers.
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Pointers & Dynamic Arrays Shinta P.
Memory Management Issues, Solutions, and Examples.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Dynamic Allocation Joe Meehean. Dynamic Allocation Memory for local objects is automatically created and reclaimed memory is created for it at beginning.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
UFS003C3 Lecture 15 Data type in C & C++ Using the STL.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
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 2 2 Call The Project Dynamic-Memory 4 4 # include "Utilities.hpp" int main(int argc, char * argv[]) { short int *PtrNo; (*PtrNo) = 5; printf ("(*PtrNo)
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
Data Structures in C++ Pointers & Dynamic Arrays Shinta P.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
CSE 220 – C Programming malloc, calloc, realloc.
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
group work #hifiTeam
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Indirection.
Memory Leaks and Dangling Pointers
Review Chapter 10 PPT for full coverage.
Dynamic Memory.
The Constructors Lecture 7 Fri, Feb 2, 2007.
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
SPL – PS2 C++ Memory Handling.
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

Memory Leaks and Dangling Pointers Lecture 5 Secs 2.4, 3.4 Mon, Jan 28, 2008

Memory Leaks A memory leak occurs when all pointers to a block of allocated memory have been lost. Leaked memory cannot be accessed or reallocated. Excessive memory leaks may cause the program to run out of usable memory and crash. Memory leaks should always be avoided.

Dangling Pointers A dangling pointer is a non-null pointer that points to unallocated memory. Dereferencing a dangling pointer may cause the program to crash.

Avoiding Dangling Pointers It impossible to test a non-null pointer to see if it is dangling. Always set pointers to NULL if they do not point to allocated memory. Then compare them to NULL to see whether they point to allocated memory.

Example: Vectr::setSize() The setSize() function of the Vectr class must Allocate new memory of the specified size. Copy the old values into the new memory. Deallocate the memory that the Vectr is currently using. Redirect the Vectr to the new memory.

Example: Vectr::input() The input() function of the Vectr class must Deallocate the memory that the Vectr is currently using (if any). Allocate new memory for the values to be input. Continue to increase the allocated memory as more values are read.

Example: Vectr::input() vectr.h vectr.cpp VectrTest.cpp

Constructors and the new Operator The new operator is used in conjunction with the constructors. int* pi = new int(123); int* pai = new int[123]; string* ps = new string("Hello"); Rational* pr = new Rational(2, 3); Date* pd = new Date("Jan", 23, 2002); Point2D* ppt = new Point; Point2D* papt = new Point2D[10];

Constructors and the new Operator When using the new operator to create an array of objects, each object in the array is initialized through the default constructor.