Pointer Variables int i; declares an int variable and sets aside a named memory location to store...

43
Pointer Variables int i; • declares an int variable and sets aside a named memory location to store the int int * iptr; • declares a variable that holds the address of an int; memory is allocated for the address (pointer) but not the int

Transcript of Pointer Variables int i; declares an int variable and sets aside a named memory location to store...

Page 1: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Pointer Variables

int i;

• declares an int variable and sets aside a named memory location to store the intint * iptr;

• declares a variable that holds the address of an int; memory is allocated for the address (pointer) but not the int

Page 2: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Pointing to an existing int

int i = 25;

• Allocates memory for an int and stores the valkue 25int * iptr;

• Allocates memory for a pointer to an intiptr = & i;

• sets the value of iptr to the address of i• i and *iptr are the same

– changing either one will change the other

iptr

i25

iptri25

Page 3: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Creating a new int

int j = 15;

• Allocates momory for an int and stores the value 15int * jptr; = new int;

• sets aside memory for an int and puts the address int j*j = j;

• stores value of j in memory pointed to by jptr

j15

jptr

15jptr j15

Page 4: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Pointers and Dynamic arrays

• A dynamic array is declared as a pointerint * iArray;

• use new to allocate appropriate amount of memoryiArray = new int[ desiredSize];

• use iArray just like you use any arrayiArray[index] = someValue;

Page 5: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Changing the size

• Allocate a new block of memory with newint *temp = new int[newSize];

• Copy the elements in the current memoryfor (int j=0; j<currentSize; j++)

temp[j] = iArray[i];

• delete the old memory– delete [] iArray;

• reassign the pointeriarray = temp;

Page 6: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Pointers and the “new” Operator

• Pointer Declarations– pointer variable of type “pointer to double”– can store the address of a double t in p

double *p;

• The new operator creates a variable of type double & puts the address of the variable in pointer p

p = new double;

• Dynamic allocation - memory is allocated while the program is running instead of before it starts

Page 7: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Pointers

• Actual address has no meaning

• Form: type *variable;• Example: double *p;

?

P

Page 8: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

new Operator

• Actually allocates storage

• Form: new type; // one memory location

new type [n]; // n memory locations

• Example: p = new double;

Page 9: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Pointer Variables

• If they aren't used for arrays, you have to use them differently

Page 10: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Accessing Data with Pointers

• * - indirection operator*p = 15.5;

• Stores floating value 15.5 in memory location *p - the location pointed to by p

15.5

p

Page 11: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Pointer Statements

double *p;

p = new doublet;

*p = 15.5;

cout << “The contents of the memory cell pointed to by p is “ << *p << endl;

OutputThe contents of memory cell pointed to by p is 15.5

Page 12: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Pointer Operations

• Pointers can only contain addresses

• So the following are errors:p = 1000;

p = 15.5;

• You need to assign an address to pp = &varOfAppropriateType

Page 13: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Pointer Operations

• Assignment of pointers if q & p are the same pointer typeq = p;• p and q both refer to the same memeory

location - the same variable

• relational operations == and != compare addresses not the values stored at those addresses

Page 14: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Pointers to Objects

class electric {public: string current; int volts;};electric *p, *q;• p and q are pointers to objects of type electric

Page 15: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Pointers to objects

p = new electric;• Allocates storage for struct of type electric and

places address into pointer p

? ?current voltsp

Page 16: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Assignments

*p.current = “AC”;*p.volts = 115;

• Statements above can also be written– p ->current = “AC”;– p ->volts = 115;

AC 115current voltsp

Page 17: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Member Access via Pointers

• From: p ->m• Example: p ->volts

• Example:– cout << p->current << p->volts << endl;

• Output– AC115

Page 18: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Pointers to Objects

q = new electric;• Allocates storage for object of type electric and

places address into pointer q• Copy contents of p struct to q struct

*q = *p;

AC 115

q->current q->voltsq

