130 likes | 284 Views
Pointers and Dynamic Memory. Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating matrices dynamically. Data Addresses in Memory. int myInt; // &myInt == 5000
E N D
Pointers and Dynamic Memory • Memory as byte array • Pointers • Arrays relationship to pointers • Operator ‘new’ • Operator ‘delete’ • Copy ctor • Assignment operator • ‘this’ const pointer • Allocating matrices dynamically
Data Addresses in Memory int myInt; // &myInt == 5000 char myChar; // &myChar == 5004
Type* ptr; int* intPtr = NULL; Fish* fishPtr; Pointer Variables • Declare a pointer • Pointer ptr is variable that can hold an address of an object of type Type
Pointer Manipulations int m = 10; // Make intPtr point to (reference) m int* intPtr = &m; // *intPtr and m are aliases now string* sPtr; // No automatic init. sPtr = new string ("cool"); // Ctor call cout << *sPtr << " has " << sPtr->size () << " chars" ; // Delete what you “new”! delete sPtr;
Accessing Data with Pointers int x = 50, y = 100; int* px = &x; int* py = &y; y += *px; *py = 20; px = py;
arr [0] arr [1] arr [2] arr [3] arr [4] arr [5] arr [6] 6028 6000 6004 6008 6012 6016 6020 6024 arr arr +1 arr +2 arr +5 arr +7 Arrays and Pointers Array name is const pointer to first element, i.e., arr == &arr[0]
Operator ‘new’ • Use ptrs. and ‘new’ when • object lifetime must extend beyond block in which it’s declared; • you need to allocate memory at run time and can’t avoid it. • Time24* p = new Time24 (); • // *p is 00:00 (midnight) • Time24* q = new Time24 (8, 15); • // *q is 8:15 AM • cout << p->getHour (); • cout << (*q).getMinutes (); • p->advance (2, 15); • ... • delete p; delete q;
Operator ‘delete’ • Deallocating a dynamic array • use a slightly different form of delete (operator delete[ ]) • place square brackets [ ] between delete and the pointer variable name • system deallocates all of the memory originally assigned to the dynamic array T* arr = new T[SIZE]; // Allocated space for SIZE objects // arr[0]…arr[SIZE-1] delete[] arr; // Deallocate dynamic array storage
Classes with Pointer Members • Recipe for classes that allocate memory dynamically • Destructor: ~C (); • Copy constructor: C (const C& cObject); • Operator=: C& operator= (const C& cObject); • Default member-wise copy usually not appropriate
Copy Constructor / Overloaded Assignment Operator Time24 t1 (5, 30); // Invokes copy ctor Time24 t2 = t1; // or Time24 t2 (t1); // Default copy ctor will do // (For each field f) // init t2.f with t1.f // Invoke operator= t2 = t1; // Default will do // (For each field f) // t2.f = t1.f
A Dynamic Class (Uses ‘new’) class C { public: C (int x, int y) : m1 (x), m2 (new int (y)) { } ~C () { delete m2; } C (const C& co) // Must be const & : m1 (co.m1), m2 (new int (*(co.m2)) { } C& operator= (const C& co) { // new? if (this != &co) { m1 = co.m1; *m2 = *(co.m2); } return (*this); } private: int m1; int* m2; };
Copy Constructor C objB (objA);
“this” pointer • Every non-static member function has a pointer to the invoking object • thispoints to invoking object • If had to declare: (const) MyClass*const this; • *thisis the invoking object • operator= returns *this, so can do • x = y = z;