1 / 28

Pointers and References

Pointers and References. Pointers & Memory. 0x00. 0x04. 0x08. 0x0B. 0x10. 0x14. 0x18. 0x1B. 0x20. Pointers & Memory. int x = 5;. 0x00. 0x04. 0x08. 0x0B. 0x10. 0x14. 0x18. 0x1B. 0x20. 5. x. Pointers & Memory. int x = 5; int * y = &x. 0x00. 0x04. 0x08. 0x0B. 0x10.

aneko
Download Presentation

Pointers and References

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 References

  2. Pointers & Memory 0x00 0x04 0x08 0x0B 0x10 0x14 0x18 0x1B 0x20

  3. Pointers & Memory int x = 5; 0x00 0x04 0x08 0x0B 0x10 0x14 0x18 0x1B 0x20 5 x

  4. Pointers & Memory int x = 5; int* y = &x 0x00 0x04 0x08 0x0B 0x10 0x14 0x18 0x1B 0x20 5 0x04 x y

  5. Pointers & Memory int x = 5; int* y = &x int* z = y; 0x00 0x04 0x08 0x0B 0x10 0x14 0x18 0x1B 0x20 5 0x04 0x04 x y z

  6. Pointers & Memory int x = 5; int* y = &x int* z = y; *z = 0; 0x00 0x04 0x08 0x0B 0x10 0x14 0x18 0x1B 0x20 0 0x04 0x04 x y z

  7. Allocating memory using new Point *p = new Point(5, 5); • new can be thought of a function with slightly strange syntax • new allocates space to hold the object. • new calls the object’s constructor. • new returns a pointer to that object.

  8. Deallocating memory using delete // allocate memory Point *p = new Point(5, 5); ... // free the memory deletep; For every call to new, there must be exactly one call to delete.

  9. Using new with arrays int x = 10; int* nums1 = new int[10]; // ok int* nums2 = new int[x]; // ok • Initializes an array of 10 integers on the heap. • C++ equivalent of the following C code int* nums = (int*)malloc(x * sizeof(int));

  10. Using delete on arrays // allocate memory int* nums1 = newint[10]; int* nums3 = newint[x][4][5]; ... // free the memory delete[] nums1; delete[] nums3; • Have to use delete[].

  11. Destructors • delete calls the object’s destructor. • delete frees space occupied by the object. • A destructor cleans up after the object. • Releases resources such as memory.

  12. Destructors – an Example class Segment { public: Segment(); virtual ~Segment(); private: Point *m_p0, *m_p1; };

  13. Destructors – an Example Segment::Segment() { m_p0 = new Point(0, 0); m_p1 = new Point(1, 1); } Segment::~Segment() { if (m_p0) delete m_p0; if (m_p1) delete m_p1; }

  14. Syntactic Sugar “->” Point *p = new Point(5, 5); // Access a member function: (*p).move(10, 10); // Or more simply: p->move(10, 10);

  15. Stack vs. Heap What happens when p goes out of scope?

  16. Dynamic Memory 1 2 3 4 5 6 7 S

  17. Dynamic Memory 1 2 3 4 5 6 7 S 0 0 0 0 0 0 0 0 0 0 0 0 0 0 T int* T = newint[2*n];

  18. Dynamic Memory 1 2 3 4 5 6 7 S 1 2 3 4 5 6 7 0 0 0 0 0 0 0 T for (i = 0; i < n; i++) T[i] = S[i];

  19. Dynamic Memory 1 2 3 4 5 6 7 S 1 2 3 4 5 6 7 0 0 0 0 0 0 0 T delete[] S;

  20. Dynamic Memory S 1 2 3 4 5 6 7 0 0 0 0 0 0 0 T S = T;

  21. Passing by value void Math::square(inti) { i = i*i; } int main() { inti = 5; Math::square(i); cout << i << endl; }

  22. Passing by reference void Math::square(int&i) { i = i*i; } int main() { inti = 5; Math::square(i); cout << i << endl; }

  23. What is a reference? • An alias – another name for an object. int x = 5; int &y = x; // y is a // reference to x y = 10;

  24. Introducing: const void Math::printSquare(const int&i){ i = i*i; cout << i << endl; } int main() { inti = 5; Math::printSquare(i); Math::printCube(i); } Won’t compile.

  25. Can also pass pointers to const void Math::printSquare(const int*pi) { *pi = (*pi) * (*pi); cout << pi << endl; } int main() { inti = 5; Math::printSquare(&i); Math::printCube(&i); } Still won’t compile.

  26. Declaring things const const River nile; const River* nilePc; River* constnileCp; const River* constnileCpc

  27. Read pointer declarations right to left // A const River const River nile; // A pointer to a const River const River* nilePc; // A const pointer to a River River* constnileCp; // A const pointer to a const River const River* constnileCpc

  28. Exercises • Create a class called Point with two private data members, a public print function, and constructors as well as a destructor that prints a message when it is called. • Create an array of 10 Points on the stack, initialize them with non-zero data, and print them. Verify that that the destructor is called for all 10 points when your program exits. • Create an array of 10 Points on the heap, initialize them with non-zero data, and print them. Do not delete the array and verify the destructor is not called (you have created a memory leak). Now write your program to delete the array at the end. Verify that only one destructor is called and your program crashes (you have created a memory leak and a bug). Now write your program to delete[] the array at the end. Verify that all destructors are called.

More Related