pointers and references
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/1.jpg)
Pointers and References
![Page 2: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/2.jpg)
Pointers & Memory
0x00 0x04 0x08 0x0B 0x10 0x14 0x18
0x1B
0x20
![Page 3: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/3.jpg)
5
int x = 5;
x
0x00 0x04 0x08 0x0B 0x10 0x14 0x18
0x1B
0x20
Pointers & Memory
![Page 4: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/4.jpg)
Pointers & Memory
5
int x = 5;int* y = &x
x y0x04
0x00 0x04 0x08 0x0B 0x10 0x14 0x18
0x1B
0x20
![Page 5: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/5.jpg)
Pointers & Memory
5
int x = 5;int* y = &xint* z = y;
x y0x04 0x04
z
0x00 0x04 0x08 0x0B 0x10 0x14 0x18
0x1B
0x20
![Page 6: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/6.jpg)
Pointers & Memory
0
int x = 5;int* y = &xint* z = y;*z = 0;
x y0x04 0x04
z
0x00 0x04 0x08 0x0B 0x10 0x14 0x18
0x1B
0x20
![Page 7: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/7.jpg)
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.
![Page 8: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/8.jpg)
Deallocating memory using delete
// allocate memoryPoint *p = new Point(5, 5);
...// free the memorydelete p;
For every call to new, there must beexactly one call to delete.
![Page 9: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/9.jpg)
Using new with arrays
int x = 10;int* nums1 = new int[10]; // okint* nums2 = new int[x]; // ok
• Initializes an array of 10 integers on the heap.• C++ equivalent of the following C codeint* nums = (int*)malloc(x * sizeof(int));
![Page 10: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/10.jpg)
Using delete on arrays// allocate memoryint* nums1 = new int[10];int* nums3 = new int[x][4][5];
...// free the memorydelete[] nums1;delete[] nums3;
• Have to use delete[].
![Page 11: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/11.jpg)
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.
![Page 12: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/12.jpg)
Destructors – an Example
class Segment{public: Segment(); virtual ~Segment();private: Point *m_p0, *m_p1;};
![Page 13: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/13.jpg)
Destructors – an ExampleSegment::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;}
![Page 14: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/14.jpg)
Syntactic Sugar “->”
Point *p = new Point(5, 5);
// Access a member function:(*p).move(10, 10);
// Or more simply:p->move(10, 10);
![Page 15: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/15.jpg)
Stack vs. HeapOn the Heap / Dynamic allocation
On the Stack / Automatic allocation
Point *p = new Point();
Point *ps = new Point[n];
Point p;
Point ps[10];
What happens when p goes out of scope?
![Page 16: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/16.jpg)
Dynamic Memory
S 1 2 3 4 5 6 7
![Page 17: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/17.jpg)
Dynamic Memory
S
T
1 2 3 4 5 6 7
0 0 0 0 0 0 0 0 0 0 0 0 0 0
int* T = new int[2*n];
![Page 18: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/18.jpg)
Dynamic Memory
S
T
1 2 3 4 5 6 7
1 2 3 4 5 6 7 0 0 0 0 0 0 0
for (i = 0; i < n; i++) T[i] = S[i];
![Page 19: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/19.jpg)
Dynamic Memory
S
T
1 2 3 4 5 6 7
1 2 3 4 5 6 7 0 0 0 0 0 0 0
delete[] S;
![Page 20: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/20.jpg)
Dynamic Memory
S
T 1 2 3 4 5 6 7 0 0 0 0 0 0 0
S = T;
![Page 21: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/21.jpg)
Passing by value
void Math::square(int i) { i = i*i;}
int main() { int i = 5; Math::square(i); cout << i << endl;}
![Page 22: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/22.jpg)
Passing by reference
void Math::square(int &i) { i = i*i;}
int main() { int i = 5; Math::square(i); cout << i << endl;}
![Page 23: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/23.jpg)
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;
![Page 24: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/24.jpg)
Introducing: const
void Math::printSquare(const int &i){ i = i*i; cout << i << endl;}
int main() { int i = 5; Math::printSquare(i); Math::printCube(i);}
Won’t compile.
![Page 25: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/25.jpg)
Can also pass pointers to const
void Math::printSquare(const int *pi) {
*pi = (*pi) * (*pi); cout << pi << endl;}
int main() { int i = 5; Math::printSquare(&i); Math::printCube(&i);}
Still won’t compile.
![Page 26: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/26.jpg)
Declaring things const
const River nile;
const River* nilePc;
River* const nileCp;
const River* const nileCpc
![Page 27: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/27.jpg)
Read pointer declarations right to left
// A const Riverconst River nile;
// A pointer to a const Riverconst River* nilePc;
// A const pointer to a RiverRiver* const nileCp;
// A const pointer to a const Riverconst River* const nileCpc
![Page 28: Pointers and References](https://reader035.vdocument.in/reader035/viewer/2022062310/56816387550346895dd474a9/html5/thumbnails/28.jpg)
Exercises1. 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.
2. 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.
3. 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.