03 pointers ll
TRANSCRIPT
-
8/13/2019 03 Pointers LL
1/62
Dr. Ahmad R. Hadaegh
A.R. Hadaegh Nati onal University Page 1
Pointer:
Dynamic Allocation
This lecture prepared by the instructors at the University of Manitoba in Canada
and has been modified by Dr. Ahmad Reza Hadaegh
-
8/13/2019 03 Pointers LL
2/62
Dr. Ahmad R. Hadaegh
A.R. Hadaegh Nati onal University Page 2
Dynamic Allocation
- Main memory can be thought of as a (very)
large one-dimensional array of memory
locations
- Each location holds 1 byte and has its ownaddress
- Starts at 0 in increments of 1
- Usually in hexadecimal
- 0000 - FFFF represents 64K of memory- 64 * 1024 = 65,536 memory locations
0000
0001
0002
0003
0004
0005
FFFF
-
8/13/2019 03 Pointers LL
3/62
Dr. Ahmad R. Hadaegh
A.R. Hadaegh Nati onal University Page 3
Dynamic Allocation
- When variables are declared they are allocated memory
- An integer requires (say) 4 bytes and thus gets 4
consecutive locations in memory
- Most machines would store an int in 4 bytes
- The address of the integer is the first byte and is
effectively stored via the variable name
-
8/13/2019 03 Pointers LL
4/62
Dr. Ahmad R. Hadaegh
A.R. Hadaegh Nati onal University Page 4
Dynamic Allocation
- An integer variable X would effectively point to the
starting address of the 4 bytes required
- X must be dereferenced in order to be used
- Dereference means to interpret what exists at a
particular memory location
- Dereference is implicit here
memoryinteger variable X
-
8/13/2019 03 Pointers LL
5/62
Dr. Ahmad R. Hadaegh
A.R. Hadaegh Nati onal University Page 5
Dynamic Allocation
- It is possible to explicitly declare a variablethat contains the address of other variables
- Creating variables that are pointers
-
8/13/2019 03 Pointers LL
6/62
Dr. Ahmad R. Hadaegh
A.R. Hadaegh Nati onal University Page 6
Dynamic Allocation
typedef
int* intptr;
int main ( ) {
intptr ptr1;
intptr is a pointer to
an integer
Must be a pointer to a valid C++ type
(either built-in or user defined type)
-
8/13/2019 03 Pointers LL
7/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 7
Dynamic Allocation
Syntax notes:
int main ( ) {
int* ptr1;
int* ptr2;int* ptr3;
int main ( ) {
int *ptr1, *ptr2, *ptr3;
is the same as ...
-
8/13/2019 03 Pointers LL
8/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 8
Dynamic Allocation
- A pointer in C++ must point to storage of a particular typeptr1 is a pointer to an integer
- ptr1 is a 4 byte variable that is able to point to a 4
byte variable
- The value of ptr1 is not initialized and is at this
point garbage
-
8/13/2019 03 Pointers LL
9/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 9
Dynamic Allocation
- To allocate storage for ptr1 to point to:
4 bytes of storageallocated for ptr1
to point to
typedef
int* intptr;
int main ( ) {
intptr ptr1;ptr1 = new int;
-
8/13/2019 03 Pointers LL
10/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 10
Dynamic Allocation
- Unlike normal variables, pointers must be explicitly
dereferenced
*ptr1 = 200;
Store 200 in the
4 bytes pointed toby ptr1
-
8/13/2019 03 Pointers LL
11/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 11
Dynamic Allocation
- Other manipulation may take place
i = *ptr1;
Assign 200 to integer variable i
-
8/13/2019 03 Pointers LL
12/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 12
Dynamic Allocation
Other manipulation may not sensibly take place
i = ptr1;
Assign the address
that ptr1 points to
to integer variable i?
-
8/13/2019 03 Pointers LL
13/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 13
Dynamic Allocation
- What happens here? ptr1 only exists once --as such, each new points
ptr1 to newly allocated
2 byte area
The first 999 storage areas
are reserved by the OS, but
no longer accessible
Memory leak!
typedef int* intptr;
void main () {intptr ptr1; int i;
for (i=1;i
-
8/13/2019 03 Pointers LL
14/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 14
Dynamic Allocation
- Storage that is allocated via new should be freed via
delete during the execution of a program
Not really useful, but
you get the point
typedef int* intptr;
void main ()
{intptr ptr1; int i;
for (i=1;i
-
8/13/2019 03 Pointers LL
15/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 15
Dynamic Allocation
typedef int* intptr;
void main () {
intptr P, Q;
P = new int;
*P = 1
Q = new int;*Q = 2;
cout
-
8/13/2019 03 Pointers LL
16/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 16
Dynamic Allocation
typedef int* intptr;
void main () {
intptr P, Q;
P = new int;
*P = 1Q = new int;
*Q = 2;
cout
-
8/13/2019 03 Pointers LL
17/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 17
Dynamic Allocation
typedef int* intptr;
void main () {
intptr P, Q;
P = new int;
* P = 1Q = new int;
*Q = 2;
cout
-
8/13/2019 03 Pointers LL
18/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 18
Dynamic Allocation
typedef int* intptr;
void main () {
intptr P, Q;
P = new int;
*P = 1Q = new int;
*Q = 2;
cout
-
8/13/2019 03 Pointers LL
19/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 19
Dynamic Allocation
typedef int* intptr;
void main () {
intptr P, Q;
P = new int;
*P = 1Q = new int;
*Q = 2;
cout
-
8/13/2019 03 Pointers LL
20/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 20
Dynamic Allocation
typedef int* intptr;
void main () {
intptr P, Q;
P = new int;
*P = 1Q = new int;
*Q = 2;
cout
-
8/13/2019 03 Pointers LL
21/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 21
Dynamic Allocation
typedef int* intptr;
void main () {
intptr P, Q;
P = new int;
*P = 1Q = new int;
*Q = 2;
cout
-
8/13/2019 03 Pointers LL
22/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 22
Dynamic Allocation
typedef int* intptr;
void main () {
intptr P, Q;
P = new int;
*P = 1Q = new int;
*Q = 2;
cout
-
8/13/2019 03 Pointers LL
23/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 23
Dynamic Allocation
typedef int* intptr;
void main () {
intptr P, Q;
P = new int;
*P = 1
Q = new int;
*Q = 2;
cout
-
8/13/2019 03 Pointers LL
24/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 24
Dynamic Allocation
typedef int* intptr;
void main () {
intptr P, Q;
P = new int;
*P = 1Q = new int;
*Q = 2;
cout
-
8/13/2019 03 Pointers LL
25/62Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 25
Dynamic Allocation
* P = 7;
cout
-
8/13/2019 03 Pointers LL
26/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 26
Dynamic Allocation
*P = 7;
cout
-
8/13/2019 03 Pointers LL
27/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 27
Dynamic Allocation
*P = 7;
cout
-
8/13/2019 03 Pointers LL
28/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 28
Dynamic Allocation
*P = 7;cout
-
8/13/2019 03 Pointers LL
29/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 29
Dynamic Allocation
*P = 7;
cout
-
8/13/2019 03 Pointers LL
30/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 30
Dynamic Allocation
*P = 7;
cout
-
8/13/2019 03 Pointers LL
31/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 31
Dynamic Allocation
NULL- Is a built-in constant that sets a pointer variable to
something that can not be dereferenced
- Also makes it clear that a pointer variable is in fact not
pointing to anything- A garbage pointer may be non NULL and thus look like
its pointing to something
i f (some_pointer = = NULL)
cout
-
8/13/2019 03 Pointers LL
32/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 32
Dynamic Allocation
- Pointer variables can be initialized to NULL
intptr a=NULL, b=NULL, c=NULL;
- Probably a good habit to set pointer variables to NULL
after deleting them
-
8/13/2019 03 Pointers LL
33/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 33
Dynamic Allocation
#include
using namespace std;
typedef int* intptr;
void main () {
intptr ptr1;
ptr1 = new int;
*ptr1 = 12345;
delete ptr1;
cout
-
8/13/2019 03 Pointers LL
34/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 34
Dynamic Allocation
#include
using namespace std;
typedef int* intptr;
void main (){
intptr ptr1;
ptr1 = new int;
*ptr1 = 12345;
delete ptr1;
ptr1 = NULL;
cout
-
8/13/2019 03 Pointers LL
35/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 35
Dynamic Allocation
- Using NULL does not guarantee that that you protectyourself from doing something silly as in:
Dynamic Allocation
-
8/13/2019 03 Pointers LL
36/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 36
Dynamic Allocation
#include
using namespace std;
typedef int* intptr;
void main () {
intptr ptr1;ptr1 = new int;
*ptr1 = 12345;
delete ptr1;
ptr1 = NULL;
ptr1 = new int;
cout
-
8/13/2019 03 Pointers LL
37/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 37
Dynamic Allocation
#include
using namespace std;
typedef int* intptr;
void main () {
intptr ptr1, ptr2;
ptr1 = new int;
*ptr1 = 12345;
delete ptr1;
ptr1 = NULL;
ptr2 = new int;ptr1 = new int;
cout
-
8/13/2019 03 Pointers LL
38/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 38
Dynamic Allocation
- When dealing with pointers, you areresponsible for ensuring they do not dangle
Note:
- Dynamic allocation is one of the most
important topic of this course. So you need
understand it very well.
-
8/13/2019 03 Pointers LL
39/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 39
Link Lists
-
8/13/2019 03 Pointers LL
40/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 40
Link List: Linked structures
- Linked lists often provide an elegant alternative to
structures such as arrays
- A linked list is readily created in most procedural
languages Linked lists are often represented in the
following manner:
-
8/13/2019 03 Pointers LL
41/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 41
Linked Lists
node
Pointer to
next node
Last nodes pointer
is NULL
top
27 -38 4 36
Data
-
8/13/2019 03 Pointers LL
42/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 42
Linked List
- The simplest linked structure
- Sometimes called a linear linked list
- The result of having an initial pointer to a node
- And dynamically created nodes that point to other nodes
- A dynamic incarnation of a simple array
- Both have their advantages
-
8/13/2019 03 Pointers LL
43/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 43
Linked Lists
- Big advantage over array in that linked list isdynamically created
- Use only as many nodes as required for data
- List grows and shrinks accordingly
- Linked lists are made up of a pointer (top in this case) that points
to the first of a collection of homogeneous nodes
- Unlike the nodes, top contains no data top is not
dynamically
created while the rest of the linked structure is
-
8/13/2019 03 Pointers LL
44/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 44
Declaration for linked list
class node;
typedef node* nodeptr;class node {
public:
int number;
nodeptr next;};
//--------------------------------
int main ( )
{nodeptr top;
.
}
Only variable declared
-
8/13/2019 03 Pointers LL
45/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 45
Linked Lists
- All that exists at this time is an uninitialized pointer to a node
- No nodes have been allocated yet
- To make it clear the linked list is empty
top = NULL;
NULL is a specialC++ constant
-
8/13/2019 03 Pointers LL
46/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 46
Dereferencing: Allocating a node
- The Code:
top = new node;
- Allocates space for a new node and place its startingmemory address in top
-
8/13/2019 03 Pointers LL
47/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 47
Dereferencing
- Given that a node exists and given that a pointer exists that
contains the nodes address, the contents of the node may
be modified
- Again, using a pointer to reference storage is called
dereferencing
top -> number = 123;
top -> next = NULL;
Note the arrow notation
that allows access to a
field within a node
-
8/13/2019 03 Pointers LL
48/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 48
Dereferencing
- Now have:
top
Remember that top is a
declared variable -- a pointer
to a node
This node is not a variable --it is dynamically allocated
via the new statement
123
-
8/13/2019 03 Pointers LL
49/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 49
New (again)
top = new node; // again!
top
123
top
123
top now pointsto a new node
Old node still exists, but
can no longer be accessed --
memory leak!
-
8/13/2019 03 Pointers LL
50/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 50
Delete
- Linked lists are dynamic which means they can
grow and shrink appropriately
- Grow with new
- Shrink with delete
Delete
-
8/13/2019 03 Pointers LL
51/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 51
Delete
- delete top;
top
123
top
123
top now contains
garbage
Node might still exist, but
is at the control of the OS
?
-
8/13/2019 03 Pointers LL
52/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 52
Insert
- Inserting many nodes:
- Important to be able to insert more than a single
node into a linked list
-
8/13/2019 03 Pointers LL
53/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 53
Insert
- Assume an existing linked list structure and definition
nodeptr newnode;
top
27 -38 4 36
I t
-
8/13/2019 03 Pointers LL
54/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 54
Insert
newnode = new node;
newnode -> number = 123;
newnode -> next = top;
top = newnode;
top
27 -38 4 36
123
newnode
This code assumes you alwayswant to place a newly allocated
node at the front of the list
-
8/13/2019 03 Pointers LL
55/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 55
Searching
- Many operations upon a linked structures involve searching.
bool search (nodeptr top, int key)
{
nodeptr curr=top;
bool found=false;while ((curr != NULL) && (!found))
if (curr -> number == key)
found = true;
else
curr = curr -> next;
return (found);
}
-
8/13/2019 03 Pointers LL
56/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 56
More on Inserting
- Have seen inserting a node to the front of a linked list
- Possible that this might not be satisfactory
- If a list is to be maintained in sorted order (forexample), an insert might have to be done in the
middle or the the end of the list
More on Insert
-
8/13/2019 03 Pointers LL
57/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 57
Find insertion
point (prev)
Insert
void insert (nodeptr& top, int item){
nodeptr curr=top, prev=NULL, newnode;
bool found=false;newnode = new node;newnode -> number = item;while ((curr != NULL) and (!found))
if (item > curr -> number){
prev = curr;curr = curr -> next;
}else
found = true;
newnode -> next = curr;if (prev == NULL)top = newnode;
elseprev -> next = newnode;
}
Deleting
-
8/13/2019 03 Pointers LL
58/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 58
Deleting
- Removing items from a linked list will normally require a
search and knowledge of different cases of delete (much likeinsert)
- Deleting the first node of a linked list requires the manipulation
of the top pointer
- Deleting a node in the middle of a linked list requires the
manipulation of pointers within allocated nodes -- top pointer
will remain unchanged
- Deleting at the end of a linked list requires the sameoperations as delete in the middle
Deleting
-
8/13/2019 03 Pointers LL
59/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 59
void remove (nodeptr& top, int key){
nodeptr curr, temp;
// Code assumes key will be foundif (key == top -> number){
temp = top;top = top -> next;
}else{
curr = top;while (curr -> next -> number != key)
curr = curr -> next;temp = curr -> next;curr -> next = curr -> next -> next;
}delete temp;
}
Double dereference
Deleting
-
8/13/2019 03 Pointers LL
60/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 60
g
- For every node that is allocated via new, there should be a
node given back to the OS via delete
- This implies that a function should probably exist to
destroy an existing linked list
void destroy (nodeptr& top){
nodeptr curr=top, temp;
while (curr != NULL)
{
temp = curr;
curr = curr -> next;
delete temp;
}
top = NULL;
}
If you declare and create a
linked list within a function
without destroying it, you
will have a memory leak when
you leave the function ends
Copying
-
8/13/2019 03 Pointers LL
61/62
Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 61
- Functions can work with more than one linked list at a time- Here is a function that copies one linked list to another:
void copy (nodeptr atop, nodeptr& btop) {nodeptr acurr, bcurr;destroy (btop); // deleted previous nodes in the list if there is anyif (atop != NULL) {
btop = new node;btop -> number = atop -> number;
acurr = atop;bcurr = btop;while (acurr -> next != NULL) {
bcurr -> next = new node;acurr = acurr -> next;bcurr = bcurr -> next;
bcurr -> number = acurr -> number;}
bcurr -> next = NULL;}
}
Linked lists
-
8/13/2019 03 Pointers LL
62/62
Linked lists
- Linked lists are general purpose structures that can bemanipulated to solve a diverse set of problems
- Working successfully with linked lists takes study and
practice
- It really helps if you understand the basics