0

I'm trying to make a list of objects (not using std::list) containing a pointer to the next and previous objects. For some reason the following code is throwing a segmentation fault, but when I commented out std::cout the code won't throw a segmentation fault, and when I don't compile with cmake, but with clang++. In both cases I'm using C++14.

#include <iostream>
class myListElement
{
    myListElement *next;
    double val;
    public:
    myListElement(int entry, myListElement *newPrev):val(entry), prev(newPrev){}
    void setNext(myListElement *newPrev){prev = newPrev;}
};

class myList
{
    myListElement *first,*last;
    public:
    myList(){}
    ~myList(){}
    void push_back(int entry)
    {
        myListElement temp(entry,last);
        if(last != nullptr)
        {
            last->setNext(&temp);
        }
    }
};

int main()
{
    int n = 1000;
    myList my_list;
    //std::cout << "\ntest";
    for(int i = 0; i < n; ++i)
    {
        my_list.push_back(i+1);
    }
}

How can that be the case?

I'm sorry for the long code, but I couldn't find any part to delete without getting the Segmentation fault and keeping the sense of the program. Thank you for every bit of help!

Omar Himada
  • 2,540
  • 1
  • 14
  • 31
  • 6
    You never initialize `last`. – hyde Jan 23 '18 at 18:04
  • 3
    During development, always enable all compiler warnings. – 500 - Internal Server Error Jan 23 '18 at 18:04
  • I originally did initialize last, but deleted the line to shorten the Code example, because it didn't Change anything about the problem. – a_familiar_wolf Jan 23 '18 at 18:07
  • `first` and `last` are uninitialized, and `temp` only exists while in `push_back` and the pointer is not valid afterwards (but you saved it). You are also missing a declaration for `prev`. – crashmstr Jan 23 '18 at 18:08
  • 2
    "but deleted the line to shorten the Code example" you must provide [mcve] so shorting code example is good but it still must be complete and reproduce the problem. – Slava Jan 23 '18 at 18:12
  • @crashmstr that's true but that's only the case, because i shortened the Code to post it here. Doesn't Change anything about the error. – a_familiar_wolf Jan 23 '18 at 18:12
  • @a_familiar_wolf *but when i add commentary std::cout to the Code it wont throw a Segmentation fault,* -- Advice -- leave the code alone that produces the segmentation fault. Don't add, move, or remove lines of code that really have nothing to do with the error (such as `cout` statements). You want to make sure that the error is duplicated so that you get a chance to fix it. By changing your code to add do-nothing lines like `cout` statements, you risk masking the bug, thus making it more difficult to fix the issue. – PaulMcKenzie Jan 23 '18 at 18:13
  • 1
    @a_familiar_wolf Yes, and at least two of those other things are very important to any problems you are seeing. We can't guess what you knowingly omitted and what you did not do. If we have enough code where it would actually *compile*, we can best help. – crashmstr Jan 23 '18 at 18:13
  • 1
    During development, always enable all compiler warnings. And pay attention to them – pm100 Jan 23 '18 at 18:13
  • 2
    `&temp` this is called dangling pointer https://stackoverflow.com/questions/17997228/what-is-a-dangling-pointer – Slava Jan 23 '18 at 18:15

1 Answers1

1

You do not initialize first and last with nullptr in the constructor.

You store pointers to local objects temp, their life is limited by push_back exit. You got dangling pointers and UB.

That is why STL exists and must be used. It is developed by the best C++ professionals and well tested.

273K
  • 29,503
  • 10
  • 41
  • 64
  • Thanks! That solved it, even though I'm still very confused, that adding the std::cout line also removed the Segmentation fault. – a_familiar_wolf Jan 23 '18 at 18:18
  • 1
    @a_familiar_wolf -- Read my original comment. By adding or removing do-nothing lines, you're changing the executable in a way where the corruption bug is moved to another part of the code, or maybe masked completely. – PaulMcKenzie Jan 23 '18 at 18:24
  • 2
    @a_familiar_wolf That is why undefined behaviour is named so, it is unpredictable and floatable. – 273K Jan 23 '18 at 18:24
  • Ok thanks for the info! Btw is there a way to Close a question? – a_familiar_wolf Jan 23 '18 at 18:30
  • You can delete your question. https://meta.stackexchange.com/questions/42265/how-can-i-delete-my-question – 273K Jan 23 '18 at 18:34