Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.

Similar presentations


Presentation on theme: "Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny."— Presentation transcript:

1 Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny

2 Pointers Pointers are variables storing the memory address of another data/variable/object. 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20

3 Pointers Pointers are variables storing the memory address of another data/variable/object. int x=5; 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 5 x

4 5 x y Pointers and Memory 0x04
Pointers are variables storing the memory address of another data/variable/object. int x=5; int *y = &x; Operator & gives the memory address of a variable/object 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 5 0x04 x y

5 5 x y z Pointers and Memory 0x04 0x04
Pointers are variables storing the memory address of another data/variable/object. int x=5; int *y = &x; Operator & gives the memory address of a variable/object int* z = y; 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 5 0x04 0x04 x y z

6 5 x y z Pointers and Memory 0x04 0x04 int *y = &x; int* z = y;
What will happen if I write: *z = 0; ? 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 5 0x04 0x04 x y z

7 x y z Pointers and Memory 0x04 0x04 int *y = &x; int* z = y;
What will happen if I write: *z = 0; ? * is a dereferencing operator – gives the content of the memory address pointed by (or stored in) the pointer 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C 0x20 0x04 0x04 x y z

8 Memory allocation – new operator
new allocates space to hold the object. new calls the object’s constructor. new returns a pointer to that object. Point * A = new Point (10, 20);

9 Memory Deallocation – Delete Operator
For every call to new, there must be exactly one call to delete. Point * A = new Point (10, 20); //allocates memory delete A; //deallocates or frees the memory

10 Pointers And Array An array can act as a pointer
Array name is a pointer to first element in the array Pointer can be indexed like an array int arr[5] = {1,2,3,4,5}; cout << arr[2] << "," << *(arr+2); //displays 3,3

11 Dynamic Allocation & Deallocation of Array
Static allocation: You must know the size of the array before hand int arr[10]; Dynamic Allocation Size of the array can be passed as a variable size_t sz = 10; int* arr = new int[sz]; Deallocate : delete[] arr;

12 Memory HEAP MEMORY STACK

13 Stack vs. Heap Heap – Dynamic Allocation Point *p = new Point(5,10);
double *amount = new double[5]; Stack – static allocation Point p(5,10); double amount[5]; What happens when p goes out of scope?

14 Pair programming Driver – One at the keyboard
Navigator – Helps direct driver Teamwork and communication are key here Switch roles frequently!


Download ppt "Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny."

Similar presentations


Ads by Google