If I change the ObtainRequests function and create an object that points to the newly created reqNode after I've modified it, and use that in line 28 of the function, would that bypass the issue? |
That's what I did in the code I mentioned
|
requests.InsertBack(&newnode);
|
Here
&newnode
creates a pointer (i.e. an object) that points to newnode.
What you could do is create the reqNode with
malloc (or
new). Then it will no longer get destroyed automatically. Instead it will be destroyed when you use
free (or
delete if you used
new to create the object). In other words, every time you remove an element from the linked list you would have to remember to use
free (or
delete) otherwise you'll have a "memory leak". This is something you should do from outside the linked list class template because it is the user of the linked list that decided he wanted to store pointers to "dynamically allocated objects".
Smart pointers such as std::unique_ptr can help with this but it doesn't work well with
malloc, and if your professor wants you to use
malloc I doubt he wants you to use smart pointers. Note that
malloc is quite rare to see in modern C++ code. It's more of a C thing.
malloc and
free do not call constructors and destructors like
new and
delete do.