#unexpected segmentation fault

39 messages · Page 1 of 1 (latest)

cloud pilot
#
#include <iostream>
#include <format>

namespace Phyll {
    template <typename T> class LinkedList {
    private:
        struct Node {
            T data;
            Node *prev, *next;
            explicit Node(const T& data, Node* prev = nullptr, Node* Next = nullptr)
                : data{data}, prev{prev}, next{next} {}
        };

        Node* head;
    public:
        LinkedList() : head{nullptr} {}

        Node* addToEmpty(const T& data) {
            if (head == nullptr) {
                head = new (std::nothrow) Node{data};
                if (head == nullptr)
                    return nullptr;
                return head;
            }
            return nullptr;
        }

        Node* addToEnd(const T& data) {
            Node* temp{new (std::nothrow) Node{data}};
            if (temp == nullptr)
                return nullptr;
            if (head == nullptr) {
                head = temp;
                return head;
            }
            Node* tp{head};
            while (tp != nullptr && tp->next != nullptr)
                tp = tp->next;

            if (tp == nullptr)
                head = temp;
            else {
                tp->next = temp;
                temp->prev = tp;
            }
            return head;
        }

        Node* createList() {
            int n;
            std::cout << "Enter the number of nodes: ";
            std::cin >> n;

            if (n == 0)
                return head;

            std::cout << "Enter the element for the node 1: ";
            int data;
            std::cin >> data;
            head = addToEmpty(data);

            Node* lastNode{head};

            for(int i = 1; i < n; i++) {
                std::cout << std::format("Enter the element for the Node {}: ", i+1) << std::flush;
                std::cin >> data;
                lastNode = addToEnd(data);
            }
            return head;
        }

        void print() const {
            Node* temp{head};
            std::cout << "nullptr";
            while (temp != nullptr) {
                std::cout << std::format(" <- [prev][{}][next] -> ", temp->data);
                temp = temp->next;
            }
            std::cout << "nullptr\n";
        }

        ~LinkedList() {
            while (head != nullptr) {
                const Node* current{head};
                head = head->next;
                delete current;
            }
        }
    };
}

int main() {
    Phyll::LinkedList<int> list;
    list.createList();
    list.print();
}

a little help pls

earnest ridgeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

cloud pilot
#

debug:

Signal = SIGSEGV (Segmentation fault)
this = {Phyll::LinkedList<int> *const} 0x5ffe48 
data = {const int &} 3
temp = {Phyll::LinkedList<int>::Node *} 0x1014e20 
tp = {Phyll::LinkedList<int>::Node *} 0xbaadf00dbaadf00d 
#

in

            while (tp != nullptr && tp->next != nullptr)
naive gazelle
#

0xbaadf00dbaadf00d tends to stand for unintialized heap I think.

#

Add -fsanitize=undefined,address to your flags for better diagnostics.

inner pendant
#

what inputs are you giving it to replicate the segfault?

cloud pilot
#

ok

inner pendant
#

seems to run ok for me (debug build)

#

(windows, msvc)

naive gazelle
#

That's why you use sanitizers 😛

cloud pilot
#

oh wait

inner pendant
#

haha, yeah, good point

cloud pilot
#

explicit Node(const T& data, Node* prev = nullptr, Node* next = nullptr)
: data{data}, prev{prev}, next{Next} {}
💀

#

incorrect constructing

#

!solved

earnest ridgeBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

inner pendant
#

that just shouldn't have compiled though, i had to make that small correction to get it to run

naive gazelle
#

I didn't correct anything 🤔

inner pendant
#

weird... what is it using to init next here then...

explicit Node(const T& data, Node* prev = nullptr, Node* Next = nullptr)
    : data{data}, prev{prev}, next{next} {}
inner pendant
#

but it's not used in the initializer, only lowercase next is used in there

#

next{next}

cloud pilot
#

Node* Next = nullptr

#

if i put
next{Next} it would be valid

#

but instead. i called double next

#

instead calling from its parameters

inner pendant
#

yeah, i just lowercased the parameter to get it to build

#

interesting that msvc doesn't allow that syntax but gcc doesn't mind it

#

ah nvm, it might just be the warning level i have enabled (and warnings as errors)... this is what it was spitting out before i made that edit:

C:\Users\sal\development\vorlac\roguelike\src\main.cpp(14): error C2220: the following warning is treated as an error
C:\Users\sal\development\vorlac\roguelike\src\main.cpp(14): warning C4100: 'Next': unreferenced formal parameter
  C:\Users\sal\development\vorlac\roguelike\src\main.cpp(14): note: the template instantiation context (the oldest one first) is
  C:\Users\sal\development\vorlac\roguelike\src\main.cpp(111): note: see reference to class template instantiation 'Phyll::LinkedList<int>' being compiled
  C:\Users\sal\development\vorlac\roguelike\src\main.cpp(98): note: while compiling class template member function 'Phyll::LinkedList<int>::~LinkedList(void)'
  C:\Users\sal\development\vorlac\roguelike\src\main.cpp(111): note: see the first reference to 'Phyll::LinkedList<int>::~LinkedList' in 'main'
  C:\Users\sal\development\vorlac\roguelike\src\main.cpp(9): note: while compiling class 'Phyll::LinkedList<int>::Node'
  C:\Users\sal\development\vorlac\roguelike\src\main.cpp(14): note: while compiling class template member function 'Phyll::LinkedList<int>::Node::Node(const T &,Phyll::LinkedList<T>::Node *,Phyll::LinkedList<T>::Node *)'
          with
          [
              T=int
          ]
  C:\Users\sal\development\vorlac\roguelike\src\main.cpp(43): note: see the first reference to 'Phyll::LinkedList<int>::Node::Node' in 'Phyll::LinkedList<int>::addToEnd'
  C:\Users\sal\development\vorlac\roguelike\src\main.cpp(82): note: see the first reference to 'Phyll::LinkedList<int>::addToEnd' in 'Phyll::LinkedList<int>::createList'
  C:\Users\sal\development\vorlac\roguelike\src\main.cpp(112): note: see the first reference to 'Phyll::LinkedList<int>::createList' in 'main'
  ninja: build stopped: subcommand failed.
#

if warnings as errors wasn't set, it'd probably also allow it

naive gazelle
inner pendant
#

nice, yeah, strict warnings and treating warnings as errors saves me from typos and small mistakes like this pretty often. @cloud pilot it might be worth tightening up the warning level you're using to compile, you should be able to find a sweet spot that doesn't complain about too much of the stuff you don't care about, but report things like this typo. if you're using cmake you can add an option and toggle it with something like this:

if (ENABLE_WARNINGS_AS_ERRORS MATCHES ON)
    message(NOTICE "[${PROJECT_NAME}] Warnings being treated as errors")
    set_target_properties(${PROJECT_NAME}
      PROPERTIES
        COMPILE_WARNING_AS_ERROR ON
    )
endif()
cloud pilot
#

Warning level 4

#

Or i use -Wall

inner pendant
#

ok yeah that's a pretty good setting for msvc, I use the same on my end, so it may have actually been reported, but still allowed it if you didn't have warnings set as errors