Chapter: 10               Pointers              Lecture: 40 and 41               Date: 22.10.2012
Memory Management:            new and delete operators   Array drawback: requires at program write-up how    big the arra...
Memory Management:           new and delete operatorsint main(){ char* str = “Idle hands are the devil’s workshop.”;   int...
Syntax: new operator
Memory obtained by new and pointer to it
The delete operator   The statement delete[] ptr returns to the system whatever    memory was pointed to by ptr.   The b...
Pointers to Objects   Pointers can point to objects as well as to simple data    types and arrays.   The statement Dista...
Linked Lists: A Chain of Pointers   Another way to store data   A linked list is a collection of nodes where each node  ...
Pointer Variable    The variable that stores the reference to another variable is     what we call a pointer.    e.g.,   ...
int* ptr;                                1270ptr = &IntVar1;           ptr                                         1271  c...
Linked Lists: Examplestruct link     //one element of list{int data;       //data itemlink* next;     //pointer to next li...
Linked Lists: Exampleclass linklist     //a list of links{private:link* first; //pointer to first linkpublic:linklist() //...
Linked Lists: Examplevoid linklist::additem(int d) //add data item{link* newlink = new link; //make a new linknewlink->dat...
Linked Lists: Examplevoid linklist::display() //display all links{link* current = first; //set ptr to first linkwhile( cur...
Linked Lists: A Chain of Pointers
Linked Lists: A Chain of Pointers
Assignment # 03   Constructor and destructor with new operator (p.462)   Pointer to Pointers (p.474 – p.479)   A parsin...
Upcoming SlideShare
Loading in …5
×

Lec 40.41 - pointers

253 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
253
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Student Book
  • Lec 40.41 - pointers

    1. 1. Chapter: 10 Pointers Lecture: 40 and 41 Date: 22.10.2012
    2. 2. Memory Management: new and delete operators Array drawback: requires at program write-up how big the array will be! The following array statement does not work! cin >> size; // get size from user int arr[size]; // error; array size must be a constant The problem is resolved with C++ operator called new The new operator obtains memory from the operating system and returns a pointer to its starting point.
    3. 3. Memory Management: new and delete operatorsint main(){ char* str = “Idle hands are the devil’s workshop.”; int len = strlen(str); //get length of str char* ptr; //make a pointer to char ptr = new char[len+1]; //set aside memory: string + ‘0’ strcpy(ptr, str); //copy str to new memory area ptr cout << “ptr=” << ptr << endl; //show that ptr is now in str delete[] ptr; //release ptr’s memory return 0; }
    4. 4. Syntax: new operator
    5. 5. Memory obtained by new and pointer to it
    6. 6. The delete operator The statement delete[] ptr returns to the system whatever memory was pointed to by ptr. The brackets following delete indicate that we’re deleting an array. If you create a single object with new, you don’t need the brackets when you delete it. ptr = new SomeClass; // allocate a single object ... delete ptr; // no brackets following delete
    7. 7. Pointers to Objects Pointers can point to objects as well as to simple data types and arrays. The statement Distance dist; defines an object called dist of class Distance. When we do not know how many objects to create, the new operator can be used in creating any number of objects at run time.
    8. 8. Linked Lists: A Chain of Pointers Another way to store data A linked list is a collection of nodes where each node consists of two fields: The first field holds the value or data and the second field holds the reference to the next node or null if the linked list is empty.
    9. 9. Pointer Variable The variable that stores the reference to another variable is what we call a pointer. e.g., Pointer-to Pointer/Pointer-variable int * ptr; //variable “ptr” as a pointer-to “int” ptr = &InVar;
    10. 10. int* ptr; 1270ptr = &IntVar1; ptr 1271 cout << ptr ; 1271 25 IntVar1 1272 ptr points-to to the 1273 address of IntVar1 1274 11 1275 IntVar2 1270 1271 25 1272 IntVar1int* ptr; ptr 1273ptr = &IntVar2; 1274 1274 cout << ptr ; 11 ptr points-to to the 1275 IntVar2 address of IntVar2
    11. 11. Linked Lists: Examplestruct link //one element of list{int data; //data itemlink* next; //pointer to next link};
    12. 12. Linked Lists: Exampleclass linklist //a list of links{private:link* first; //pointer to first linkpublic:linklist() //no-argument constructor{ first = NULL; } //no first linkvoid additem(int d); //add data item (one link)void display(); //display all links};
    13. 13. Linked Lists: Examplevoid linklist::additem(int d) //add data item{link* newlink = new link; //make a new linknewlink->data = d; //give it datanewlink->next = first; //it points to next linkfirst = newlink; //now first points to this}
    14. 14. Linked Lists: Examplevoid linklist::display() //display all links{link* current = first; //set ptr to first linkwhile( current != NULL ) //quit on last link{cout << current->data << endl; //print datacurrent = current->next; //move to next link}}
    15. 15. Linked Lists: A Chain of Pointers
    16. 16. Linked Lists: A Chain of Pointers
    17. 17. Assignment # 03 Constructor and destructor with new operator (p.462) Pointer to Pointers (p.474 – p.479) A parsing example + horse race simulation (p.479 – p.489)

    ×