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

Need answered, not sure what i need to do Sosed on Problems 3.7 and 10.32 from P

ID: 3606638 • Letter: N

Question

Need answered, not sure what i need to do

Sosed on Problems 3.7 and 10.32 from Programming Language Pragmatics4 Edition by Michoel L Scott Sadly, after running for a while, Brad's program always runs out of memory and crashes. Explain what's going wrong As part of the development team at MumbleTech.com, Janet has written a list manipulation library for C that contains, among other things, the code below b) After Janet patiently explains the problem to him, Brad gives it another try: void * data etruct list nede next; *hile (more vidgete)) insert (next-adgetj), L); .4ode* T-reverse (L) ; list nodeist node malles(sizeof (list nede)) t->data = d; delete 1ist(L); L=T; return t; This seems to solve the insufficient memory problem, but where the program used to produce correct results (before running out of memory), now its output is strangely corrupted, and Brad goes back to Janet for advice., What will she tell him this time? list node0 while (L) c) It was determined that for future software implementation, aC version of this library would be more beneficial. Build a (more complete) C++ version of this singly linked list library. Discuss the issue of storage management. Under what circumstances should one delete the elements of a list when deleting the list itself? What should the destructor for 1ist data member? Should it recursively delete node next? Etn insert (L->data, t) ->next ; node do? Should it delete its return rtn: while (L) L-L->nexti free (t->data): ree (t); a) Accustomed to Java, new team member Brad includes the following code in the main loop of his program while more vidceta)) insert (nextwageLU , L) ; L-reverse (L) ;

Explanation / Answer

a)

Basically in Java, whatever memory locations are not currently being pointed by variables in the code, are removed by garbage collector explicitly. In this case the insert method builds a list completely.. so suppose the list is 1->2->3->4. Lets call this list L1. The variable L in Brad program points to this list.

Now when Brad calls the reverse function, then in reverse function, we are basically creating a whole new reversed list by explicitly createing the nodes by copying values to new nodes.. Thus it creates one ore list (Lets call L2) and returns the reference to that.

Now in Main program of Brad, L was earlier pointing to L1 and now L points to L2. If this would have been java, then L1 list would have been garbage collected by compiler because now in main program nobody holds the reference for L1. As in C++, there is no automatic garbage collector, we explicitly need to delete the original list once we got a reversed copy of that.

b)

In the delete function, we are just doing a free(t->data), which means the widget dynamic data will be deleted. Note: in insert function, we have created the memory for the node, not for the data which data pointer is pointing to. The pointer to data is provided to us by next_widget() function.

In Reverse function, when we create a new node, we pass the data function reference to new node. So basically L1 and L2, nodes of both lists are pointing to the same data reference. now if in delete_list(L) i delete the free(t->data) then that data is deleted and this change will be visible from the newly created reversed list also because that also refeences the same. Hence the output is random and corrupted.

c)

Ideally we should only delete, whatever memory we have got allocated for us in the class. If you notice, here we have just allocated the memory for each node, not for its data member. Hence we should only delete the node, not node's data.

the destructor for the list would be like below:

void list_destructos(list_node *L){

while(L) {

list_node*t = L;

L = L->next;

free(t);

}

}

Also, the reverse function should reverse the list in place, that means, it should not create any new node and copy the data, Rather the correct approach would be to play with the pointers of node and rearrange them in such a way so that list is reversed without using extra memory.

list_node* reverse(list_node* L) {

// if last node

if(!L->next) {

return L;

}

// if list is 1->2->3->4

// we remove node 1-> and call reverse on(2->3->4), we will get(4->3->2)

// remember, 1 is still pointing to node 2, so we can go to node 2 and attach 1 there.

list_node* thisNode = L;

// get the remaining list reversed

list_node* newHead = reverse(L->next);

thisNode->next->next = thisNode;

thisNode->next = 0;

return newHead;

}

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