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 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started