Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory."— Presentation transcript:

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

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

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

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

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

6 CS-1030 Dr. Mark L. Hornick 6 The delete() operator Syntax: delete p; // p is a pointer Rules Make sure p is pointing to a valid object Don’t delete the same object more than once The object must have been created with the new() operator 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!!!

7 CS-1030 Dr. Mark L. Hornick 7 delete() and the destructor The destructor is called when you delete an object via the pointer Employee* p = new Employee(“Tom”); delete p; // calls ~Employee() Never dereference the pointer after deleting the object! 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->giveBigRaise(); // crash and burn !!!!

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

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

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

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

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


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

Similar presentations


Ads by Google