150 likes | 251 Views
Revision on C++ Pointers. TCP1201: 2013/2014. Pointer Basics. Why pointer is important? Reference/Point to existing data without cloning the data . Dynamic memory allocation (create the amount of data based on runtime need). Dynamic polymorphism (Lecture 4)
E N D
Revision on C++ Pointers TCP1201: 2013/2014
Pointer Basics • Why pointer is important? • Reference/Point to existing data without cloning the data. • Dynamic memory allocation (create the amount of data based on runtime need). • 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).
Point to Data Without Cloning the Data 1432 1432 3 a [ int ] int a = 3; int *p = &a; cout << *p; // 3 cout << p; // 1432 4324 1FB5 1432 p [ int* ] p [ int * ] 'a' stores an int . 'p' stores the address of variable 'a'. The ampersand '&' is called address operator which returns the address of variable 'a'. We say 'p' points to 'a'. 'p' is not a clone of 'a'.
Pointers Basics int a = 3; 1FA0 1100 10 99 3 int *p; p = &a; cout << a << endl; cout << *p << endl << endl; 1100 [int] p [ int* ] a a = 10; cout << a << endl; cout << *p << endl << endl; *p = 99; cout << a << endl; cout << *p << endl << endl;
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; 1FA0 1FB4 1100 1100 1100 3 [int] p [ int* ] a 1104 6 [int] 1108 9 [int] 12 1112 [int] 15 [int] 1116
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; 1FA0 1FB4 1100 1100 1100 3 [int] p [ int* ] a 1104 6 [int] 1108 9 [int] 12 1112 [int] 15 [int] 1116
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; 1FA0 1FB4 1100 1100 1100 4 [int] p [ int* ] a 1104 6 [int] 1108 9 [int] 12 1112 [int] 15 [int] 1116
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; 1FA0 1FB4 1100 1100 1100 4 [int] p [ int* ] a 1104 6 [int] 1108 9 [int] 12 1112 [int] 15 [int] 1116
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; 1FB4 1100 1100 4 [int] 1FA0 1104 a 1104 6 [int] p [ int* ] 1108 9 [int] 12 1112 [int] 15 [int] 1116
Dangling Pointers • Is pointer that does not point to an valid address. • Use of dangling pointer generates runtime error easily. int* p; // 'p' usually does not point to // a valid address. *p = 10; // Runtime error usually. ? p [ int* ]
Dynamic Memory Allocation (DMA) • To create new data dynamically. • We use new operator to create new data, and later use deleteoperator to release the memory used by the data. • The newoperator allocates the memory needed for the new data, and returns the address of the new data. int* p = newint; *p = 10; ... delete p; 1FA0 10 1104 1100 [int] p [ int* ]
Dynamic Memory Allocation (DMA) int* p; p = newint[4]; 1FA0 1104 1100 [int] p [ int* ] 1104 [int] 1108 [int] 1112 [int] [int] 1116
Dynamic Memory Allocation (DMA) int* p; p = newint[4]; p[0] = 12; p[1] = 24; p[2] = 15; p[3] = 35; 1FA0 1104 1100 12 [int] p [ int* ] 1104 22 [int] 1108 15 [int] 35 1112 [int]
Dynamic Memory Allocation (DMA) int* p; p = newint[4]; p[0] = 13; p[1] = 42; p[2] = 15; p[3] = 32; 1FA0 1104 1100 [int] p [ int* ] 1104 [int] 1108 [int] 1112 [int] ... ... ... delete[] p; p = NULL;