1 / 14

Revision on C++ Pointers

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)

ellard
Télécharger la présentation

Revision on C++ Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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).

  3. 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'.

  4. 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;

  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; 1FA0 1FB4 1100 1100 1100 3 [int] p [ int* ] a 1104 6 [int] 1108 9 [int] 12 1112 [int] 15 [int] 1116

  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; 1FA0 1FB4 1100 1100 1100 3 [int] p [ int* ] a 1104 6 [int] 1108 9 [int] 12 1112 [int] 15 [int] 1116

  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; 1FA0 1FB4 1100 1100 1100 4 [int] p [ int* ] a 1104 6 [int] 1108 9 [int] 12 1112 [int] 15 [int] 1116

  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; 1FA0 1FB4 1100 1100 1100 4 [int] p [ int* ] a 1104 6 [int] 1108 9 [int] 12 1112 [int] 15 [int] 1116

  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; 1FB4 1100 1100 4 [int] 1FA0 1104 a 1104 6 [int] p [ int* ] 1108 9 [int] 12 1112 [int] 15 [int] 1116

  10. 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* ]

  11. 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* ]

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

  13. 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]

  14. 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;

More Related