0

I was trying to implement the linked list stl but I always get runtime error if someone can help me solve this problem please. What I was doing is when inserting an element I check if it's the first element if it is I make the head and tail point to it if not I use the node's next pointer to point to the new element and move the tail so it point to the last inserted element the error appears in the insert function.

#include <iostream>
 
using namespace std;
 
template<typename T>
struct Node {
    T item;
    Node *next = nullptr;
};
 
template<typename T>
class list {
    static int sz;
    Node<T> *head, *tail;
public:
    void insert(T x) {
        Node<T> new_node;
        new_node.item = x;
        if (!sz) {
            *head = new_node;
            *tail = new_node;
        } else {
            tail->next = &new_node;
            *tail = new_node;
        }
        sz++;
    }
 
    int size() {
        return sz;
    }
 
    friend ostream &operator<<(ostream &os, list<T> &o) {
        while (o.head->next != nullptr) {
            os << o.head->item << " ";
            o.head = o.head->next;
        }
        return os;
    }
};
 
template<typename T>
int list<T>::sz = 0;
 
int main() {
    list<int> l;
    l.insert(5);
    l.insert(6);
    l.insert(7);
    cout << l.size() << endl;
    cout << l << endl;
    return 0;
}
  • 3
    `static int sz;` makes it impossible to have more than one list. There's no `list` constructor so `head` and `tail` are uninitialized. `*head = new_node; *tail = new_node;` tries to copy the contents of `new_node` into memory that has not been allocated. `tail->next = &new_node;` is a dangling pointer since `new_node` is a local variable and it is destroyed at the end of that function. – Retired Ninja Mar 03 '22 at 18:55
  • 3
    Add to that your ostream operator leaks the entire list into the ether. Basically, most of this thing needs work and/or a rewrite. I also wouldn't expect to last long needlessly pulling the entire `std` namespace *and* naming your template class the same as a standard container. – WhozCraig Mar 03 '22 at 18:59
  • @WhozCraig It prints once! Good enough, ship it! – Retired Ninja Mar 03 '22 at 19:11
  • I didn't catch the thing about ostream operator and the std namespace – Mahammad Ayman Mar 03 '22 at 19:16
  • He means that `o.head = o.head->next;` trashes the linked list by modifying `head`. At the end of the loop `head` points to the list terminator, so as far as the list's concerned, the list is empty. – user4581301 Mar 03 '22 at 19:24
  • Well.. sort of empty. `tail` is left dangling in the wind with a now-bogus pointer value. And of course `sz` is no longer accurate. Regarding namespace std, [Why is "using namespace std;" bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – WhozCraig Mar 03 '22 at 19:25
  • [Address sanitizer](https://gcc.godbolt.org/z/d7fGozbMT) catches an error. Running the program in a debugger with that enabled should give you lots of info to work with. – chris Mar 03 '22 at 19:25
  • Recommended compiler options with GCC and clang: `-O3 -Wall -Wextra -pedantic -fsanitize=address -fsanitize=undefined`. `-O3` turns the optimizer all the way on, making the compiler take a much closer look at the code in order to make it faster. The closer it looks, the more likely it is to see mistakes. `-Wall -Wextra` makes the compiler report on a broader range of potential errors. `-pedantic` tells the compiler to keep an eye out for non-Standard, and probably non-portable, code. – user4581301 Mar 03 '22 at 19:32
  • `-fsanitize=address -fsanitize=undefined` turns on extra checks to see if the program does anything stupid and possibly hard-to-spot at runtime. The more you get the tools to do for you, the faster the job's done. – user4581301 Mar 03 '22 at 19:32
  • Should I make a temp pointer to iterate on the list? – Mahammad Ayman Mar 03 '22 at 19:36
  • Discuss that with [your rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging). If Duckie agrees that it's a good idea, back up your code (so you can easily recover if both you and duckie are wrong) and try it out. – user4581301 Mar 03 '22 at 19:42
  • how can I link my project with the Sanitizers I'm using Clion – Mahammad Ayman Mar 04 '22 at 12:36

0 Answers0