Pointers

Please download to get full document.

View again

of 12
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information Report
Category:

Documents

Published:

Views: 44 | Pages: 12

Extension: PDF | Download: 0

Share
Description
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
Transcript
PointersAnd Dynamic MemoryCS-1030Dr. Mark L. HornickDynamic 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-1030Dr. Mark L. HornickObject access via pointersint id;Employee anEmp; // as an objectid = anEmp.getID(); // object accessEmployee* pe = new Employee; // ptrid = pe->getID(); // ptr access//id = (*pe).getID() // never doneCS-1030Dr. Mark L. HornickDynamic Memory C++ does not have a Garbage Collector
  • Who cleans up after you?
  • CS-1030Dr. Mark L. HornickDynamic Memory C++ does not have a Garbage Collector
  • Who cleans up after you?
  • You do!
  • CS-1030Dr. Mark L. HornickThe 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-1030Dr. Mark L. Hornickdelete() 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-1030Dr. Mark L. HornickDynamic 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-1030Dr. Mark L. HornickObject 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-1030Dr. Mark L. HornickMemory 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-1030Dr. Mark L. HornickNULL 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-1030Dr. Mark L. HornickUsage of NULL #include <cstdlib>… Dog* pDog = NULL;pDog = new Dog;… delete pDog;pDog = NULL; // For safetyCS-1030Dr. Mark L. Hornick
    Related Search
    We Need Your Support
    Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

    Thanks to everyone for your continued support.

    No, Thanks
    SAVE OUR EARTH

    We need your sign to support Project to invent "SMART AND CONTROLLABLE REFLECTIVE BALLOONS" to cover the Sun and Save Our Earth.

    More details...

    Sign Now!

    We are very appreciated for your Prompt Action!

    x