Segment Fault.

Receiving a segment fault need help fixing.

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
  #ifndef DNODE_H
#define DNODE_H



template<class Item>
class dnode
{
  public:
    dnode(const Item& init_data = Item(), dnode* init_next = NULL, dnode* init_prev = NULL)
    {
      data_field = init_data;
      next_field = init_next;
      previous_field = init_prev;
    }

    Item& data()
    {
      return data_field;
    }

    const Item& data() const
    {
      return data_field;
    }

    const dnode* next() const
    {
      return next_field;
    }

    dnode* next()
    {
      return next_field;
    }

    const dnode* previous() const
    {
      return previous_field;
    }

    dnode* previous()
    {
      return previous_field;
    }

    void set_data(const Item& new_data)
    {
      data_field = new_data;
    }

    void set_next(dnode* new_link)
    {
      next_field = new_link;
    }

    void set_previous(dnode* new_link)
    {
      previous_field = new_link;
    }

  private:
    Item data_field;
    dnode* next_field;
    dnode* previous_field;

};

#endif
Not enough info need all 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#ifndef ITERATOR_H // THE ITERATOR.H CODE
#define ITERATOR_H

#include "dnode.h"

template<class Item>
class dlist;

template <class Item>
class node_iterator: public std::iterator<std::bidirectional_iterator_tag, Item>
{
  public:
    friend class dlist<Item>;

    node_iterator(dnode<Item>* init = NULL)
   {
      current = init;
   }

   Item& operator *() const
   {
      return current -> data();
   }

   node_iterator& operator ++()
   {
      current = current -> next();
      return *this;
   }

   node_iterator& operator ++(int)
   {
      node_iterator<Item> original(current);
      current = current -> next();
      return original;
   }

   node_iterator& operator --(){
      current = current -> previous();
      return *this;
   }

   node_iterator& operator --(int)
   {
      node_iterator<Item> original(current);
      current = current -> previous();
      return original;
   }

   bool operator ==(const node_iterator<Item> other) const
   {
      return current == other.current;
   }

   bool operator !=(const node_iterator<Item> other) const
   {
      return current != other.current;
   }

  private:
    dnode<Item>* current;
};

template <class Item>
class const_node_iterator
: public std::iterator<std::bidirectional_iterator_tag, Item>
{
public:
   friend class dlist<Item>;

   const_node_iterator(dnode<Item>* init = NULL)
   {
      current = init;
   }

   const Item& operator *() const
   {
      return current -> data();
   }

   const_node_iterator& operator ++()
   { 
      current = current -> next();
      return *this;
   }

   const_node_iterator& operator ++(int)
   {
      node_iterator<Item> original(current);
      current = current -> next();
      return original;
   }

   const_node_iterator& operator --()
   { 
      current = current -> previous();
      return *this;
   }

   const_node_iterator& operator --(int)
   { 
      node_iterator<Item> original(current);
      current = current -> previous();
      return original;
   }

   bool operator ==(const node_iterator<Item> other) const
   {
      return current == other.current;
   }

   bool operator !=(const node_iterator<Item> other) const
   {
      return current != other.current;
   }

  private:
    const dnode<Item>* current;
};

#endif
---------------------------------------------------dlist.h-------------------------------------------
#ifndef DLIST_H
#define DLIST_H

#include "dnode.h"
#include "iterator.h"

template<class Item>
class dlist
{
  public:
    typedef node_iterator<Item> iterator;
    typedef const_node_iterator<Item> const_iterator;
    dlist();
    dlist(const dlist& source);
    ~dlist();
    void operator =(const dlist& source);

    void rear_insert(const Item& entry);
    void show();
    void front_insert(const Item& entry);
    void front_remove();
    void rear_remove();
    void insert_before(iterator position, const Item& entry);
    void insert_after(iterator position, const Item& entry);
    void remove(iterator position);
    int size();

   iterator begin()
   {
      return iterator(head);
   }
   const_iterator begin() const
   {
      return iterator(head);
   }
   iterator end()
   {
      return iterator();
   }
   const_iterator end() const
   {
      return iterator();
   }
   iterator r_begin()
   {
      return iterator(tail);
   }
   const_iterator r_begin() const
   {
      return iterator(tail);
   }
   iterator r_end()
   {
      return iterator();
   }
   const_iterator r_end() const{
      return iterator();
   }

