Segmentation Fault?

Why I am getting a segmentation fault in my program? I think its a problem with my push function being stuck in the decision statement. How can I fix this?

Here is my code:
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#ifndef QUEUE_H
#define QUEUE_H

#include <iostream>
#include <stdexcept>
#include <cstdlib>

template <class T>
struct QNode
   {
     QNode(const T&);

     //Data members
     T data;
     QNode<T> *next;
   };


//Method definitions for the QNode class

template <class T>
QNode<T>::QNode(const T& newData)
{  
  data = newData
  next = NULL;  
}

// Forward declaration of the Queue template class
template <class T>
class Queue;

// Forward declaration of the operator<< template function
template <class T>
std::ostream& operator<<(std::ostream&, const Queue<T>&);

template <class T>
class Queue
   {
   // friend declaration for the template function - note the
   // special syntax
   friend std::ostream& operator<< <>(std::ostream&, const Queue<T>&);

   public:

        Queue();
      // ~Queue();
       // Queue(const Queue<T>&) copy constructor?
        int size() const;
        bool empty() const;
       // void clear();
       // T& front() throw(out_of_range);
       // T& back() throw(out_of_range);
        void push(const T&);
       // void pop();


    //Operators to overload
    //assignment operator

   private:

         QNode<T> *qFront;
         QNode<T> *qRear;

   };

// Method definitions for the Queue class

template <class T>
std::ostream& operator<<(std::ostream& out, const Queue<T>& rightOp)
                                                                    
template <class T>
class Queue
   {
   // friend declaration for the template function - note the
   // special syntax
   friend std::ostream& operator<< <>(std::ostream&, const Queue<T>&);

   public:

        Queue();
      // ~Queue();
       // Queue(const Queue<T>&) copy constructor?
        int size() const;
        bool empty() const;
       // void clear();
       // T& front() throw(out_of_range);
       // T& back() throw(out_of_range);
        void push(const T&);
       // void pop();


    //Operators to overload
    //assignment operator

   private:

         QNode<T> *qFront;
         QNode<T> *qRear;

   };

// Method definitions for the Queue class

template <class T>
std::ostream& operator<<(std::ostream& out, const Queue<T>& rightOp)

{
  int i;
  for(i = 0; i < rightOp.size(); i++)
     out << " " << rightOp;
  
  return out;
}                                               

template <class T>
Queue<T>::Queue()
{
  qFront = NULL;
  qRear  = NULL;
}

template <class T>
int Queue<T>::size() const
{
  int count = 0;
  QNode<T> *ptr;

  ptr = qFront;

  while(ptr != NULL)
  {
    count++;
    ptr = ptr->next;
  }

 return count;

}

template <class T>
bool Queue<T>::empty() const
{
  if (qFront == NULL)
     return true;
  else
     return false;
}

template <class T>
void Queue<T>::push(const T& item)
{
  QNode<T> *ptr, *lagPtr;
  ptr = new QNode<T>(item);

  ptr->data = item;
  ptr->next = NULL;

   if(qFront == NULL)
  {
    qFront = ptr;
  }
  else
  {
    lagPtr = qFront;
      while(lagPtr->next!=NULL)
      lagPtr = lagPtr->next;

   lagPtr->next = ptr;
  }


}

#endif /* QUEUE_H */
                                              


Driver Program

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

#include "Queue.h"
#include <iostream>
#include <stdexcept>

using std::cout;
using std::endl;
using std::out_of_range;

