0

I've been trying to create a linked list in C++. I am trying to avoid using the keyword new when creating it, but it doesn't seem to work

// Linked lists
struct Node 
{
    int value;
    Node *next;

    Node( int val ) : value( val ), next(nullptr) {};
};


int main() 
{
    vector<int> vec = { 2,5,7,1,4,7 };

    //insertian
    Node head(0); // we need to use a pointer
    Node* ptr = &head;

    for ( auto v : vec)
    {
        Node* temp = new Node( v ); // Node temp( v );
        ptr->next = temp; //ptr->next = &temp;
        ptr = ptr->next; 
    }
}

The code above works fine. But if I replace the code inside the loop with the commented lines, then it fails. I'm not sure why.

I was also informed that you need to perform a delete when using new. If using new cant be avoided then how can the delete be performed?

user2384330
  • 77
  • 1
  • 7
  • 1
    `Node temp( v )` is destroyed after each iteration. Accessing the destroyed object is UB. – Ch3steR Jan 04 '22 at 12:21
  • 1
    Does this answer your question? [What is the scope of a 'while' and 'for' loop?](https://stackoverflow.com/questions/7880658/what-is-the-scope-of-a-while-and-for-loop) – Ch3steR Jan 04 '22 at 12:21
  • 1
    The use of ```new``` can be avoided with smart pointers. For example unique pointer can be used like ```#include ``` ```struct Node{std::unique_ptr next; ...};``` ```ptr->next = std::make_unique(v);```. No need to call delete as they will deallocate the memory they point to when the unique_ptr gets destroyed automatically. – Oli L Jan 04 '22 at 12:28
  • 1
    @OliL One potential problem with this solution is that list destruction is recursive and if a list is long enough, destruction might result in stack overflow. – Evg Jan 04 '22 at 12:30
  • So unless I use smart pointers is there a way to free that memory before the program ends if I want to when using new? – user2384330 Jan 04 '22 at 12:30
  • 2
    Sure. That's what `~Node()` is for. – Evg Jan 04 '22 at 12:31
  • In production code, using smart pointers has the chance of blowing the stack due to unbounded recursive destruction in `~Node`. Better to have a loop that destructs each node. It will make Herb Sutter happy. – Eljay Jan 04 '22 at 15:18

1 Answers1

1

Saying Node temp( v ) will create a local variable scoped to the for loop. On each iteration the local variable will be created, and at the end of the iteration destroyed.

You're storing a pointer to the local variable, which is undefined behaviour. What is probably happening is that the local variable is being created over the top of the old one on each iteration (also this is implementation dependent).

As you want the node to outlive the lifetime of the iteration you will need to allocate the node on the heap via new

Sean
  • 60,939
  • 11
  • 97
  • 136
  • So unless I use smart pointers is there a way to free that memory before the program ends if I want to when using new? – user2384330 Jan 04 '22 at 12:30
  • @user2384330 - Smart pointers are your best bet. They'll handle the releasing of the memory for you. – Sean Jan 04 '22 at 12:40
  • ***So unless I use smart pointers is there a way to free that memory before the program ends if I want to when using new?*** Before smart pointers existed you had to iterate through the list freeing each node using delete. – drescherjm Jan 04 '22 at 14:35