  private:
   dnode<Item>* head;
   dnode<Item>* tail;
};
#include "dlist.template"
#endif
----------------------------------------------------dlist.template-------------------------------------
template <class Item>
dlist<Item>::dlist()
{
   head = NULL;
   tail = NULL;
}

template <class Item>
void dlist<Item>::rear_insert(const Item& entry)
{
   if(head == NULL)
   {
      head = new dnode<Item>(entry);
      tail = head;
   }else
   {
      tail -> set_next(new dnode<Item>(entry, NULL, tail));
      tail = tail -> next();
   }
}

template <class Item>
void dlist<Item>::show()
{
   dnode<Item>* cursor;
   for(cursor = head; cursor != NULL; cursor = cursor -> next())
   {
      std::cout << cursor -> data() << std::endl;
   }
}

template <class Item>
void dlist<Item>::front_insert(const Item& entry)
{
   if(head == NULL)
   {
      head = new dnode<Item>(entry);
      tail = head;
   }
   else
   {
      head -> set_previous(new dnode<Item>(entry, head, NULL));
      head = head -> previous();
   }
}

template<class Item>
void dlist<Item>::front_remove()
{
   if(head == NULL)
   {
      return;
   }
   else if(head -> next() != NULL)
   {
      dnode<Item>* remove_ptr = head;
      head = head -> next();
      head -> set_previous(NULL);
      delete remove_ptr;
   }
}

template<class Item>
void dlist<Item>::rear_remove()
{
   if(head != NULL)
   {
      dnode<Item>* remove_ptr = tail;
      tail = tail -> previous();
      tail -> set_next(NULL);
      delete remove_ptr;
   }
}

template<class Item>
void dlist<Item>::insert_before(iterator position, const Item& entry)
{
   position.current -> set_previous(new dnode<Item>(entry, position.current, position.current -> previous()));
   position.current -> previous() -> set_next(position.current);
}

template<class Item>
void dlist<Item>::insert_after(iterator position, const Item& entry)
{
   position.current -> set_next(new dnode<Item>(entry, position.current -> next(), position.current));
   position.current -> next() -> next() -> set_previous(position.current);
}

template<class Item>
int dlist<Item>::size()
{
   int total = 0;
   iterator it;
   for(it = begin(); it != end(); ++it)
   {
      total++;
   }
   return total;
}

template<class Item>
void dlist<Item>::remove(iterator position)
{
   dnode<Item>* remove_ptr = position.current;
   position.current -> set_previous(position.current -> next());
   delete remove_ptr;
}

template<class Item>
dlist<Item>::dlist(const dlist& source)
{
   head = NULL;
   tail = NULL;
   if(source.head == NULL)
   {
      return;
   }

   dnode<Item>* sourceCursor = source.head;
   while(sourceCursor != NULL)
   {
      rear_insert(sourceCursor -> data());
      sourceCursor = sourceCursor -> next();
   }
}

template<class Item>
dlist<Item>::~dlist()
{
   dnode<Item>* cursor = head;
   dnode<Item>* remove_ptr;
   while(cursor != NULL)
   {
      remove_ptr = cursor;
      cursor = cursor -> next();
      delete remove_ptr;
   }
}

