Pointers and References Pointers Memory 0 x 00

  • Slides: 28
Download presentation
Pointers and References

Pointers and References

Pointers & Memory 0 x 00 0 x 04 0 x 08 0 x

Pointers & Memory 0 x 00 0 x 04 0 x 08 0 x 0 B 0 x 10 0 x 14 0 x 18 0 x 1 B 0 x 20

Pointers & Memory int x = 5; 0 x 00 0 x 04 5

Pointers & Memory int x = 5; 0 x 00 0 x 04 5 x 0 x 08 0 x 0 B 0 x 10 0 x 14 0 x 18 0 x 1 B 0 x 20

Pointers & Memory int x = 5; int* y = &x 0 x 00

Pointers & Memory int x = 5; int* y = &x 0 x 00 0 x 04 0 x 08 0 x 0 B 0 x 10 5 0 x 04 x y 0 x 14 0 x 18 0 x 1 B 0 x 20

Pointers & Memory int x = 5; int* y = &x int* z =

Pointers & Memory int x = 5; int* y = &x int* z = y; 0 x 00 0 x 04 0 x 08 0 x 0 B 0 x 10 0 x 14 0 x 18 0 x 1 B 0 x 20 5 0 x 04 x y z

Pointers & Memory int x = 5; int* y = &x int* z =

Pointers & Memory int x = 5; int* y = &x int* z = y; *z = 0; 0 x 00 0 x 04 0 x 08 0 x 0 B 0 x 10 0 x 14 0 x 18 0 x 1 B 0 x 20 0 0 x 04 x y z

Allocating memory using new Point *p = new Point(5, 5); • new can be

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.

Deallocating memory using delete // allocate memory Point *p = new Point(5, 5); .

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

Using new with arrays int x = 10; int* nums 1 = new int[10];

Using new with arrays int x = 10; int* nums 1 = new int[10]; int* nums 2 = 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));

Using delete on arrays // allocate memory int* nums 1 = new int[10]; int*

Using delete on arrays // allocate memory int* nums 1 = new int[10]; int* nums 3 = new int[x][4][5]; . . . // free the memory delete[] nums 1; delete[] nums 3; • Have to use delete[].

Destructors • delete calls the object’s destructor. • delete frees space occupied by the

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.

Destructors – an Example class Segment { public: Segment(); virtual ~Segment(); private: Point *m_p

Destructors – an Example class Segment { public: Segment(); virtual ~Segment(); private: Point *m_p 0, *m_p 1; };

Destructors – an Example Segment: : Segment() { m_p 0 = new Point(0, 0);

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

Syntactic Sugar “->” Point *p = new Point(5, 5); // Access a member function:

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

Stack vs. Heap On the Heap / On the Stack / Dynamic allocation Automatic

Stack vs. Heap On the Heap / On the Stack / Dynamic allocation Automatic allocation Point *p = new Point(); Point p; Point *ps = new Point[n]; Point ps[10]; What happens when p goes out of scope?

Dynamic Memory S 1234567

Dynamic Memory S 1234567

Dynamic Memory S 1234567 T 0000000 int* T = new int[2*n];

Dynamic Memory S 1234567 T 0000000 int* T = new int[2*n];

Dynamic Memory S 1234567 T 12345670000000 for (i = 0; i < n; i++)

Dynamic Memory S 1234567 T 12345670000000 for (i = 0; i < n; i++) T[i] = S[i];

Dynamic Memory S 1234567 T 12345670000000 delete[] S;

Dynamic Memory S 1234567 T 12345670000000 delete[] S;

Dynamic Memory S T 12345670000000 S = T;

Dynamic Memory S T 12345670000000 S = T;

Passing by value void Math: : square(int i) { i = i*i; } int

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

Passing by reference void Math: : square(int &i) { i = i*i; } int

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

What is a reference? • An alias – another name for an object. int

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;

Introducing: const void Math: : print. Square(const int &i){ Won’t compile. i = i*i;

Introducing: const void Math: : print. Square(const int &i){ Won’t compile. i = i*i; cout << i << endl; } int main() { int i = 5; Math: : print. Square(i); Math: : print. Cube(i); }

Can also pass pointers to const void Math: : print. Square(const int *pi) {

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

Declaring things const River nile; const River* nile. Pc; River* const nile. Cp; const

Declaring things const River nile; const River* nile. Pc; River* const nile. Cp; const River* const nile. Cpc

Read pointer declarations right to left // A const River nile; // A pointer

Read pointer declarations right to left // A const River nile; // A pointer to a const River* nile. Pc; // A const pointer to a River* const nile. Cp; // A const pointer to a const River* const nile. Cpc

Exercises 1. Create a class called Point with two private data members, a public

Exercises 1. 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 nonzero data, and print them. Verify 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 nonzero 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.