Segmentation fault and new to c++

I am getting a segmentation fault whenever I uncomment the call to the method "removeDuplicates". Any help would be appreciated.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>

using namespace std;

class Node
{

    public:
        Node * nextNode;
        Node * prevNode;
        int data;



        void setData(int data)
        {
            this->data = data;

        };

        int getData()
        {
            return data;

        };
        Node *getNext()
        {
            return nextNode;
        };
        Node *getPrev()
        {
            return prevNode;

        };
        void setNext(Node *anotherNode)
        {
            this->nextNode = anotherNode;

        };
        void setPrev(Node *anotherNode)
        {

            this->prevNode = anotherNode;
        };
        void removeDuplicates(Node * myPtr)
        {
            cout << "I am in";

            Node * nodePtr1 = myPtr;
            Node * nodePtr2 = nodePtr1;
            Node * temp = nodePtr2;

            for(int a=0; a<5; a++)
            {

                for(int a=0; a<5; a++)
                {
                    nodePtr2 = nodePtr2->getNext();
                    temp = nodePtr2;
                    if((nodePtr2->getData()) == (nodePtr1->getData()))
                       {
                           nodePtr2 = nodePtr2->getNext();
                           temp->setNext(nodePtr2);
                       }

                }
                nodePtr1 = myPtr->getNext();
            }
        };
};




int main()
{
    Node * firstNode = new Node;
    Node * secondNode = new Node;
    Node * thirdNode = new Node;
    Node * fourthNode = new Node;
    Node * fifthNode = new Node;



    firstNode->setData(2);
    firstNode->setNext(secondNode);

    secondNode->setData(10);
    secondNode->setNext(thirdNode);

    thirdNode->setData(3);
    thirdNode->setNext(fourthNode);

    fourthNode->setData(2);
    fourthNode->setNext(fifthNode);

    fifthNode->setData(5);

     Node * myPtr = firstNode;
     cout << myPtr->getData();

    cout << myPtr;

   // myPtr->removeDuplicates(myPtr);

/*
    while(myPtr != 0)
   {
       cout << myPtr->getData() << " -> ";
       myPtr = myPtr->getNext();

   }


*/

         return 0;
}
You're trying to dereference a null pointer; nodePtr2->getData(). nodePtr2 is null after you go through the loop a while.
But I have a cout << "I am in" at line 47. It does not even output that in console. Shouldnt it hit the nested for loops and call that just before?
That's not being flushed because it segfaults before the buffer is emptied. Stick a line ending on the end of it.

Then, crank up the debugger and run it. You'll get something like this:


Program received signal SIGSEGV, Segmentation fault.
0x0000000000400b28 in Node::getData (this=0x0) at 182.cpp:23
23	            return data;


and then when you go up one like this...

(gdb) up
#1  0x0000000000400c04 in Node::removeDuplicates (this=0x603010, 
    myPtr=0x603010) at 182.cpp:61
61	                    if((nodePtr2->getData()) == (nodePtr1->getData()))


you'll see the problem comes from this call, and then when you investigate the pointer nodePtr2 like this:

(gdb) print nodePtr2
$2 = (Node *) 0x0


you'll see the pointer has a value of 0x0; it is a null pointer.

Topic archived. No new replies allowed.