Question: Consider the following data structure created by a C or C++ program using dynamic memory management. There are two linked lists that point to data

Consider the following data structure created by a C or C++ program using dynamic memory management. There are two linked lists that point to data nodes and the same data nodes can be in both lists. If the program must delete this entire data structure, there is a problem. If the program traverses both lists and deletes the data nodes pointed to by both lists, then some data nodes get deleted more than once. We know that this is a problem for some dynamic memory managers. However, if the program traverses only one of the lists, not all data nodes get deleted. This results in what is often called a memory leak. a) From the point of view of secure coding, which is worse, traversing both lists and possibly deleting memory twice, or having a memory leak? Justify your answer in a paragraph. b) Can you think of a way to implement this data structure such that neither problem will occur? Describe how you would do this in enough detail so that it is clear how to delete the entire data structure with neither problem occurring. Hint: you might have to add something to the data structure.

Consider the following data structure created by a C or C++ program

Consider the following data structure created by a C or C++ program using dynamic memory management. There are two linked lists that point to data nodes and the same data nodes can be in both lists If the program must delete this entire data structure, there is a problem. If the program traverses both lists and deletes the data nodes pointed to by both lists, then some data nodes get deleted more than once. We know that this is a problem for some dynamic memory managers. However, if the program traverses only one of the lists, not all data nodes get deleted. This results in what is often called a memory leak a) From the point of view of secure coding, which is worse, traversing both lists and possibly deleting memory twice, or having a memory leak? Justify your answer in a paragraph. b) Can you think of a way to implement this data structure such that neither problem will occur? Describe how you would do this in enough detail so that it is clear how to delete the entire data structure with neither problem occurring. Hint: you might have to add something to the data structure

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!