Week 19 Dynamic Data Structure
Week 19 Dynamic Data Structure
3
Dynamic memory
Allows you to decide at run time how much
space is needed.
Very useful with lists, stacks and queues.
We do not have to worry about specifying an
upper limit to the size needed.
We can customise our lists to fit the amount of
information that will be stored.
We can expand and shrink the lists to respond to
additions and deletions
4
What do you need to do to reserve
memory dynamically?
You need a special sort of variable that holds an address
A pointer is a variable that holds an address.
You must use a special type of declaration to create a pointer
variable.
You need to know what sort of object will be created
You need to use a special notation
E.g to create a pointer that contains an address of an integer
int * x;
The asterisk informs the compiler that x holds an address. And that
this will be an address of an integer.
You also need two C++ operators for dynamic memory allocation
new and delete
5
Memory Pool
Every program is provided with a pool of
memory it can use during execution.
This is called the free store or heap
The size of this heap depends on your
system.
Local variables and function parameters
are stored in another pool called the
stack
6
To create an integer dynamically
1. #include <iostream.h>
2.
3. int main() {
4. int * x;
5. Create an integer
6. x = new int; dynamically
7.
8. *x = 8;
9. cout << “Address of x is “ << &x << endl;
10. cout << “Address stored in x is “ << x << endl;
11. cout << “Value stored at address “ << x << “ is “ << *x << endl;
12.
13. delete x;
14. return 0; delete from memory
15. }
7
Typical output: you will have different addresses!
Address of x is 0x0012FF7C
Address stored in x is 0x00300030
Value stored at address 0x00300030 is 8
Press any key to continue
8
Note
line 6: Allocation of memory
x = new int;
means grab enough memory for one integer from the heap and return its
address and store the address in x
line 13:
delete x;
free up the memory allocated for x for use by this and other programs.
9
Use of &.
Note
line 9: means address of
cout << “Address of x is “ << &x << endl;
means display the address of x
line 10:
cout << “Address stored in x is “ << x << endl;
displays what is stored in x. Since x is a pointer it too will be an address
Use of *.
Line 4: means x is a pointer
int * x;
Line 11: (*x) Dereferences pointer.
“at the address stored in” operator
cout << “Value stored at address “ << x << “ is “ << *x << endl;
Display the value “at the address stored in” x
10
What use is this?
We can grab any amount of memory from the heap
To grab enough memory for 100 integers all we need
is a pointer to hold one address (of an integer).
A request for memory for 100 ints using the new
operator.
new will grab contiguous space for 100 ints and
return the base address to the pointer
int *theData;
theData = new int[100];
This could be an expression. E.g. a variable. Suppose we had a data file
with an unknown number of records. A varaible nItems could be used to
count the number of records in the file. a block of memory could then
be allocated that can contain exaclty the records in the data files.
theData = new int[nItems]; 11
Another example
int *data;
cout << “Enter the number of items to be processed : ”;
cin >> nItems;
data = new int[nItems];
Note the use of square brackets. This looks like an array. Remember an array is a
contiguous block of memory. The array name holds the base address of that block
of memory.
An array name is a special kind of pointer!
The point is that once we allocate a block of memory dynamically we can treat the
pointer like an array.
See Demo 2
12
Dynamic structure Allocation
Suppose we have a declaration for a structure
struct myDataType {
char name[20];
char phoneNo[15];
float balance;
};
13
Accessing parts of a dynamically allocated structure
Normally to access fields of a structure we use the dot notation.
myDataType obj1;
myDataType *obj2;
obj2 = new myDataType;
15
Accessing parts of a dynamically allocated array of
structures
Warning: this is confusing!
However if we have an array of structures created dynamically we have to use
the normal syntax.
myDataType *obj2;
16
Passing a dynamically allocated atomic variable to a function
#include <iostream.h>
int main() {
int *number;
#include <iostream.h>
int main() {
int *number;
int nItems;
#include <iostream.h>
struct myDataType {
char name[20];
int grade;
};
int main() {
myDataType *newrecord;
DEMO5
newrecord = new myDataType; //created dynamically
ReadData(newrecord);
cout << newrecord->name << " " << newrecord->grade << endl;
delete record;
return 0;
}
19
Passing an Array of structs to a function
#include <iostream.h>
#include <stdlib.h>
struct myDataType {
char name[20];
int grade;
};
for (i=0;i<nItems;i++)
cout <<record[i].name << " " << record[i].grade << endl;
return 0;
}
21
A Couple of Ideas to think about
NULL pointers
typedef
22
NULL
You often see NULL used in programs.
It is a special constant pointer value (a zero address) that can be
assigned to any sort of pointer.
int *x = NULL;
23
typedef
You can define alternative names for existing types
can aide readability of code
is important for some dynamic data structures (linked lists)
E.g.
typedef int banana;
banana x; //variable x is a banana
//banana is an int
//same as int x;
OR More commonly to “clean up pointer declarations
typedef float * floatPtr;
floatPtr f1ptr,f2ptr; //f1prt and f2ptr are pointers to floats
24
Practical Application-Linked Lists
27
After declaration of pointers
front NULL
newNode NULL
28
Insertion to list
front NULL
data 10
newNode
next NULL NULL
29
Insertion to list
front = newNode;
front
data 0
newNode
next NULL NULL
30
Insertion to list
data 10
front
next NULL NULL
data 20
newNode
next NULL NULL
31
front->next = newNode; Insertion to list
newNode = NULL
data 10
front
next NULL
data 20
next NULL NULL
newNode NULL
32
NodePtr cursor; Deletion from list (note
cursor = front; requires another pointer)
data 10
front
next NULL
cursor
data 20
next NULL NULL
newNode NULL
33
front = front->next; //deleting first node
data 10
cursor
next NULL
data 20
front
next NULL NULL
newNode NULL
34
delete cursor;
cursor = NULL;
temp
NULL
data 20
front
next NULL NULL
newNode NULL
35
Demo 6 and 7
36
Linked lists
37