Presentation is loading. Please wait.

Presentation is loading. Please wait.

Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.

Similar presentations


Presentation on theme: "Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data."— Presentation transcript:

1 Revision on C++ Pointers TCP1201: 2013/2014

2 Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data. 2. Dynamic memory allocation (create the amount of data based on runtime need). 3. Dynamic polymorphism (Lecture 4)  A pointer always stores the address of a data.  Before we can use a pointer, it must point to a valid address that is achieved through:  Pointing it to an existing data, or  Using it to create a new data (use new operator).

3 3 int a = 3; int *p = &a; cout << *p; // 3 cout << p; // 1432 The ampersand '&' is called address operator which returns the address of variable 'a'. 'a' stores an int. 'p' stores the address of variable 'a'. Point to Data Without Cloning the Data 1432 4324 p [ int* ] 3 a [ int ] 1432 1FB5 We say 'p' points to 'a'. 'p' is not a clone of 'a'. p [ int * ]

4 Pointers Basics int a = 3; 3 1100 [int] 1100 1FA0 p [ int* ] a = 10; cout << a << endl; cout << *p << endl << endl; *p = 99; cout << a << endl; cout << *p << endl << endl; 1099 a int *p; p = &a; cout << a << endl; cout << *p << endl << endl;

5 Pointers Basics int a[5] = {3, 6, 9, 12, 15}; int *p = a; cout << a[0] << endl; cout << *p << endl << endl; for (int i=0; i<5; i++) cout << a[i] << " "; cout << endl; for (int i=0; i<5; i++) cout << p[i] << " "; cout << endl << endl; 3 6 9 1100 1104 1108 1112 12 [int] 1100 1FA0 p [ int* ] 1100 1FB4 a 1116 15 [int]

6 Pointers Basics cout << (*p)++ << endl; cout << *p << endl << endl; for (int i=0; i<5; i++) cout << a[i] << " "; cout << endl; for (int i=0; i<5; i++) cout << p[i] << " "; cout << endl << endl; 3 6 9 1100 1104 1108 1112 1100 1FA0 p [ int* ] 12 [int] 1100 1FB4 a 1116 15 [int]

7 Pointers Basics cout << (*p)++ << endl; cout << *p << endl << endl; for (int i=0; i<5; i++) cout << a[i] << " "; cout << endl; for (int i=0; i<5; i++) cout << p[i] << " "; cout << endl << endl; 4 6 9 1100 1104 1108 1112 1100 1FA0 p [ int* ] 12 [int] 1100 1FB4 a 1116 15 [int]

8 Pointers Basics cout << *p++ << endl; cout << *p << endl << endl; for (int i=0; i<5; i++) cout << a[i] << " "; cout << endl; for (int i=0; i<5; i++) cout << p[i] << " "; cout << endl << endl; 4 6 9 1100 1104 1108 1112 1100 1FA0 p [ int* ] 12 [int] 1100 1FB4 a 1116 15 [int]

9 Pointers Basics cout << *p++ << endl; cout << *p << endl << endl; for (int i=0; i<5; i++) cout << a[i] << " "; cout << endl; for (int i=0; i<5; i++) cout << p[i] << " "; cout << endl << endl; 4 6 9 1100 1104 1108 1112 1104 1FA0 p [ int* ] 12 [int] 1100 1FB4 a 1116 15 [int]

10 Is pointer that does not point to an valid address. Use of dangling pointer generates runtime error easily. 10 int* p; // 'p' usually does not point to // a valid address. *p = 10; // Runtime error usually. p [ int* ] ? Dangling Pointers

11 Dynamic Memory Allocation (DMA)  To create new data dynamically.  We use new operator to create new data, and later use delete operator to release the memory used by the data.  The new operator allocates the memory needed for the new data, and returns the address of the new data. int* p = new int; *p = 10;... delete p; 1100 1FA0 p [ int* ] [int] 10

12 Dynamic Memory Allocation (DMA) int* p; p = new int[4]; 1100 1104 1108 1112 1100 1FA0 p [ int* ] [int]

13 Dynamic Memory Allocation (DMA) int* p; p = new int[4]; p[0] = 12; p[1] = 24; p[2] = 15; p[3] = 35; 12 22 15 1100 1104 1108 1112 1100 1FA0 p [ int* ] 35 [int]

14 Dynamic Memory Allocation (DMA) int* p; p = new int[4]; p[0] = 13; p[1] = 42; p[2] = 15; p[3] = 32; 1100 1104 1108 1112 1100 1FA0 p [ int* ] [int]... delete[] p; p = NULL;

15 Review question int main() { int a[] = {11, 22, 33, 44, 55}; int x; int *p = a; x = *p++; cout<< "*p = " << *p <<"\n\n"; cout<< "x = " << x <<"\n\n\n"; int b[] = {11, 22, 33, 44, 55}; int y; int *q = b; y = (*q)++; cout<< "*q = " << *q <<"\n\n"; cout<< "y = " << y <<"\n\n\n"; int c[] = {11, 22, 33, 44, 55}; int z; int *r = c; z = *++r; cout<< "*r = " << *r <<"\n\n"; cout<< "z = " << z <<"\n\n\n"; return 0; }


Download ppt "Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data."

Similar presentations


Ads by Google