Page 19: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Pointers to Objects

q->volts = 220;

q = p;

AC 220

q->current q->voltsq

AC 115

AC 220

q

p q->current q->voltsp->current p->volts

Page 20: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

4/24/02 Section 2

• Final: Wed May 15, 1:00-3:00 pm– one problem from each of last two exams– practice exams

• Program 6 - due May 3– demos - May 1 - May 8– anyone who finishes early can make

arrangements to come in sooner

Page 21: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Appointment Class

• Want to write a program to make appointments for demos for program 6

• What should go into Appointment class?– need extra classes

• Time• Date

• How do we use the class?

Page 22: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Appointment.h

class Appointment {

public:

private:

}

Appointment(); // constructors

// set and get functionsTime start, end;Date day;String names[][], notes, userID;int howMany;

Page 23: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

13.2 Manipulating the Heap

• When new executes where is struct stored ?

• Heap– C++ storage pool available to new operator

• Effect of p = new node;

• Figure 14.1 shows Heap before and after executing new operator

Page 24: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Effect on new on the Heap

Page 25: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Returning Cells to the Heap

• Operation– delete p;

• Returns cells back to heap for re-use• When finished with a pointer delete it• Watch dual assignments and initialization

• Form: delete variable;• Example: delete p;

Page 26: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

13.3 Linked Lists

• Arrange dynamically allocated structures into a new structure called a linked list

• Think of a set of children’s pop beads• Connecting beads to make a chain• You can move things around and re-connect

the chain• We use pointers to create the same effect

Page 27: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Children’s Beads

Page 28: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Declaring Nodes

• If a pointer is included in a struct we can connect nodesstruct node

{

string word;

int count;

node *link;

};

node *p, *q, *r;

Page 29: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Declaring Nodes

• Each var p, q and r can point to a struct of type node– word (string)– count (int)– link (pointer to a node address)

word count link

Struct of type node

String Integer Address

Page 30: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Connecting Nodes

• Allocate storage of 2 nodesp = new node;q = new node;

• Assignment Statementsp->word = “hat”;p->count = 2;q->word = “top”;q->count = 3;

Page 31: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Figure 13.3

Page 32: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Connecting Nodes

• Link fields undefined until assignmentp->link = q;

• Address of q is stored in link field pointed to by p

• Access elements as followsq->word or p->link->word

• Null stored at last link fieldq->link = NULL; or p->link->link = NULL;

Page 33: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Connecting Nodes

Page 34: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Inserting a Node

• Create and initialize noder = new node;

r->word = “the”;

r->count = 5;

• Connect node pointed to by p to node pointed to by rp->link = r;

• Connect node pointed to by r to node pointed to

• by qr->link = q;

Page 35: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Inserting a New Node in a List

Page 36: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Insertion at Head of List

• OldHead points to original list headoldHead = p;

• Point p to a new nodep = new node;

• Connect new node to old list headp->link = oldHead;

Page 37: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Insertion at Head of List

Page 38: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Insertion at End of List

• Typically less efficient (no pointer)

• Attach new node to end of listlast->link = new node;

• Mark end with a NULLlast->link->link = NULL;

Page 39: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Insertion at End of List

Page 40: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Deleting a Node

• Adjust the link field to remove a node• Disconnect the node pointed to by r

p->link = r->link;

• Disconnect the node pointed to by r from its successorr->link = NULL;

• Return node to Heapdelete r;

Page 41: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Deleting a Node

Page 42: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

Traversing a List

• Often need to traverse a list• Start at head and move down a trail of

pointers• Typically displaying the various nodes

contents as the traversing continues• Advance node pointer

head = head->link;

• Watch use of reference parameters

Page 43: Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.

PrintList.cpp

// FILE: PrintList.cpp

// DISPLAY THE LIST POINTED TO BY HEAD

void printList (listNode *head)

{

while (head != NULL)

{

cout << head->word << " " << head ->count

<< endl;

head = head->link;

}

}