template<class Item>
void dlist<Item>::operator =(const dlist& source)
{
   if(this == &source)
   {
      return;
   }
   dnode<Item>* cursor = head;
   dnode<Item>* remove_ptr;
   while(cursor != NULL)
   {
      remove_ptr = cursor;
      cursor = cursor -> next();
      delete remove_ptr;
   }


   if(source.head == NULL)
   {
      return;
   }
   head = NULL;
   tail = NULL;
   dnode<Item>* sourceCursor = source.head;
   while(sourceCursor -> next() != NULL)
   {
      rear_insert(sourceCursor -> data());
      sourceCursor = sourceCursor -> next();
   }
}
Last edited on
Still missing a main. I need to be able to run it.
There is a decent amount of code give me one second. I`m trying to compile main2.cc and swatches.cc then it proceeds to give me a segment fault.
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
main
#include <iostream>
#include <fstream>
#include "dlist.h"
#include "swatches.h"

using namespace std; 

int main() 
{	
	dlist<Swatch> swatches;
	dlist<Swatch>::iterator it; 
	ifstream fin; 
	fin.open("swatches.txt"); 
	if (fin.fail())
	{
		cout << "Could not open input file." << endl; 
		return 1; 
	}
	Swatch tmp; 
	while (fin >> tmp)
	{
		int red = tmp.get_red(); 
		int green = tmp.get_green(); 
		int blue = tmp.get_blue(); 
		
		if ( (red>=green) && (red>=blue) ) // red is dominant
		{
			swatches.front_insert(tmp); 
		}
		else if ( (green >= red) && (green >= blue) ) 
				// green is dominant
		{
			swatches.rear_insert(tmp); 
		}
		else // blue is dominant
		{
		   it = swatches.begin();
		   for(int i = 0;i < swatches.size()/2;i++)
			++it; // loop moves iterator to the middle
		   if(swatches.size()%2 == 1){
			//if(swatches.size()>2){
			//it++;
			//}
			swatches.insert_before(it,tmp);
		   }
		   else{
			swatches.insert_after(it,tmp);
		   }
		}
	}
	fin.close(); 

	dlist<Swatch> copy(swatches); // make a copy
	
	// remove the front, back, and centermost swatch from the copy
	copy.front_remove(); 
	copy.rear_remove(); 
	it = copy.begin();
	for(int i =0; i < copy.size()/2; ++i)
		++it;
	//if(copy.size()%2 ==1) ++it; // if list has a true middle
					// step up into it
	copy.remove(it);
	
	
	// output the original list frontwards
	for (dlist<Swatch>::iterator i=swatches.begin(); i != swatches.end(); ++i)
	{
		cout << *i << endl; 
	}
	
	cout << endl << endl; // some space
	
	// output the copy frontwards
	for (dlist<Swatch>::iterator i=copy.begin(); i != copy.end(); ++i)
	{
		cout << *i << endl; 
	}

	cout << endl << endl; // some space

	// output the original backwards
	for (dlist<Swatch>::iterator i=swatches.r_begin(); i != swatches.r_end(); --i)
	{
		cout << *i << endl; 
	}
	
	cout << endl << endl; // some space

	// destroy the original list by alternating between removal of first and 
	// last items.  Print each item as it is removed
	int counter=0; 
	while (swatches.size() > 0)
	{
		cout<<*swatches.begin()<<endl;
		swatches.front_remove();
		if(swatches.size() > 0){
		    cout<<*swatches.r_begin()<<endl;
		    swatches.rear_remove();
		}
	}

	cout << endl << endl; // some space

	// output the copy backwards
	for (dlist<Swatch>::iterator i=copy.r_begin(); i != copy.r_end(); --i)
	{
		cout << *i << endl; 
	}

	return 0; 
}

swatches.cc----------------------------------------------
#include "swatches.h"
#include <iomanip>
#include <cstdlib>
#include <cstdio>
using namespace std;

	// CONSTRUCTORS
Swatch::Swatch(){
    color=0;
    red=green=blue=0;
    width=0;
    length=0;
}
Swatch::Swatch(unsigned long n_color, int n_width, int n_length){
    color=n_color;
    red = n_color/TWO_COLOR;
    blue = n_color%ONE_COLOR;
    green = (n_color/ONE_COLOR)%ONE_COLOR;
    width=n_width;
    length=n_length;
}
	// ACCESSOR FUNCTIONS
//This function returns the color as a number which is how it is 
//actually stored
unsigned long Swatch::get_color(){
     return color;
}

//This function returns a string version of the color number in standard
// RGB format
std::string Swatch::color_string(){
    char a_c_string[10]; 
    sprintf(a_c_string,"%x",color);
    std::string tmp(a_c_string);
    int leading = 6 - tmp.length();
    if(leading <= 0) return tmp;
    else{
	std::string padding;
	for(int i=0; i<leading; ++i){
	    padding += '0';
	}
	padding += tmp;
	return padding;
	}
}

//Return amount of red in swatch
unsigned long Swatch::get_red(){
	return red;
}

//Return amount of green in a swatch
unsigned long Swatch::get_green(){
	return green;
}

//Return amount of blue in a swatch
unsigned long Swatch::get_blue(){
	return blue;
}

// Return width of the swatch
int Swatch::get_width(){
    return width;
}

// Returns length of the swatch
int Swatch::get_length(){
    return length;
}

	// MODIFICATION FUNCTIONS
// Sets color to value of the argument
void Swatch::set_color(unsigned long n_color){
    color = n_color;
    red = n_color/TWO_COLOR;
    blue = n_color%ONE_COLOR;
    green = (n_color/ONE_COLOR)%ONE_COLOR;

}

// Sets color to value created when string is converted to a hex number
void Swatch::set_color(std::string n_color){
      //The strol function will work with any base from 2 to 36.
      //The 16 here denotes that we are converting from a 
      //hexadecimal representation.
    color = strtol(n_color.c_str(), (char **)NULL, 16);
    red = color/TWO_COLOR;
    blue = color%ONE_COLOR;
    green = (color/ONE_COLOR)%ONE_COLOR;

}

// Sets width to value of the argument
void Swatch::set_width(int n_width){
    width=n_width;
}

// Sets length to value of argument
void Swatch::set_length(int n_length){
    length=n_length;
}



std::ostream& operator <<(std::ostream& outs,const Swatch& sw){
    char a_c_string[10];
    sprintf(a_c_string,"%x",sw.color);
    std::string tmp(a_c_string);
    int leading = 6 - tmp.length();
    if(leading <= 0) outs<<tmp;
    else{
        std::string padding;
        for(int i=0; i<leading; ++i){
            padding += '0';
        }
        padding += tmp;
        outs<<padding;
        }
    outs<<"  "<<sw.width<<"  "<<sw.length;
    return outs;
}

std::istream& operator >>(std::istream& ins, Swatch& sw){

	ins>>hex>>sw.color>>dec>>sw.width>>sw.length;
	sw.red = sw.color/sw.TWO_COLOR;
    	sw.blue = sw.color%sw.ONE_COLOR;
    	sw.green = (sw.color/sw.ONE_COLOR)%sw.ONE_COLOR;

	return ins;
}

swatches.h------------------------------
#include <iostream>
#include <iomanip>
#include <string>
#ifndef SWATCH_H
#define SWATCH_H

class Swatch{
    public:
	static const unsigned long ONE_COLOR = 256;
	static const unsigned long TWO_COLOR = 256*256;
	// CONSTRUCTORS
	Swatch();
	Swatch(unsigned long n_color, int n_width, int n_height);
	// ACCESSOR FUNCTIONS
	unsigned long get_color();
	std::string color_string();
	unsigned long get_red();
	unsigned long get_green();
	unsigned long get_blue();
	int get_width();
	int get_length();
	// MODIFICATION FUNCTIONS
	void set_color(unsigned long n_color);
	void set_color(std::string n_color);
	void set_width(int n_width);
	void set_length(int n_length);
	// FRIENDS
	friend std::ostream& operator <<(std::ostream& outs, const Swatch& sw);
	friend std::istream& operator >>(std::istream& ins, Swatch& sw);
    private:
	unsigned long color;
	unsigned long red, green, blue;
	int width;
	int length;
};
#endif

Last edited on
I can compile it but I get two warnings related to sprintf in swatches.cc saying that %x expects unsigned int but the argument is long unsigned int. The format spec should be %lx.

But I can't run it because I still need an input file. Probably would've been best to provide a link to an uploaded zip file. But if the input file is not too long you can just post it here. You might still want to provide a zip to make it easier for others, though.
swatches.txt------------------------

fbc454 12 35
b71bfd 23 49
835fb0 21 58
0f59c2 18 51
23b873 27 35
3ebfe7 21 44
a0ca1b 15 48
0e1fdc 14 40
509ce6 15 54
42d6fa 20 37
487ceb 24 45
1f7532 16 56
c339ea 21 58
9ca5b2 18 44
38fbf4 15 43
f0df60 24 54
004164 15 33
1cdec9 13 47
1c054e 10 42
d2f3a6 26 57
ce7731 26 56
5fdf7f 24 51
269f39 23 54
29a1ab 21 56
ad2279 18 57
7e5bee 23 56
952e99 27 48
8a7a99 12 56
e7482c 16 46
1f620a 10 45
a06362 24 48
4ca036 20 50
1e15b9 14 43
a2af11 20 37
594196 25 40
7114c4 18 31
d47692 15 35
b5ebdc 20 52
fe4297 22 53
3470dd 19 55
505300 23 36
67cbea 14 37
7a3bda 18 40
b5ed95 25 36
487e53 17 30
a9c3e8 11 49
ce1dca 12 53
7fa978 16 49
453990 11 57
cb283e 18 55
ceb3dd 12 41
5f7edf 17 44
2ae7f8 10 31
cdf64d 22 46
e572bc 10 30
cd0e94 23 58
bec9b2 17 35
acb2ec 29 29
b5f6f1 20 58
e52236 19 46
c4e16f 27 41
06f6cc 13 42
61d279 16 53
a49b72 24 51
60d634 28 35
20b1cd 17 29
cac73d 21 42
ec1f75 18 35
e47fc9 21 30
c735f9 27 50
10fc42 19 48
090d24 24 39
4edd5e 11 57
8c092b 18 39
0f5e9a 29 44
b32401 17 42
f134e9 17 46
442489 11 29
502f64 21 31
46c5dd 23 32
39925c 21 40
eb7e6f 28 34
fc0765 13 35
b4302b 17 40
696c21 15 36
c32ddd 16 44
de2806 19 35
e23d82 29 56
509a8f 21 37
406488 14 43
0a8343 23 29
f7ecd6 13 37
d067bc 17 54
301bb9 28 33
95515d 21 31
4dc9cf 11 45
317857 20 52
333675 19 31
a409cd 25 52
0d75bd 12 54
1cba01 28 46
5266ae 26 33
15a4d9 26 45
2de1c0 21 37
3f29c4 12 54
ed2883 19 51
917f3b 14 45
c0916e 15 33
f7069c 12 46
bd862f 10 33
e4b841 23 34
efd229 20 33
6c6dbe 17 55
1ba343 20 43
c56568 22 29
766175 22 35
91d280 18 52
e69ad5 11 34
800134 23 47
7edb70 15 44
73469c 24 52
1a3523 23 39
2be560 24 35
3ef13c 22 41
44aaaf 19 37
f69ae0 12 30
54ba2a 22 36
25c38a 28 36
819fa5 11 48
60e9c0 27 46
d4a1ca 13 30
0c4c91 22 57
bd5b7c 28 58
c16557 18 50
2e91dc 21 41
a8db98 10 52
d0ad0b 20 30
931c6a 10 30
27a94f 20 43
It looks like the second next() in this line in dlist::insert_after is returning NULL:
 
    position.current->next()->next()->set_previous(position.current);


Changing it to this lets the program run some more but then I get a "stack smashing detected" error:
1
2
    if (position.current->next()->next())
        position.current->next()->next()->set_previous(position.current);
Last edited on
How should I got about fixing that?
Last edited on
The stack smashing? It means you're overflowing an array. In swatches.cc try making a_c_string bigger (try 32 instead of 10) in both of the functions where you have it. And remember to change the "%x" in both of those sprintf's to "%lx" (that's a lowercase L).

If you mean the next() thing, then look at the edit to the previous post.
Last edited on
So i`ve fixed everything you told me to including the array and it still seems to dump. Thanks for the help!
Last edited on
You have a similar problem here:

