The document discusses memory management in C++ using new and delete operators to dynamically allocate and free memory, and how pointers can be used to point to allocated memory blocks and objects. It also covers linked lists as another data structure that uses pointers to link objects together in a chain, providing an example linked list class with functions to add and display linked list elements.
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; }
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
}
}
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)