SlideShare una empresa de Scribd logo
1 de 17
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 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.
Memory Management:
           new and delete operators
int 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; }
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 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
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.
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.
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;
int* ptr;                                1270
ptr = &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        IntVar1
int* ptr;                ptr             1273
ptr = &IntVar2;
                          1274           1274
  cout << ptr ;
                                                11
                  ptr points-to to the   1275        IntVar2
                  address of IntVar2
Linked Lists: Example


struct link     //one element of list
{
int data;       //data item
link* next;     //pointer to next link
};
Linked Lists: Example
class linklist     //a list of links
{
private:
link* first; //pointer to first link
public:
linklist() //no-argument constructor
{ first = NULL; }          //no first link
void additem(int d); //add data item (one link)
void display(); //display all links
};
Linked Lists: Example

void linklist::additem(int d) //add data item
{
link* newlink = new link; //make a new link
newlink->data = d; //give it data
newlink->next = first; //it points to next link
first = newlink; //now first points to this
}
Linked Lists: Example

void linklist::display() //display all links
{
link* current = first; //set ptr to first link
while( current != NULL ) //quit on last link
{
cout << current->data << endl; //print data
current = current->next; //move to next link
}
}
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 parsing example + horse race simulation (p.479 –
    p.489)

Más contenido relacionado

Destacado

Destacado (7)

Lec 38.39 - pointers
Lec 38.39 -  pointersLec 38.39 -  pointers
Lec 38.39 - pointers
 
Lec 36 - pointers
Lec 36 -  pointersLec 36 -  pointers
Lec 36 - pointers
 
Lec 50
Lec 50Lec 50
Lec 50
 
Lec 49 - stream-files
Lec 49 - stream-filesLec 49 - stream-files
Lec 49 - stream-files
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array Pointer
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 

Similar a Lec 40.41 - pointers

How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdffeelinggift
 
For this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docxFor this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docxmckellarhastings
 
Lecture11 standard template-library
Lecture11 standard template-libraryLecture11 standard template-library
Lecture11 standard template-libraryHariz Mustafa
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfpoblettesedanoree498
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxMeghaKulkarni27
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfKylaMaeGarcia1
 
Unit 1 LINEAR DATA STRUCTURES
Unit 1  LINEAR DATA STRUCTURESUnit 1  LINEAR DATA STRUCTURES
Unit 1 LINEAR DATA STRUCTURESUsha Mahalingam
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfEricvtJFraserr
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxBelow is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxgilliandunce53776
 
Data structure
Data structureData structure
Data structureNida Ahmed
 

Similar a Lec 40.41 - pointers (20)

Lec 37 - pointers
Lec 37 -  pointersLec 37 -  pointers
Lec 37 - pointers
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
For this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docxFor this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docx
 
Lecture11 standard template-library
Lecture11 standard template-libraryLecture11 standard template-library
Lecture11 standard template-library
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 
Unit 1 LINEAR DATA STRUCTURES
Unit 1  LINEAR DATA STRUCTURESUnit 1  LINEAR DATA STRUCTURES
Unit 1 LINEAR DATA STRUCTURES
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
Pointers and arrays
Pointers and arraysPointers and arrays
Pointers and arrays
 
Link list
Link listLink list
Link list
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxBelow is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docx
 
Data structure
Data structureData structure
Data structure
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 

Más de Princess Sam

Lec 47.48 - stream-files
Lec 47.48 - stream-filesLec 47.48 - stream-files
Lec 47.48 - stream-filesPrincess Sam
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functionsPrincess Sam
 
Lec 33 - inheritance
Lec 33 -  inheritanceLec 33 -  inheritance
Lec 33 - inheritancePrincess Sam
 
Lec 30.31 - inheritance
Lec 30.31 -  inheritanceLec 30.31 -  inheritance
Lec 30.31 - inheritancePrincess Sam
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloadingPrincess Sam
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-stringsPrincess Sam
 

Más de Princess Sam (7)

Lec 47.48 - stream-files
Lec 47.48 - stream-filesLec 47.48 - stream-files
Lec 47.48 - stream-files
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functions
 
Lec 33 - inheritance
Lec 33 -  inheritanceLec 33 -  inheritance
Lec 33 - inheritance
 
Lec 30.31 - inheritance
Lec 30.31 -  inheritanceLec 30.31 -  inheritance
Lec 30.31 - inheritance
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 

Lec 40.41 - pointers

  • 1. Chapter: 10 Pointers Lecture: 40 and 41 Date: 22.10.2012
  • 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. Memory Management: new and delete operators int 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; }
  • 5. Memory obtained by new and pointer to it
  • 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. 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. 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. 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. int* ptr; 1270 ptr = &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 IntVar1 int* ptr; ptr 1273 ptr = &IntVar2; 1274 1274 cout << ptr ; 11 ptr points-to to the 1275 IntVar2 address of IntVar2
  • 11. Linked Lists: Example struct link //one element of list { int data; //data item link* next; //pointer to next link };
  • 12. Linked Lists: Example class linklist //a list of links { private: link* first; //pointer to first link public: linklist() //no-argument constructor { first = NULL; } //no first link void additem(int d); //add data item (one link) void display(); //display all links };
  • 13. Linked Lists: Example void linklist::additem(int d) //add data item { link* newlink = new link; //make a new link newlink->data = d; //give it data newlink->next = first; //it points to next link first = newlink; //now first points to this }
  • 14. Linked Lists: Example void linklist::display() //display all links { link* current = first; //set ptr to first link while( current != NULL ) //quit on last link { cout << current->data << endl; //print data current = current->next; //move to next link } }
  • 15. Linked Lists: A Chain of Pointers
  • 16. Linked Lists: A Chain of Pointers
  • 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)

Notas del editor

  1. Student Book