int main()
   {
   cout << "Testing default constructor\n\n";

   Queue<int> q1;

   cout << "q1 (size " << q1.size() << "): " << q1 << endl;
   cout << "q1 is " << ((q1.empty()) ? "empty\n" : "not empty\n");
   cout << endl;

   cout << "Testing push()\n\n";

   q1.push(17);

   cout << "q1 (size " << q1.size() << "): " << q1 << endl; //causes                                                
                                                          // segmentation fault
   cout << "q1 is " << ((q1.empty()) ? "empty\n" : "not empty\n");
   cout << endl;



Last edited on
Well, this depends on the T. If it allows assignment, then all you have to do is this:

data = newData;
I don't believe it works because my push function isn't working properly. Or maybe my size function.

Here is all my code so far:

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

#ifndef QUEUE_H
#define QUEUE_H

#include <iostream>
#include <stdexcept>
#include <cstdlib>

template <class T>
struct QNode
   {
     QNode(const T&);

     //Data members
     T data;
     QNode<T> *next;
   };


//Method definitions for the QNode class

template <class T>
QNode<T>::QNode(const T& newData)
{
   data = newData;
   next = NULL;
}


template <class T>
class Queue;

// Forward declaration of the operator<< template function
template <class T>
std::ostream& operator<<(std::ostream&, const Queue<T>&);

template <class T>
class Queue
   {
   // friend declaration for the template function 
   // special syntax
   friend std::ostream& operator<< <>(std::ostream&, const Queue<T>&);

   public:

        Queue();
      // ~Queue(); 
       // Queue(const Queue<T>&) copy constructor
        int size() const;
        bool empty() const;
       // void clear();
       // T& front() throw(out_of_range);
       // T& back() throw(out_of_range);
        void push(const T&);
       // void pop();


    //Operators to overload
    //assignment operator

   private:

         QNode<T> *qFront;
         QNode<T> *qRear;

   };


template <class T>
std::ostream& operator<<(std::ostream& out, const Queue<T>& rightOp)

{
  int i;
  for(i = 0; i < rightOp.size(); i++)
     out << " " << rightOp; // is this correct? 

  return out;
}



template <class T>
Queue<T>::Queue()
{
  qFront = NULL;
  qRear  = NULL;
}

template <class T>
int Queue<T>::size() const
{
  int count = 0;
  QNode<T> *ptr;

  ptr = qFront; //trans list

//count nodes
  while(ptr != NULL) 
  {
    count++;
    ptr = ptr->next;
  }

 return count;

}

template <class T>
bool Queue<T>::empty() const
{
  if (qFront == NULL)
     return true;
  else
     return false;
}

template <class T>
void Queue<T>::push(const T& item)
{
  QNode<T> *ptr, *lagPtr;
  ptr = new QNode<T>(item); //If I don't put(item) i get an error regarding the QNode construct

  ptr->data = item;
  ptr->next = NULL;

  if(qFront == NULL)
  {
    qFront = ptr;
  }
  else
  {
    lagPtr = qFront;
      while(ptr->next!=NULL)
      lagPtr = lagPtr->next;

   lagPtr->next = ptr;
  }

}

#endif  


Driver program

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
#include "Queue.h"
#include <iostream>
#include <stdexcept>

using std::cout;
using std::endl;
using std::out_of_range;

int main()
   {
   cout << "Testing default constructor\n\n";

   Queue<int> q1;

   cout << "q1 (size " << q1.size() << "): " << q1 << endl;
   cout << "q1 is " << ((q1.empty()) ? "empty\n" : "not empty\n");
   cout << endl;

   cout << "Testing push()\n\n";

   q1.push(17);

   cout << "q1 (size " << q1.size() << "): " << q1 << endl;
   cout << "q1 is " << ((q1.empty()) ? "empty\n" : "not empty\n");
   cout << endl;


If I comment out the size function in the driver program it will "compile" but I don't know if its really the size function being the problem or my push function. I am really new with templates, queues, and linked lists.
1
2
3
4
5
6
7
8
else
{
    lagPtr = qFront;
        while(ptr->next!=NULL)
        lagPtr = lagPtr->next;

    lagPtr->next = ptr;
}

You meant lagPtr->next!=NULL instead in your loop condition.
Last edited on
I still get a segmentation fault with the changed you mentioned. I think its stuck in the decision statement. Is it because the list is some how never reaches NULL?
Last edited on
Here is the problem:

1
2
3
4
5
6
7
8
9
10
template <class T>
std::ostream& operator<<(std::ostream& out, const Queue<T>& rightOp)

{
  int i;
  for(i = 0; i < rightOp.size(); i++)
     out << " " << rightOp; // is this correct?  /*No, it isn't*/

  return out;
}

This enters into a recursive call, printing the Queue over and over. The reason this didn't happen at the beginning is because the size was zero.

What you want to do is start at the beginning of the queue and loop over it, much like you're already doing in the push and size methods, while printing each node's data.
Yeah this seems to work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <class T>
std::ostream& operator<<(std::ostream& out, const Queue<T>& rightOp)
{
  QNode<T> *ptr;
  ptr = rightOp.qFront;

  while(ptr != NULL)
  {
     out << " " << ptr->data;
     ptr = ptr->next;
  }


  return out;
}


Thanks for the help


Topic archived. No new replies allowed.