pointer variables
play

Pointer Variables Chapter 4: (Pointers and) Linked Lists Declaring - PowerPoint PPT Presentation

Pointer Variables Chapter 4: (Pointers and) Linked Lists Declaring a variable creates space for it Pointer variables in a region of process memory called stack Operations on pointer variables each memory cell has an address


  1. Pointer Variables Chapter 4: (Pointers and) Linked Lists � Declaring a variable creates space for it � Pointer variables � in a region of process memory called stack � Operations on pointer variables � each memory cell has an address � Linked lists � memory can be considered to be linearly addressed starting from 0 to MAX � Operations on linked lists int var = 268; var � Variations on simple linked lists � � � � � 268 � � � doubly linked lists 0x000 0x498 0x999 � circular linked lists � Use pointers to refer to variables indirectly by pointing at them EECS 268 Programming II 1 EECS 268 Programming II 2 Pointer Variable � Declaration Pointer Variable � Assignment � Can assign address of any variable (including another � A pointer contains the location, or address in pointer variable) to the pointer variable memory, of a memory cell int var = 268; int *p = &var; � Declaration of an integer pointer variable p p var � � � static allocation; initially undefined, but not NULL � 0x498 � 268 � � int var = 268; 0x000 0x490 0x498 0x999 � Indirect updates through pointer variables int *p; *p = 168; p var p var � � � � � NA � 268 � � � 0x498 � 168 � � 0x498 0x000 0x490 0x999 0x000 0x490 0x498 0x999 4 EECS 268 Programming II 3 EECS 268 Programming II

  2. Pointer Variable � Assignment Pointer Variable � Types � & : address-of operator � All pointer variables hold integer addresses, but � ���������������� - �������������������� have types � expression *p represents the memory cell to which p � very important during pointer arithmetic points int a, *ip = &a, **pp; � Pointer variables are also variables! char c, *cp = &c; � need space in memory ip ���������������������������� ip �������� � can have pointer variables pointing to other pointer cp ���������������������������� cp ������ variables pp = &a; // Is this valid ? int a, *p, **pp; � Multiple/divide with pointer variables generally is p = &a; pp = &p; not meaningful see C4-pointers.cpp EECS 268 Programming II 5 EECS 268 Programming II 6 New Operator Delete Operator � All declared variables, arrays are statically � Memory available to a program is limited assigned space (on the stack) by the compiler � return dynamically allocated memory to the � Can also allocate space dynamically at runtime system if no longer needed � use the new operator � use the delete operator int *p = new int; int *p = new int(268); double *dp = new double(4.5); cout ������������������������ my_class *instance = new my_class(); delete p; � if the operator new cannot allocate memory, it throws the exception std::bad_alloc (in the <new> header) � very uncommon see C4-funcarg.cpp EECS 268 Programming II 7 EECS 268 Programming II 8

  3. � ������������������������������������������������ De-allocating Memory Memory Leak � A memory leak is another common problem � delete leaves the variable contents undefined when using pointers and dynamic memory � a pointer to a deallocated memory (*p) cell is � happens when allocated memory can no longer be possible and dangerous reached � deallocated memory can be reassigned after � so, cannot be de-allocated! another call to new � wastes memory resources, eventually system will run out of memory refers to undefined memory int i, *ip; ip = new int(268); � called the dangling pointer error ip = &i; // memory leak! � p = NULL; // safeguard see C4-dangling.cpp EECS 268 Programming II 9 EECS 268 Programming II 10 Pointer Examples Pointers EECS 268 Programming II 11 EECS 268 Programming II 12

  4. Best Practices Dynamic Allocation of Arrays � Memory allocated using new should be deallocated � ������������������������������������������������ using delete int arraySize = 50; � destructor is a good place to deallocate memory double *anArray = new double[arraySize ]; � implicitly called once object goes out of scope � delete[] to release array memory � can also be called explicitly when object no longer needed delete[] anArray; � Do not call delete again to de-allocate same memory � The size of a dynamically allocated array can be � usually happened unintentionally! increased � Do not call delete on a pointer � that is not initialized or is NULL, double *oldArray = anArray; � that is pointing to a variable not allocated using new anArray = new double[2*arraySize]; EECS 268 Programming II 13 EECS 268 Programming II 14 Arrays and Pointers Linked List? � ������������������������������������������������ � Options for implementing an ADT List � Pointer variable assigned to an array name can � Array has a fixed size be used just like an array � Data must be shifted during insertions and deletions int arr[100], *ip; � Linked list is able to grow in size as needed ip = arr; � Does not require the shifting of items during insertions and deletions for(i=0 ; i<100 ; i++) ip[i] = arr[i]+1; // ip and arr are aliased � ip[i], arr[i], *(ip+i) all point to the same location. EECS 268 Programming II 15 EECS 268 Programming II 16

  5. Linked List ? Pointer-Based Linked Lists � A node in a linked list is usually a struct struct Node { int item Node *next; }; // end Node � The head pointer points to the first node in a linked list Figure 4-1 ( a) A linked list of integers; (b) insertion; (c) deletion Figure 4-7 A head pointer to a list EECS 268 Programming II 17 EECS 268 Programming II 18 Pointer-Based Linked Lists Displaying the Contents of a Linked List � If head is NULL , the linked list is empty � Reference a node member with the -> operator p->item � A node is dynamically allocated � Visits each node in the linked list Node *p; // pointer to node � pointer variable cur keeps track of current node p = new Node; // allocate node for (Node *cur = head; cur != NULL; cur = cur->next) cout << cur->item << endl; EECS 268 Programming II 19 EECS 268 Programming II 20

  6. � Deleting a Specified Node from a Displaying the Contents of a Linked List Linked List � Deleting an interior node prev->next = cur->next; Figure 4-9 The effect of the assignment cur = cur->next Figure 4-10 Deleting a node from a linked list EECS 268 Programming II 21 EECS 268 Programming II 22 Deleting the First Node from a Inserting a Node into a Specified Linked List Position of a Linked List To insert a node between two nodes � Deleting the first node newPtr->next = cur; head = head->next; prev->next = newPtr; Figure 4-12 Inserting a new node into a linked list Figure 4-11 Deleting the first node EECS 268 Programming II 23 EECS 268 Programming II 24

  7. Inserting a Node at the Beginning of a Inserting a Node into a Specified Linked List Position of a Linked List � To insert a node at the beginning of a linked � Finding the point of insertion or deletion for a list sorted linked list of objects newPtr->next = head; Node *prev, *cur; head = newPtr; for (prev = NULL, cur = head; (cur != NULL )&&(newValue > cur->item); prev = cur, cur = cur->next); Figure 4-13 Inserting at the beginning of a linked list EECS 268 Programming II 25 EECS 268 Programming II 26 A Pointer-Based Implementation of Constructors and Destructors the ADT List � Public methods � Private data members � Default constructor initializes size and head � isEmpty � head � A destructor is required for de-allocating � getLength � size dynamically allocated memory � insert � Local variables to � else, we will have a memory leak! � remove methods List::~List() � retrieve � cur { � Private method while (!isEmpty()) � prev � find remove(1); } // end destructor see C4-ListP.cpp EECS 268 Programming II 27 EECS 268 Programming II 28

  8. Constructors and Destructors Shallow Copy vs. Deep Copy � Copy constructor creates a deep copy � copies size, head, and the linked list � the copy of head points to the copied linked list � In contrast, a shallow copy � copies size and head � the copy of head points to the original linked list � If you omit a copy constructor, the compiler generates one � but it is only sufficient for implementations that use statically allocated arrays Figure 4-18 Copies of the linked list in Figure 4-17; (a) a shallow copy; (b) a deep copy EECS 268 Programming II 29 EECS 268 Programming II 30 Comparing Array-Based and Pointer- Comparing Array-Based and Pointer- Based Implementations Based Implementations � Size � Retrieval � increasing the size of a resizable array can waste � the time to access the ith item storage and time � Array-based: Constant (independent of i) � linked list grows and shrinks as necessary � Pointer-based: Depends on i � Insertion and deletion � Storage requirements � Array-based: Requires shifting of data � array-based implementation requires less memory than a pointer-based one for each item in the ADT � Pointer-based: Requires a traversal EECS 268 Programming II 31 EECS 268 Programming II 32

Recommend


More recommend