1
void add_first(int e) {
        Node* newest = new Node(e, first);
        first = newest;
        n++;
}

On this code i declared a node pointer "newest".

list->add_first(4);

Let's say i call that function for the second time.

list->add_first(8)

Is it still that same *newest from the first time i called the function pointing at a new node OR did the previous *newest die after the function and on the second function call a new *newest was generated?

//Full code in case you need extra information

template <typename T>
class List {
    struct Node {
        T elem;
        Node* next;

        Node(T elem = 0, Node* next = nullptr) : elem(elem), next(next) {}
    };

    Node* first;
    size_t n;

public:
    List () : n(0), first(nullptr) {}
    ~List () {
        while (first != nullptr) {
            Node* aux = first;
            first = first->next;
            delete aux;
        }
    }

    bool addFirst(T e) {
        Node* newest = new Node(e, first);
        first = newest;
        n++;
        return true;
    }
    bool add(T e, size_t p) {
        if (p > n) return false;
        if (p == 0) return addFirst(e);

        Node* newest = new Node(e);

        Node* aux = first;
        size_t i = 1;
        while (i++ < p) {
            aux = aux->next;
        }
        newest->next = aux->next;
        aux->next = newest;
        n++;
        return true;
    }
    bool addLast(T e) {
        return add(e, n);
    }
};
Bata
  • 183
  • 2
  • 6
  • 17
  • 1
    In your example the pointers do die, but the memory address they store remain allocated. – plasmacel May 13 '18 at 18:31
  • 3
    You should probably pick up [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) - everything will be explained there – UnholySheep May 13 '18 at 18:32
  • So the second time i call the function it's actually a new *newest? ok thanks – Bata May 13 '18 at 18:32

3 Answers3

4
new Node(e, first)

will allocate new memory on the heap for an object of type Node, where it will remain until you manually deallocate it using delete. If you don't retain a reference to it, then you can't deallocate it and you have a memory leak.

Node* newest = ...;

will create a pointer of type Node on the stack until it goes out of scope (in this case, when the function returns), initializing it to reference the object you just created.

It's important to note that newest is not the object; it is merely referencing it for the time being.

first = newest;

The type of first is not provided here, but presumably in order for it to compile it is defined at a higher scope, e.g. a member variable, and accepts an assignment of type Node*...so it's probably a Node* itself.

In which case this will by default set first to reference the same object pointed to by newest. It is not pointing to newest in this scenario, but both are now referencing the same object.

When the function returns, newest(the pointer) will be destroyed because it is defined within the scope of the function invocation, but the Node object you created will live on the heap, as well as first which references it because it's defined outside this scope.

If you immediately call this function again, e.g. in a loop, you will allocate another object of type Node, set first to that object, and the object previously pointed to by first is now orphaned on the heap without a reference.

EDIT: I did just notice that you pass first into the constructor for Node, so it's possible you're making a separate connection therein.

Swavity
  • 49
  • 7
  • So if: "*first" points "*newest" and "*newest" points a node. After the function ends "*newest" gets deleted and "*first" still points the node? – Bata May 13 '18 at 20:54
  • Correct, except `first`is never pointing to `newest`. You're executing an assignment on `first` to another pointer, which by default will set `first`'s address reference equal to that of `newest`'s address reference; effectively, you're telling `first` to point to the same object at which `newest` is pointing. You could actually leave `newest` out entirely with the same result by doing: `first = new Node(e, first);` – Swavity May 13 '18 at 21:08
2

newest (i.e. the pointer) dies when the function returns (because it is a local variable). The next time the function is called, a new newest variable is created and destroyed again.

*newest (i.e. the object it's pointing to) remains alive until you explicitly delete it (because you created it with new)

melpomene
  • 84,125
  • 8
  • 85
  • 148
2

Pointers are just types which hold a memory address of an instance of a specific type. In your example you dynamically allocate memory to store an instance of Node using new and you store its address to a pointer (notice that new returns with a pointer which points to the allocated memory). When the function returns, the pointer "dies", but the allocated memory remains allocated.

plasmacel
  • 8,183
  • 7
  • 53
  • 101