1 / 13

Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’

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

obelia
Download Presentation

Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’

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 • Memory as byte array • Pointers • Arrays relationship to pointers • Operator ‘new’ • Operator ‘delete’ • Copy ctor • Assignment operator • ‘this’ const pointer • Allocating matrices dynamically

  2. Data Addresses in Memory int myInt; // &myInt == 5000 char myChar; // &myChar == 5004

  3. 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

  4. 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;

  5. Accessing Data with Pointers int x = 50, y = 100; int* px = &x; int* py = &y; y += *px; *py = 20; px = py;

  6. 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]

  7. 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;

  8. 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

  9. 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

  10. 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

  11. 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; };

  12. Copy Constructor C objB (objA);

  13. “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;

More Related