120 likes | 235 Views
Dive into the intricacies of dynamic memory management in C++ through pointers. Learn how to allocate memory with the `new` operator and deallocate it using `delete`. Understand the importance of managing object lifetimes, potential memory leaks, and the significance of the destructor. Discover how to prevent crashes by correctly handling pointers after deletion and maintaining code safety with NULL pointers. Gain essential insights that are pivotal for effective memory management in C++, crucial for both beginners and seasoned developers.
E N D
Pointers And Dynamic Memory CS-1030 Dr. Mark L. Hornick
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
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
Dynamic Memory C++ does not have a Garbage Collector • Who cleans up after you? CS-1030 Dr. Mark L. Hornick
Dynamic Memory C++ does not have a Garbage Collector • Who cleans up after you? • You do! CS-1030 Dr. Mark L. Hornick
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
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
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
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
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
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
Usage of NULL #include <cstdlib> … Dog* pDog = NULL; pDog = new Dog; … delete pDog; pDog = NULL; // For safety CS-1030 Dr. Mark L. Hornick