Lab 3: Dynamic Memory & Linked Lists
You will complete three problems on a worksheet, followed by an implementation of a singly-linked list. Please attend the section you are registered for to receive your lab checkoff.
For this week’s lab work, there are three files:
In the first part of the lab, you will review basic concepts of C memory management, completing the three worksheet problems to reinforce your understanding as you go along.
In the second half of the lab, we will introduce the implementation of a singly-linked list. Your job here is to commplete the code in lab3.c
, specifically:
Node *list_create(void *a_value)
: Create a list containing a node of pointer toa_value
.Node *list_push_to_front(Node *a_head, void *a_value)
: Add a new node with valuea_value
to the front of the list.Node *list_pop_last(Node *a_head)
: Detach and return the last node of the list.void list_free(Node *a_head)
: Deallocates the entire linked list.
Of these, the trickiest one is probably list_free
, in which you must deallocate an entire linked list. Don’t forget to also deallocate each list node’s value!
You can test your code with an invocation of our container and only two lines:
$ rv gcc -Wall -Wextra -Wpedantic -Wshadow -Wformat=2 -std=c23 -fsanitize=address,undefined -o test_lab3 lab3.c
$ rv qemu test_lab3