Pointers And Dynamic Memory CS1030 Dr Mark L

  • Slides: 12
Download presentation
Pointers And Dynamic Memory CS-1030 Dr. Mark L. Hornick 1

Pointers And Dynamic Memory CS-1030 Dr. Mark L. Hornick 1

Dynamic Memory l C++ has a new() operator like Java l new() returns an

Dynamic Memory l C++ has a new() operator like Java l new() returns an address of a newly-created object You have to assign the address to a pointer Employee* pe = new Employee; l CS-1030 Dr. Mark L. Hornick 2

Object access via pointers int id; Employee an. Emp; // as an object id

Object access via pointers int id; Employee an. Emp; // as an object id = an. Emp. get. ID(); // object access Employee* pe = new Employee; // ptr id = pe->get. ID(); // ptr access //id = (*pe). get. ID() // never done CS-1030 Dr. Mark L. Hornick 3

Dynamic Memory C++ does not have a Garbage Collector l Who cleans up after

Dynamic Memory C++ does not have a Garbage Collector l Who cleans up after you? CS-1030 Dr. Mark L. Hornick 4

Dynamic Memory C++ does not have a Garbage Collector l Who cleans up after

Dynamic Memory C++ does not have a Garbage Collector l Who cleans up after you? l You do! CS-1030 Dr. Mark L. Hornick 5

The delete() operator l l Syntax: delete p; // p is a pointer Rules

The delete() operator l l Syntax: delete p; // p is a pointer Rules l Make sure p is pointing to a valid object l Don’t delete the same object more than once l The object must have been created with the new() operator l l i. e. the object must be on the heap, not the stack Never delete an object created on the stack: Employee e(“Tom”); // e created in stack memory Employee* pe = &e // points at object on stack delete pe; // crash and burn!!! CS-1030 Dr. Mark L. Hornick 6

delete() and the destructor l The destructor is called when you delete an object

delete() and the destructor l The destructor is called when you delete an object via the pointer Employee* p = new Employee(“Tom”); delete p; // calls ~Employee() l Never dereference the pointer after deleting the object! l The pointer still contains the object’s previous address, but the object is no longer valid, even if it’s “hanging around” in memory. Employee* p = new Employee(“Tom”); delete p; // object is invalidated p->give. Big. Raise(); // crash and burn !!!! CS-1030 Dr. Mark L. Hornick 7

Dynamic Memory Operators l new l l Allocates memory from the heap Invokes the

Dynamic Memory Operators l new l l Allocates memory from the heap Invokes the constructor (by context) Returns a pointer to the new object delete l l Invokes the destructor Releases the memory back to the heap CS-1030 Dr. Mark L. Hornick 8

Object Types l static l l l Memory is fixed at compile time Stored

Object Types l static l l l Memory is fixed at compile time Stored in a special area Automatic Dog some. Dog; l l Memory allocated as the program runs Stored on the stack (CS 280, CS 285) l l Automatically destroyed when it goes out of scope Dynamic Dog* p. Dog; p. Dog = new Dog; l l Drawn from a memory pool (heap) Can be released and the memory reused CS-1030 Dr. Mark L. Hornick 9

Memory Leaks l l Common and dangerous problem Sequence of events l l Dynamic

Memory Leaks l l Common and dangerous problem Sequence of events l l Dynamic memory is allocated Pointer is lost l l By reassignment p. Dog = 0; End of lifetime § l p. Dog goes out of scope Dynamic object is inaccessible l Cannot be deleted! Why is this bad? CS-1030 Dr. Mark L. Hornick 10

NULL Pointer Revisited l Purpose l l This pointer points to no object Value

NULL Pointer Revisited l Purpose l l This pointer points to no object Value is 0 Defined in <cstdlib> Common convention l l l NULL is NEVER dereferenced if new fails, it returns NULL delete “ignores” NULL CS-1030 Dr. Mark L. Hornick 11

Usage of NULL #include <cstdlib> … Dog* p. Dog = NULL; p. Dog =

Usage of NULL #include <cstdlib> … Dog* p. Dog = NULL; p. Dog = new Dog; … delete p. Dog; p. Dog = NULL; // For safety CS-1030 Dr. Mark L. Hornick 12