Week 09 Tutorial Questions
-
Your tutor has asked a lab pair to present their week 8 work.
Discuss the good, the bad and the ugly aspects of their code.
Please be gentle in any criticism - we are all learning!
-
How are you progressing with the assignment?
Do you have any questions? Have you learned anything that would be useful to share with the rest of the tutorial?
-
What does malloc() do?
What are its inputs and output and what do they mean?
Describe a function that will allocate memory for a struct and assign a pointer to the result.
-
What does free() do?
What is the input to free and how does it help it do what it needs to do?
-
What is a use after free error?
Give an example.
Discuss why these are extremely dangerous, one of the worst causes of bugs in C programs and a major source of security vulnerabilities.
-
What is a memory leak?
What does dcc --leak-check do?
-
Implement a function list_append which appends its second argument to its first.
Treat both inputs as if they are lists and may have more than one node.
It should have this prototype:
struct node *list_append(struct node *list1, struct node *list2);
As usual, struct node has this definition:struct node { int data; struct node *next; };
It should not create (malloc) any new list elements.It should just change the appropriate next field in the first list.
-
This and following questions use this datatype from lectures:
struct node { int data; struct node *next; };
See list_empty.c and list.h for this weeks linked list tute questions, and test_list.c for autotests. Alternatively download all three files as tut_list_files.zip.
-
Implement a function copy which returns a copy of a linked list.
It should have this prototype.
struct node *copy(struct node *head);
It should call malloc to create a new linked list of the same length and which contains the same data. -
Implement a function identical that returns 1 if the contents of the two linked lists are identical (same length, same values in data fields) and otherwise returns 0.
It should have this prototype:
int identical(struct node *head1, struct node *head2);
-
Implement a function set_intersection which given two linked lists in strictly
increasing order returns a new linked list containing a copy of the elements found in both lists.
The new linked list should also be in strictly increasing order. It should include only elements found in both lists.
set_intersection should call malloc to create the nodes of the new linked list.
set_intersection should have this prototype:
struct node *set_intersection(struct node *set1, struct node *set2);