Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
3/27/2018
1
University of Illinois at Urbana-ChampaignDept. of Electrical and Computer Engineering
ECE 220: Computer Systems & Programming
Fully Dynamic Allocation
ECE 220: Computer Systems & Programming © 2018 Steven S. Lumetta. All rights reserved. slide 1
Summary of Dynamic Resizing Pros and Cons
dynamically resized array◦ start with small constant◦multiply size by a constant as necessary
pros◦ easy to implement◦array uses contiguous memory
cons (quantified for 2× multiplier)◦ copying cost (≤ 2N for N players)◦waste space (~38%)
© 2018 Steven S. Lumetta. All rights reserved. slide 2ECE 220: Computer Systems & Programming
Player Deletion with Dynamic Resizing
What about deletion?
If order doesn’t matter,◦ copy last element over deleted element,◦ then reduce count◦ (requires constant time).
If order matters, deletion can be expensive.
© 2018 Steven S. Lumetta. All rights reserved. slide 3ECE 220: Computer Systems & Programming
Allocating Individual Players Requires a Pointer for Each
Can we use dynamic allocation ◦ to allocate one thing (a player) at a time◦ instead of resizing an array?
Yes, but first, we need to solve a problem:◦Every call to malloc returns a pointer.◦These pointers have no predictable relationship to one another.
◦So we need to store a pointer to each player.
© 2018 Steven S. Lumetta. All rights reserved. slide 4ECE 220: Computer Systems & Programming
3/27/2018
2
Where Can All the Pointers Go?
With dynamic resizing, we used one player pointer in the global data area:
static player_t* player_list = NULL;
Where can we put more pointers?
We can use ◦a dynamically resized array of pointers.◦But … have we really solved the problem in that case? (An array of pointers does reducing copying and waste space.)
© 2018 Steven S. Lumetta. All rights reserved. slide 5ECE 220: Computer Systems & Programming
Where Can We Put More Pointers?
Can we do something else?
© 2018 Steven S. Lumetta. All rights reserved. slide 6ECE 220: Computer Systems & Programming
player
player_list
playerplayer
player
Solution: Add a Pointer to the Player Struct!
What if we add a player_t*to the player struct?
© 2018 Steven S. Lumetta. All rights reserved. slide 7ECE 220: Computer Systems & Programming
player
player_list
playerplayer
player
Mark the End of the List by Pointing to Nothing
What about the last player’s pointer?
Set it to NULL.
© 2018 Steven S. Lumetta. All rights reserved. slide 8ECE 220: Computer Systems & Programming
player
player_list
playerplayer
player
NULL
3/27/2018
3
Singly-Linked Lists are Common for Unordered Groups
The data structure shown◦ is called a singly-linked list◦ (or, frequently, just a linked list).◦Singly-linked lists are usually used when order is not important.
How do we insert into a linked list?◦Specifically, where should we insert a new element: at the start, or the end?
Insert at the start: it’s faster.
© 2018 Steven S. Lumetta. All rights reserved. slide 9ECE 220: Computer Systems & Programming
Inserting into a Singly-Linked List Requires Two Changes
Make two changes. In what order?
© 2018 Steven S. Lumetta. All rights reserved. slide 10ECE 220: Computer Systems & Programming
player
player_list
playerplayer
player
NULL
new_player
1
2
Correct Ordering of Changes is Important
First, change the next field of the new player.
Otherwise, the old list is lost!
new_player->next = player_list;
player_list = new_player;
That’s all.
© 2018 Steven S. Lumetta. All rights reserved. slide 11ECE 220: Computer Systems & Programming
How Can We Remove Player p from the List?
Need to change pointer(link) marked in blue.
© 2018 Steven S. Lumetta. All rights reserved. slide 12ECE 220: Computer Systems & Programming
player
player_list
playerplayer
player
NULL
p
3/27/2018
4
Singly-Linked List Deletion is Linear in Size of List
Deletion is slower:◦ to delete player p◦ from a list that starts at player_list,◦we must walk over the list to find p,◦ then change pointer to p to p->next.
In general,◦with N things in the list,◦we examine on average N/2.
© 2018 Steven S. Lumetta. All rights reserved. slide 13ECE 220: Computer Systems & Programming
Modify Player Structure to Use Dynamic Allocation
Before writing player_delete,◦ let’s modify our player structure◦ to use dynamic allocation ◦ for the name* field.
*We treated the password field as a normalstring before, but technically it should be
hashed or encrypted to a fixed-length string.
© 2018 Steven S. Lumetta. All rights reserved. slide 14ECE 220: Computer Systems & Programming
Review: Example Player Structure
struct player_t {char name[32];char password[20]; int32_t age;int32_t num_games;int32_t score_dist[16];struct game_t* game;
};
© 2018 Steven S. Lumetta. All rights reserved. slide 15ECE 220: Computer Systems & Programming
char* name;
namepoints to a
dynamically allocated block of memory.player_t* next;
next is used for the linked list.
Modify player_init to Dynamically Allocate the Name
Then, in player_init, we can write…p->name = malloc (strlen (n) + 1);if (NULL == p->name) { return 0; }strcpy (p->name, n);
orp->name = strdup (n);if (NULL == p->name) { return 0; }
(recall that n is the new player’s name).
© 2018 Steven S. Lumetta. All rights reserved. slide 16ECE 220: Computer Systems & Programming
3/27/2018
5
First, Remove Player to Be Deleted from the List
Questions for you:
To delete “Y,” what needs to change?
The next field of player “Z.”
© 2018 Steven S. Lumetta. All rights reserved. slide 17ECE 220: Computer Systems & Programming
player_list
player
name
“Z”
player
name
“Y”
player
name
“X”
NULL
Free All Dynamically Allocated Data for the Player
Questions for you:
What needs to be freed to delete “Y?”
Both the player structure and the name.
© 2018 Steven S. Lumetta. All rights reserved. slide 18ECE 220: Computer Systems & Programming
player_list
player
name
“Z”
player
name
“Y”
player
name
“X”
NULL
Do Not Use Dynamic Data After Freeing It
Questions for you:
In what order?
First the name, then the player structure.
© 2018 Steven S. Lumetta. All rights reserved. slide 19ECE 220: Computer Systems & Programming
player_list
player
name
“Z”
player
name
“Y”
player
name
“X”
NULL
1
2
Ready to Write a Function to Delete a Player
Now we can write player_delete.
The function signature is:int32_t player_delete (player_t* p);◦p points to the player structure to remove from the list and free
◦ function returns 1 on success, or 0 on failure
© 2018 Steven S. Lumetta. All rights reserved. slide 20ECE 220: Computer Systems & Programming
3/27/2018
6
Using a player_t**makes the code
simpler.
Use a player_t** to Find the Link to Change
player_t** find;for (find = &player_list;
p != *find;find = &(*find)->next) {if (NULL == *find) {
return 0;}
}
© 2018 Steven S. Lumetta. All rights reserved. slide 21ECE 220: Computer Systems & Programming
Point find first tothe pointer to thehead of the list.
Initialize find to Point to the Pointer to the Head
player_t** find;for (find = &player_list;
p != *find;find = &(*find)->next) {if (NULL == *find) {
return 0;}
}
© 2018 Steven S. Lumetta. All rights reserved. slide 22ECE 220: Computer Systems & Programming
Once p == *find,we have found
the link to change.
Advance Until find Points to Pointer to Player to Delete
player_t** find;for (find = &player_list;
p != *find;find = &(*find)->next) {if (NULL == *find) {
return 0;}
}
© 2018 Steven S. Lumetta. All rights reserved. slide 23ECE 220: Computer Systems & Programming
Advance by pointing find to thenext field of the structure to whichthe pointer find points to points.
Move find from next Field to next Field
player_t** find;for (find = &player_list;
p != *find;find = &(*find)->next) {if (NULL == *find) {
return 0;}
}
© 2018 Steven S. Lumetta. All rights reserved. slide 24ECE 220: Computer Systems & Programming
3/27/2018
7
If we reach the end of thelist, p is not in the list, so fail.
For Safety, Check for End of List in Loop Body
player_t** find;for (find = &player_list;
p != *find;find = &(*find)->next) {if (NULL == *find) {
return 0;}
}
© 2018 Steven S. Lumetta. All rights reserved. slide 25ECE 220: Computer Systems & Programming
Remember that find points to the pointer to be changed.
Free the name,then the player.
Return success.
Remove the Player, Free the Blocks, and Return Success
*find = p->next;
free (p->name);
free (p);
return 1;
}
© 2018 Steven S. Lumetta. All rights reserved. slide 26ECE 220: Computer Systems & Programming
Examine How player_delete Works in Detail
Let’s do a detailed example◦of player_delete execution◦ on a linked list of three players◦with variables shown in LC-3 memory.
Let’s first identify where each variable resides:◦ in the global data area,◦ in the heap, or◦ in the stack.
© 2018 Steven S. Lumetta. All rights reserved. slide 27ECE 220: Computer Systems & Programming
Dynamically Allocated Data Reside in the …
The linked list is shown below (head on left).
Where are these data(global data, heap, or stack)?
© 2018 Steven S. Lumetta. All rights reserved. slide 28ECE 220: Computer Systems & Programming
“Gao”x8772
player
name
next x9924
x8772x6770
player
name
next x0000
xABCDx8424
player
name
next x8424
x5783x9924
“Charlie”x5783 “Baddie”xABCDheap
3/27/2018
8
File-Scope Variables Reside in the …
The file-scope variableplayer_list points tothe head of the list.
Where is player_list stored?
© 2018 Steven S. Lumetta. All rights reserved. slide 29ECE 220: Computer Systems & Programming
“Gao”x8772
player
name
next x9924
x8772x6770
player
name
next x0000
xABCDx8424
player
name
next x8424
x5783x9924
“Charlie”x5783 “Baddie”xABCDheap
player_listx6770x3998
global data
Local Variables Reside in the …
And where is local variable find?
© 2018 Steven S. Lumetta. All rights reserved. slide 30ECE 220: Computer Systems & Programming
“Gao”x8772
player
name
next x9924
x8772x6770
player
name
next x0000
xABCDx8424
player
name
next x8424
x5783x9924
“Charlie”x5783 “Baddie”xABCDheap
player_listx6770x3998
global data
findbitsxE743
stack
Parameter p is Close to Local Variable find
© 2018 Steven S. Lumetta. All rights reserved. slide 31ECE 220: Computer Systems & Programming
“Gao”x8772
player
name
next x9924
x8772x6770
player
name
next x0000
xABCDx8424
player
name
next x8424
x5783x9924
“Charlie”x5783 “Baddie”xABCDheap
player_listx6770x3998
global data
findbitsxE743
stack
px8424xE748
Start the Function by Initializing find
Here’s the loop again.for (find = &player_list;
p != *find;find = &(*find)->next) {if (NULL == *find) {
return 0;}
}
© 2018 Steven S. Lumetta. All rights reserved. slide 32ECE 220: Computer Systems & Programming
Start by initializing find.
3/27/2018
9
Initialize find to &player_list
© 2018 Steven S. Lumetta. All rights reserved. slide 33ECE 220: Computer Systems & Programming
“Gao”x8772
player
name
next x9924
x8772x6770
player
name
next x0000
xABCDx8424
player
name
next x8424
x5783x9924
“Charlie”x5783 “Baddie”xABCDheap
player_listx6770x3998
global data
findbitsxE743
stack
px8424xE748
x3998
Continue Executing the Loop
What happens next?for (find = &player_list;
p != *find;find = &(*find)->next) {if (NULL == *find) {
return 0;}
}
© 2018 Steven S. Lumetta. All rights reserved. slide 34ECE 220: Computer Systems & Programming
Execute the loop test.
Is *find Equal to p?
© 2018 Steven S. Lumetta. All rights reserved. slide 35ECE 220: Computer Systems & Programming
“Gao”x8772
player
name
next x9924
x8772x6770
player
name
next x0000
xABCDx8424
player
name
next x8424
x5783x9924
“Charlie”x5783 “Baddie”xABCDheap
player_listx6770x3998
global data
findx3998xE743
stack
px8424xE748
*find is x6770
Continue Executing the Loop
What happens next?for (find = &player_list;
p != *find;find = &(*find)->next) {if (NULL == *find) {
return 0;}
}
© 2018 Steven S. Lumetta. All rights reserved. slide 36ECE 220: Computer Systems & Programming
Execute the loop body.
3/27/2018
10
Is *find Equal to NULL?
© 2018 Steven S. Lumetta. All rights reserved. slide 37ECE 220: Computer Systems & Programming
“Gao”x8772
player
name
next x9924
x8772x6770
player
name
next x0000
xABCDx8424
player
name
next x8424
x5783x9924
“Charlie”x5783 “Baddie”xABCDheap
player_listx6770x3998
global data
findx3998xE743
stack
px8424xE748
*find is x6770
Continue Executing the Loop
What happens next?for (find = &player_list;
p != *find;find = &(*find)->next) {if (NULL == *find) {
return 0;}
}
© 2018 Steven S. Lumetta. All rights reserved. slide 38ECE 220: Computer Systems & Programming
Execute the loop update.
Where is (*find)->next?
© 2018 Steven S. Lumetta. All rights reserved. slide 39ECE 220: Computer Systems & Programming
“Gao”x8772
player
name
next x9924
x8772x6770
player
name
next x0000
xABCDx8424
player
name
next x8424
x5783x9924
“Charlie”x5783 “Baddie”xABCDheap
player_listx6770x3998
global data
findx3998xE743
stack
px8424xE748
*find is x6770
Compiler Can Calculate Offsets for Each Field
struct player_t {char* name;char password[20]; int32_t age;int32_t num_games;int32_t score_dist[16];struct game_t* game;player_t* next;
};
© 2018 Steven S. Lumetta. All rights reserved. slide 40ECE 220: Computer Systems & Programming
+x00+x01+x15+x17+x19+x39+x3A
3/27/2018
11
Set find to &(*find)->next
© 2018 Steven S. Lumetta. All rights reserved. slide 41ECE 220: Computer Systems & Programming
“Gao”x8772
player
name
next x9924
x8772x6770
player
name
next x0000
xABCDx8424
player
name
next x8424
x5783x9924
“Charlie”x5783 “Baddie”xABCDheap
player_listx6770x3998
global data
findx3998xE743
stack
px8424xE748
x67AA
x67AA
Continue Executing the Loop
And then …for (find = &player_list;
p != *find;find = &(*find)->next) {if (NULL == *find) {
return 0;}
}
© 2018 Steven S. Lumetta. All rights reserved. slide 42ECE 220: Computer Systems & Programming
Back to the loop test.
Is *find Equal to p? What About NULL?
© 2018 Steven S. Lumetta. All rights reserved. slide 43ECE 220: Computer Systems & Programming
“Gao”x8772
player
name
next x9924
x8772x6770
player
name
next x0000
xABCDx8424
player
name
next x8424
x5783x9924
“Charlie”x5783 “Baddie”xABCDheap
player_listx6770x3998
global data
findx67AAxE743
stack
px8424xE748
x67AA
*find is x9924
Continue Executing the Loop
After the loop test and the loop body…for (find = &player_list;
p != *find;find = &(*find)->next) {if (NULL == *find) {
return 0;}
}
© 2018 Steven S. Lumetta. All rights reserved. slide 44ECE 220: Computer Systems & Programming
Execute the loop update.
3/27/2018
12
Where is (*find)->next?
© 2018 Steven S. Lumetta. All rights reserved. slide 45ECE 220: Computer Systems & Programming
“Gao”x8772
player
name
next x9924
x8772x6770
player
name
next x0000
xABCDx8424
player
name
next x8424
x5783x9924
“Charlie”x5783 “Baddie”xABCDheap
player_listx6770x3998
global data
findx67AAxE743
stack
px8424xE748
x67AA
*find is x9924
x995E
Set find to &(*find)->next
© 2018 Steven S. Lumetta. All rights reserved. slide 46ECE 220: Computer Systems & Programming
“Gao”x8772
player
name
next x9924
x8772x6770
player
name
next x0000
xABCDx8424
player
name
next x8424
x5783x9924
“Charlie”x5783 “Baddie”xABCDheap
player_listx6770x3998
global data
findx67AAxE743
stack
px8424xE748
x67AA x995E
x995E
Continue Executing the Loop
And then …for (find = &player_list;
p != *find;find = &(*find)->next) {if (NULL == *find) {
return 0;}
}
© 2018 Steven S. Lumetta. All rights reserved. slide 47ECE 220: Computer Systems & Programming
Back to the loop test.
Is *find Equal to p? Yes! Loop Test Fails…
© 2018 Steven S. Lumetta. All rights reserved. slide 48ECE 220: Computer Systems & Programming
“Gao”x8772
player
name
next x9924
x8772x6770
player
name
next x0000
xABCDx8424
player
name
next x8424
x5783x9924
“Charlie”x5783 “Baddie”xABCDheap
player_listx6770x3998
global data
findx995ExE743
stack
px8424xE748
x67AA x995E
*find is x8424
3/27/2018
13
Overwrite *find with p->next
Here’s the code after the loop.
*find = p->next;
free (p->name);
free (p);
return 1;
}
Notice that we overwrite *find.
© 2018 Steven S. Lumetta. All rights reserved. slide 49ECE 220: Computer Systems & Programming
Set the Bits at *find to p->next
© 2018 Steven S. Lumetta. All rights reserved. slide 50ECE 220: Computer Systems & Programming
“Gao”x8772
player
name
next x9924
x8772x6770
player
name
next x0000
xABCDx8424
player
name
next x8424
x5783x9924
“Charlie”x5783 “Baddie”xABCDheap
player_listx6770x3998
global data
findx995ExE743
stack
px8424xE748
x67AA x995Ex0000
“Baddie” is No Longer in the List!
© 2018 Steven S. Lumetta. All rights reserved. slide 51ECE 220: Computer Systems & Programming
“Gao”x8772
player
name
next x9924
x8772x6770
player
name
next x0000
xABCDx8424
player
name
next x0000
x5783x9924
“Charlie”x5783 “Baddie”xABCDheap
player_listx6770x3998
global data
findx995ExE743
stack
px8424xE748
x67AA x995E
Finish the Rest of the Function
What’s next?
*find = p->next;
free (p->name);
free (p);
return 1;
}
© 2018 Steven S. Lumetta. All rights reserved. slide 52ECE 220: Computer Systems & Programming
Free the name, then player p.
3/27/2018
14
Free the Two Blocks of Dynamically Allocated Data
© 2018 Steven S. Lumetta. All rights reserved. slide 53ECE 220: Computer Systems & Programming
“Gao”x8772
player
name
next x9924
x8772x6770
player
name
next x0000
xABCDx8424
player
name
next x0000
x5783x9924
“Charlie”x5783 “Baddie”xABCDheap
player_listx6770x3998
global data
findx995ExE743
stack
px8424xE748
x67AA x995E
1
2
The Function is Done
What’s next?
*find = p->next;
free (p->name);
free (p);
return 1;
}
© 2018 Steven S. Lumetta. All rights reserved. slide 54ECE 220: Computer Systems & Programming
Return success!
The List After the Function has Returned
© 2018 Steven S. Lumetta. All rights reserved. slide 55ECE 220: Computer Systems & Programming
“Gao”x8772
player
name
next x9924
x8772x6770
player
name
next x0000
x5783x9924
“Charlie”x5783heap
player_listx6770x3998
global data
stack
x67AA x995E