Post on 22-May-2015
PointersPointers
Chapter: Chapter: 1010
Lecture: 40 and 41Lecture: 40 and 41
Date: 22.10.2012Date: 22.10.2012
Memory Management: Memory Management: newnew and and deletedelete operatorsoperators
Array drawback: requires at program write-Array drawback: requires at program write-up how big the array will be!up how big the array will be!
The following array statement does not work!The following array statement does not work!
cin >> size; cin >> size; // get size from user// get size from user
int arr[size]; int arr[size]; // error; array size must be a // error; array size must be a constantconstant
The problem is resolved with C++ operator The problem is resolved with C++ operator calledcalled newnew
The The new new operator obtains memory from the operator obtains memory from the operating system and returns a pointer to its operating system and returns a pointer to its starting point.starting point.
Memory Management: Memory Management: newnew and and deletedelete operatorsoperators
int main()int main()
{ char* str = “Idle hands are the devil’s workshop.”;{ char* str = “Idle hands are the devil’s workshop.”;
int len = strlen(str);int len = strlen(str); //get length of str//get length of str
char* ptr; char* ptr; //make a pointer to char//make a pointer to char
ptr = ptr = newnew char[len+1]; char[len+1]; //set aside memory: //set aside memory: string + ‘\0’string + ‘\0’
strcpy(ptr, str); strcpy(ptr, str); //copy str to new memory area ptr//copy str to new memory area ptr
cout << “ptr=” << ptr << endl; cout << “ptr=” << ptr << endl; //show that ptr is //show that ptr is now in strnow in str
deletedelete[] ptr; [] ptr; //release ptr’s memory//release ptr’s memory
return 0; }return 0; }
Syntax: Syntax: newnew operator operator
Memory obtained by Memory obtained by newnew and and pointer to itpointer to it
The The deletedelete operatoroperator
The statementThe statement delete[] ptr delete[] ptr returns to the returns to the system whatever memory was pointed to by system whatever memory was pointed to by ptrptr..
The brackets following delete indicate that The brackets following delete indicate that we’re deleting an array. If you create a single we’re deleting an array. If you create a single object with new, you don’t need the brackets object with new, you don’t need the brackets when you delete it.when you delete it.
ptr = new SomeClass;ptr = new SomeClass; // allocate a single // allocate a single objectobject
. . .. . .
delete ptr; delete ptr; // no brackets following delete// no brackets following delete
Pointers to ObjectsPointers to Objects Pointers can point to objects as well as to Pointers can point to objects as well as to
simple data types and arrays.simple data types and arrays.
The statement The statement Distance dist;Distance dist; defines an defines an object called object called distdist of class of class DistanceDistance..
When we do not know how many objects When we do not know how many objects to create, the to create, the newnew operator can be used operator can be used in creating any number of objects at run in creating any number of objects at run time.time.
Linked Lists: A Chain of Linked Lists: A Chain of PointersPointers
Another way to store dataAnother way to store data A linked list is a collection of nodes A linked list is a collection of nodes
where each node consists of two fields:where each node consists of two fields: The first field holds the value or data and the The first field holds the value or data and the second field holds the reference to the next second field holds the reference to the next node or null if the linked list is empty.node or null if the linked list is empty.
Pointer VariablePointer Variable
The variable that stores the reference to The variable that stores the reference to another variable is what we call a another variable is what we call a pointerpointer. .
e.g.,e.g.,
int int ** ptrptr; ; //variable “ptr” as a //variable “ptr” as a pointer-to “int”pointer-to “int”
ptr = &InVar;ptr = &InVar;
Pointer/Pointer-variable
Pointer-to
1270
1271
1272
1273
1274
1275
IntVar1
IntVar2
25
11
1271
ptr points-to to the address of IntVar1
1270
1271
1272
1273
1274
1275
IntVar1
IntVar2
25
11
ptr
1274
ptr
ptr points-to to the address of IntVar2
int* ptr;
ptr = &IntVar1; cout << ptr ;
int* ptr;
ptr = &IntVar2; cout << ptr ;
Linked Lists: ExampleLinked Lists: Example
struct link //one element of list{int data; //data itemlink* next; //pointer to next link};
Linked Lists: ExampleLinked 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};
Linked Lists: ExampleLinked Lists: Example
void 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}
Linked Lists: ExampleLinked Lists: Example
void 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}}
Linked Lists: A Chain of Linked Lists: A Chain of PointersPointers
Linked Lists: A Chain of Linked Lists: A Chain of PointersPointers
Assignment # Assignment # 0303
Constructor and destructor with Constructor and destructor with newnew operator (p.462)operator (p.462)
Pointer to Pointers (p.474 – p.479)Pointer to Pointers (p.474 – p.479)
A parsing example + horse race A parsing example + horse race simulation (p.479 – p.489)simulation (p.479 – p.489)