0

Here is the ListNode struct that is being used throughout this question

struct ListNode {
  int val;
  ListNode *next;
  ListNode() : val(0), next(nullptr) {}
  ListNode(int x) : val(x), next(nullptr) {}
  ListNode(int x, ListNode *next) : val(x), next(next) {}
};

In each iteration of a loop I have to create a new linked list node.

If I do this in the body of the loop it works:

// t is the linked list: I'm adding a node with a val of currentVal, 
// and a next entry of nullptr (you can see the definition of ListNode above)

t->next = new ListNode(currentVal); 

But I'm thinking about the whole "avoid using new" issue (for the sake of avoiding memory leaks). Because of that, I have in mind doing this instead:

// t is the linked list: I'm trying to add a node with a val of currentVal, 
// and a next entry of nullptr

ListNode listNode(currentVal);
t->next = &listNode;

The problem with the second method is that, starting with the second time through the loop and beyond, the ListNode struct to grabs the previous currentVal address and uses it for the next value. In other words, even though I intend to still use this constructor:

ListNode(int x) : val(x), next(nullptr) {}

It seems like this constructor is being used:

ListNode(int x, ListNode *next) : val(x), next(next) {}

...where the next value is the address of the previously created listNode.

Is there any kind of clear or delete functionality that I can run on listNode at the end of each loop to prevent this? Is there no way around using new in this context?

tarstevs
  • 23
  • 1
  • 5
  • What is *"avoid using new" issue*? You'd better start with a good introductory [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) that explains basics of C++ such as what local variables and their lifetime are. – Evg Mar 20 '22 at 05:44
  • @evg I'm not sure what you mean when you ask about avoiding using new. I'm assuming you know the answer to your question "What is 'avoid using new' issue". Is that a fair assumption? Or are you asking me to share what I have in mind about the reasons for avoiding using new? – tarstevs Mar 20 '22 at 05:46
  • 1
    Unless you are working with a fixed memory pool, you generally can't avoid using `new` when adding nodes to a linked list dynamically. In your "avoid" example, your `listNode` is a local object that gets destroyed on each loop iteration, leaving `t->next` dangling. Using proper memory management techniques when using `new` will avoid memory leaks. – Remy Lebeau Mar 20 '22 at 05:46
  • @RemyLebeau Okay, that's what I figured. Just wanted to see if there was a way to do it without introduced new and the concomitant memory management tasks. – tarstevs Mar 20 '22 at 05:48
  • By asking this question I'm trying to give you a hint that your understanding of this rule is most likely incorrect. Avoiding `new` doesn't mean you can simply replace `new` by taking an address of a local variable. The key thing to study here is an object lifetime. Local objects and `new`ed ones are *very* different in this respect. – Evg Mar 20 '22 at 05:48
  • If you want to avoid `new` in linked lists, you could use smart pointers. But be careful: destruction of a linked list built using smart pointers is recursive and could result in stack overflow if a list is long enough. – Evg Mar 20 '22 at 05:55
  • Does it make sense that this question was closed? Would it not be useful to have an answer to this posted here? The linked question https://stackoverflow.com/questions/4643713/c-returning-reference-to-local-variable is an entirely different context. Right? I'm sure a good answer here would help some people. In any event, I already have what I needed from Remy's response... I guess I just wish someone like Remy could write up a good answer for people to look through. Not sure who closed it so quickly, seems like a serious question to me... but we're good. I wont delete it just in case. – tarstevs Mar 20 '22 at 05:55
  • 1
    I guess the dupe already answers it. The context is different, but the problem is essentially the same. There might be better dupes, I'll try to find them. Yes, please don't delete it. Closed questions could be as useful as answered ones. – Evg Mar 20 '22 at 05:57
  • The speed with which questions are closed as dupes is typically proportional to the frequency such questions appear at SO. ;) I added a few more dupes, which should be more relevant. – Evg Mar 20 '22 at 06:08

0 Answers0