0

Good day,

I have a sample LinkedList, which is a very basic class for me to learn C++ with. At the moment i'm trying to add new nodes to my linked list using a class etc, and I encountered a very odd bug.

Here's my LinkedList.h:

struct Node {

    Node* next;
    int value;
};

class LinkedList {
private:
    Node* root;
public:
    LinkedList();
    void print();
    void add(int val);
    ~LinkedList();
};

And here's my LinkedList.cpp

#include "LinkedList.h"
#include <iostream>

using namespace std;

LinkedList::LinkedList() {
    root = NULL;
}

LinkedList::~LinkedList() {

}

void LinkedList::add(int val) {
   Node node;
   node.next = NULL;
   node.value = val;
   if (root != NULL)
      node.next = root;
   root = &node;
   cout << root->value << endl; // TEST PRINT 1
}

void LinkedList::print() {
    cout << root->value <<endl; // TEST PRINT 2
}

This is my main.cpp:

#include "LinkedList.h"
#include <iostream>

using namespace std;

int main() {

    LinkedList list;

    list.add(5);
    list.print();


}

This code should simply create a new node with the value '5' and make it the root. When I add a new number a different node should be created and this new node should be the root with the old root as its 'next' node.

I have two debug messages, located near 'TEST PRINT 1' and 'TEST PRINT 2'. Both lines are exactly the same, yet the first print gives me the correct value (which is 5) while the second print gives me a very weird negative number (-858993460).

What have I done wrong?

Dubb
  • 423
  • 1
  • 7
  • 21
  • `node` inside `LinkedList::add` disappears after the function ends. `root` then points to a not-existing value. Look up dangling references. – nwp May 18 '17 at 18:45
  • 1
    Possible duplicate of [Can a local variable's memory be accessed outside its scope?](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – Borgleader May 18 '17 at 18:48

1 Answers1

4

The problem is here:

root = &node;

Here you make root point to the local variable node. The local variable which will go out of scope and be destructed once the function returns.

You need to allocate nodes dynamically.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • That makes alot of sense actually. How would achieve this? – Dubb May 18 '17 at 18:46
  • @Dubb This is one of the few cases where you want to use `new`, raw and unadorned. `Node * node = new node;` Normally you would use a container (But you are building a container) or a `std::unique_ptr` – user4581301 May 18 '17 at 18:47
  • 2
    @Dubb dynamic allocation is done by operator `new`. It should be explained in any C++ textbook – Slava May 18 '17 at 18:48
  • 2
    @Dubb `Node* node = new Node` would be a good start. Then you of course needs to `delete` the nodes you allocate. And also read about [the rule of three](http://en.cppreference.com/w/cpp/language/rule_of_three). – Some programmer dude May 18 '17 at 18:48
  • @Someprogrammerdude Yea, sorry found it. Much appreciated! – Dubb May 18 '17 at 18:56
  • @Slava Yup found it! Thanks for ur help! – Dubb May 18 '17 at 18:57