March 8, 2019
For today:
1) Read HFC Chapter 6
2) Finish Homework 5
3) Prepare for a quiz (Here's the quiz from last year as practice).
Today:
1) Quiz
2) HFC Chapter 6
For next time:
1) Read the Little Book of Semaphores, Chapters 1 and 2, and download Sync (see below)
2) Start Homework 6
3) Turn in a project update (see the assignment on Canvas)
4) Make a concerted effort to get the Power Chords to record a cover of
Explicit memory management (as opposed to garbage collection) might be the biggest complicating factor in API design.
Some patterns and anti-patterns:
1) Copy everything! Make and free compound structures all at once. Avoid sharing/aliasing.
2) Make immutable objects. If you are going to share, only share immutable things.
3) Build mega-structures with shared elements, and free the whole thing between phases, or never.
1) Think of it as a draft of the final report. I think it's not additional work; rather, scaffolding for spreading the work.
2) When you pause to tell the story of your project, it influences your next steps, and helps plan your end game.
Of all the things you could do next, which are most valuable for your learning goals and for getting to a project that constitutes a solid, complete body of work?
Downloading Sync
git clone https://github.com/AllenDowney/LittleBookOfSemaphores
cd LittleBookOfSemaphores/code
python Sync.py sync_code/mutex.py
The chapter starts out building a linked list with statically allocated structures.
Then it shows the "right" way, using dynamic allocation and functions that initialize and free list nodes.
The linked list exercise for this chapter is tricky because we use a non-obvious protocol for passing around lists.
In general, there are three ways to work with lists:
1) Pass around a pointer to the first Node.
2) Pass around a List structure that contains a pointer to the first Node.
3) Pass around a pointer to a pointer to the first Node.
4) Use a sentinel Node
The exercise uses option (3). If we draw some diagrams, it will be easier to understand.
Here's the structure definition
typedef struct node {
int val;
struct node *next;
} Node;
Why did we have to give the structure a name?
Here's the constructor:
Node *make_node(int val, Node *next) {
Node *node = malloc(sizeof(Node));
node->val = val;
node->next = next;
return node;
}
And here's a function that takes a list as a parameter:
void print_list(Node **list) {
Node *current = *list;
printf("[ ");
while (current != NULL) {
printf("%d ", current->val);
current = current->next;
}
printf("]\n");
}
So let's make a state diagram for this code:
Node *head = make_node(1, NULL);
head->next = make_node(2, NULL);
head->next->next = make_node(3, NULL);
head->next->next->next = make_node(4, NULL);
Node **list = &head;
print_list(list);
If you finish quickly, show how the state diagram would be updated after performing a push (from exercise06.md). If you get done with that, try remove_by_value.