Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 7. In all this exercise, we consider the following structure type cell

ID: 3808334 • Letter: E

Question

Exercise 7. In all this exercise, we consider the following structure type cell whose purpose is to represent linked lists of character strings: struct cell char *key; struct cell *next; $7a. A bug is hidden in the code below of the function cons on a linked list of strings whose intended purpose is to append a string of characters string to a list list. Can you detect the bug, explain what bad effect could happen at run time, and sug- gest how to correct it? struct cell cons (char string, struct cell list.) struct cell *newlist; new list (struct cel malloc (sizeof (struct cell new list >key strdup (string) new list next ist return newlist; $7b. A bug is hidden in the code below for the function free list whose intended purpose is to deallocate a linked list of strings. Can you find the bug and explain how to correct it? void free ist (struct cell ist) struct cell ptr while (list NULL) ptr list list list next free (ptr); return $7c. Worse, two bugs are hidden in the code below of the function clone list whose intended purpose is to clone a linked list of character strings, that is, produce two equal but absolutely independent lists list and clone list (list) in the end. Can you find the bugs and explain how to correct them?

Explanation / Answer

7a)

here the bug is, while creating a cell, using malloc , in malloc it is given that(sizeof(struct cell *)), so, the mallac will not create a block of size, struct cell, it creates a block of a size of pointer variable..

this wil lead, we unable to initialize char *, variable

7b)

here bug is,

prt referencing to list variable, whenever the list variables moves to next, ptr also moves

so if you free(ptr), then list also frees, but not everything in the list is freed, only second node, first node remains, but all other nodes are deferenced, beacause second node deleted

7c)

first bug is in line 2,3,

that is here in line 2 we are creating a block of size struct cell for newlist variable

but line 3, we are pointing newlist to head, the block that is created in line 2, will remains as a deferenced pointer, which can't be accessed , dangling pointer

second bug is:

in while loop, every time when new node is created , we are moving newlist to newlist->next,

then the pointer head also moves along with newlist,

hence head no longer points to head of the list..

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote