1 / 12

Pointers

Pointers. And Dynamic Memory. 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;. Object access via pointers. int id; Employee anEmp; // as an object

nenet
Download Presentation

Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pointers And Dynamic Memory 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; 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 CS-1030 Dr. Mark L. Hornick

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

  5. Dynamic Memory C++ does not have a Garbage Collector • Who cleans up after you? • You do! 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 memoryEmployee* pe = &e // points at object on stackdelete pe; // crash and burn!!! CS-1030 Dr. Mark L. Hornick

  7. delete() and the destructor • The destructor is called when you delete an object via the pointerEmployee* 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 invalidatedp->giveBigRaise(); // crash and burn !!!! 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 CS-1030 Dr. Mark L. Hornick

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

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

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

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

More Related