1. Consider the following (incorrect) function which removes the head node in a
ID: 3731148 • Letter: 1
Question
1. Consider the following (incorrect) function which removes the head node in a globally defined list called my_list. The struct grade_node is used to represent a node containing grade information in a linked list of grades.
struct grade_node {
int value;
char assignment[255];
struct grade_node* next;
};
struct grade_node* my_list = ... //assume this has been populated elsewhere.
void remove_head() {
if (my_list != NULL)
my_list = my_list.next;
}
a) What is the syntax error in remove_head?
b) What is the memory issue in remove_head?
a) What is the syntax error in remove_node?
b) What is the memory issue in remove_node?
2. Consider two possible functions for adding together two xyz points:
struct point add_points(struct point p1, struct point p2)
struct point* add_points(struct point* p1, struct point* p2)
Which of these functions should we expect to operate more efficiently and why?
Explanation / Answer
Solution:
1)
a)
I can't see any error in this since the head pointer which is my_list here is pointed to the next node if exists.Althogh there is an issue with the memory deallocation, the code will leave memory location of grade_node type as dangling.
Also, we are not checking if my_list.next!= NULL
b)
The memory issue is that it is not freeing the memory which is being dereferenced in the code.
2)
struct point* add_points(struct point* p1, struct point* p2)
the function will perform more efficiently because this function is working on call by reference.
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.