1
2
3
4
5
6
7
8
9
10
template<class Item>
void dlist<Item>::rear_remove() {
    if (head != NULL) {
        dnode<Item>* remove_ptr = tail;
        tail = tail -> previous();
        if (tail)                         //// <<<===  you need this
            tail->set_next(NULL);         //// because tail might be NULL
        delete remove_ptr;
    }
}


Unfortunately, there seems to still be another problem that I don't have time to track down right now. It's related to this line near the end of main.cc:
 
		    cout<<*swatches.r_begin()<<endl;


which calls this function in swatches.cc
 
std::ostream& operator <<(std::ostream& outs,const Swatch& sw){


Somehow the sw reference is NULL in that call so accessing sw.color segfaults.
I'm back. We're you able to figure this out or do you still need some help?
I solved the case thank you!
Excellent! You probably already fixed this, but I was looking at dlist.template and saw that remove had bugs. It should be more like this:

1
2
3
4
5
6
7
8
9
template<class Item>
void dlist<Item>::remove(iterator position) {
    dnode<Item>* remove_ptr = position.current;
    if (position.current->previous())
        position.current->previous()->set_next(position.current->next());
    if (position.current->next())
        position.current->next()->set_previous(position.current->previous());
    delete remove_ptr;
}


Also, the "else if" in front_remove would mean that it won't remove the node if the front node is the only node. It should just be like this (which is more like your rear_remove) :

1
2
3
4
5
6
7
8
9
10
template<class Item>
void dlist<Item>::front_remove() {
    if (head) {
        dnode<Item>* remove_ptr = head;
        head = head->next();
        if (head)
            head->set_previous(NULL);
        delete remove_ptr;
    }
}

Topic archived. No new replies allowed.