Trouble with error: Segmentation fault: 11

The problem probably caused by the function insertfix. I don't know why, is that because there is no enough space for the pointer I used in this function?

Please help me solve this problem. rly important to me.

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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include<iostream>
using namespace std;
typedef struct node* tree;
struct node{
	int key; // jobID
	int total_time;
	int execute_time;
	tree parent;
	char color;
	tree left;
	tree right;
};
class RBtree {
	tree root;
	tree q;
public:
	RBtree() {
		q = NULL;
		root = NULL;
	}
	void insert(int,int);
	void insertfix(tree);
	void leftrotate(tree);
	void rightrotate(tree);
	void del(int key);
	void delnode(tree);
	tree successor(tree);
	void delfix(tree);
	tree search(int);
	tree upper_bound(int);
	tree lower_bound(int);
};

tree empty_node() {
	tree t = new node;
	t->key = 0;
	t->execute_time = 0;
	t->total_time = 0;
	return t;
}

void RBtree::insert(int key,int total_time){
	int i = 0;
	tree p, q;
	tree t = new node;
	t->key = key;
	t->total_time = total_time;
	t->execute_time = 0;
	t->left = NULL;
	t->right = NULL;
	t->color = 'r';
	p = root;
	q = NULL;
	if (root == NULL){
		root = t;
		t->parent = NULL;
	}
	else{
		while (p != NULL){
			q = p;
			if (p->key<t->key)
				p = p->right;
			else
				p = p->left;
		}
		t->parent = q;
		if (q->key<t->key)
			q->right = t;
		else
			q->left = t;
	}
	insertfix(t);
}

void RBtree::del(int key) {
	tree p;
	p = root;
	int found = 0;
	while (p != NULL && found == 0) {
		if (p->key == key)
			found = 1;
		if (found == 0) {
			if (p->key < key) p = p->right;
			else
				p = p->left;
		}
	}
	delnode(p);
}

void RBtree::delnode(tree p) {
	if (p == NULL)
		return;
	tree y = NULL;
	tree q = NULL;
	if (p->left == NULL || p->right == NULL)
		y = p;
	else
		y = successor(p);
	if (y->left != NULL)
		q = y->left;
	else {
		if (y->right != NULL)
			q = y->right;
		else
			q = NULL;
	}
	if (q != NULL)
		q->parent = y->parent;
	if (y->parent == NULL)
		root = q;
	else {
		if (y == y->parent->left)
			y->parent->left = q;
		else
			y->parent->right = q;
	}
	if (y != p) {
		p->color = y->color;
		p->key = y->key;
	}
	if (y->color == 'b' && q != NULL)
		delfix(q);
}


// void RBtree::insertfix(tree t){
// 	tree u;
// 	if (root == t){
// 		t->color = 'b';
// 		return;
// 	}
// 	while (t->parent != NULL && t->parent->color == 'r'){
// 		tree g = t->parent->parent;
// 		if (g->left == t->parent){
// 			if (g->right != NULL){
// 				u = g->right;
// 				if (u->color == 'r'){
// 					t->parent->color = 'b';
// 					u->color = 'b';
// 					g->color = 'r';
// 					t = g;
// 				}
// 			}
// 			else{
// 				if (t->parent->right == t){
// 					t = t->parent;
// 					leftrotate(t);
// 				}
// 				t->parent->color = 'b';
// 				g->color = 'r';
// 				rightrotate(g);
// 			}
// 		}
// 		else{
// 			if (g->left != NULL){
// 				u = g->left;
// 				if (u->color == 'r'){
// 					t->parent->color = 'b';
// 					u->color = 'b';
// 					g->color = 'r';
// 					t = g;
// 				}
// 			}
// 			else
// 			{
// 				if (t->parent->left == t)
// 				{
// 					t = t->parent;
// 					rightrotate(t);
// 				}
// 				t->parent->color = 'b';
// 				g->color = 'r';
// 				leftrotate(g);
// 			}
// 		}
// 		root->color = 'b';
// 		t = t->parent;
// 	}
// }

void RBtree::insertfix(tree t){
	tree *u = new tree;
	if (root == t){
		t->color = 'b';
		return;
	}
	while (t->parent != NULL && t->parent->color == 'r'){
		tree g = t->parent->parent;
		if (g->left == t->parent){
			if (g->right != NULL){
				u = &(g->right);
				if ((*u)->color == 'r'){
					t->parent->color = 'b';
					(*u)->color = 'b';
					g->color = 'r';
					t = g;
				}
			}
			else{
				if (t->parent->right == t){
					t = t->parent;
					leftrotate(t);
				}
				t->parent->color = 'b';
				g->color = 'r';
				rightrotate(g);
			}
		}
		else{
			if (g->left != NULL){
				u = &(g->left);
				if ((*u)->color == 'r'){
					t->parent->color = 'b';
					(*u)->color = 'b';
					g->color = 'r';
					t = g;
				}
			}
			else
			{
				if (t->parent->left == t)
				{
					t = t->parent;
					rightrotate(t);
				}
				t->parent->color = 'b';
				g->color = 'r';
				leftrotate(g);
			}
		}
		root->color = 'b';
		t = t->parent;
	}
}
void RBtree::delfix(tree p){
	tree s;
	while (p != root && p->color == 'b'){
		if (p->parent->left == p){
			s = p->parent->right;
			if (s->color == 'r'){
				s->color = 'b';
				p->parent->color = 'r';
				leftrotate(p->parent);
				s = p->parent->right;
			}
			if (s->right->color == 'b'&&s->left->color == 'b'){
				s->color = 'r';
				p = p->parent;
			}
			else{
				if (s->right->color == 'b'){
					s->left->color = 'b';
					s->color = 'r';
					rightrotate(s);
					s = p->parent->right;
				}
				s->color = p->parent->color;
				p->parent->color = 'b';
				s->right->color = 'b';
				leftrotate(p->parent);
				p = root;
			}
		}
		else{
			s = p->parent->left;
			if (s->color == 'r'){
				s->color = 'b';
				p->parent->color = 'r';
				rightrotate(p->parent);
				s = p->parent->left;
			}
			if (s->left->color == 'b'&&s->right->color == 'b'){
				s->color = 'r';
				p = p->parent;
			}
			else{
				if (s->left->color == 'b'){
					s->right->color = 'b';
					s->color = 'r';
					leftrotate(s);
					s = p->parent->left;
				}
				s->color = p->parent->color;
				p->parent->color = 'b';
				s->left->color = 'b';
				rightrotate(p->parent);
				p = root;
			}
		}
		p->color = 'b';
		root->color = 'b';
	}
}

void RBtree::leftrotate(tree p){
	if (p->right == NULL)
		return;
	else{
		tree y = p->right;
		if (y->left != NULL){
			p->right = y->left;
			y->left->parent = p;
		}
		else
			p->right = NULL;
		if (p->parent != NULL)
			y->parent = p->parent;
		if (p->parent == NULL)
			root = y;
		else{
			if (p == p->parent->left)
				p->parent->left = y;
			else
				p->parent->right = y;
		}
		y->left = p;
		p->parent = y;
	}
}

void RBtree::rightrotate(tree p){
	if (p->left == NULL)
		return;
	else{
		tree y = p->left;
		if (y->right != NULL){
			p->left = y->right;
			y->right->parent = p;
		}
		else
			p->left = NULL;
		if (p->parent != NULL)
			y->parent = p->parent;
		if (p->parent == NULL)
			root = y;
		else{
			if (p == p->parent->left)
				p->parent->left = y;
			else
				p->parent->right = y;
		}
		y->right = p;
		p->parent = y;
	}
}

tree RBtree::successor(tree p){
	tree y = NULL;
	if (p->left != NULL){
		y = p->left;
		while (y->right != NULL)
			y = y->right;
	}
	else
	{
		y = p->right;
		while (y->left != NULL)
			y = y->left;
	}
	return y;
}

tree RBtree::search(int key){
	tree p = root;
	int found = 0;
	while (p != NULL && found == 0){
		if (p->key ==key)
			found = 1;
		if (found == 0){
			if (p->key<key) p = p->right;
			else
				p = p->left;
		}
	}
	if (p == NULL)
		return empty_node();
	return p;
}
tree RBtree::upper_bound(int key) {
	tree p = root;
	tree res=empty_node();
	int distance = 999999;
	while (p != NULL) {
		if (p->key > key) {
			if (p->key - key < distance) {
				res = p;
				distance = p->key - key;
			}
			p = p->left;
		}
		else {
			p = p->right;
		}
	}
	return res;

}
tree RBtree::lower_bound(int key) {
	tree p = root;
	tree res = empty_node();
	int distance = 999999;
	while (p!=NULL) {
		if (p->key < key) {
			if (key - p->key< distance) {
				res = p;
				distance = key - p->key;
			}
			p = p->right;
		}
		else {
			p = p->left;
		}
	}
	return res;
}
Last edited on
Hello cabbdeng,

I managed to fit up the code so it would compile and it did, but without what you did in main I can not test it.

No errors or warnings on compile, so I thinking is happens when it runs.

Andy
Line 88: shouldn't you only call delnode if you found the key?

Beyond that, write a method to validate the tree and try calling it after you call each method. That should help you locate the problem.


Hi, Andy, here is my Main and MinHeap code, glad to see that you are willing to help me try this code and find its problem for me.


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
#pragma once
#define ROOT 1
typedef struct Job *job,j;
struct Job {
	int jobID;
	int execute_time;
	int total_time;
};
class minHeap {
public:
	job jobs[100000];
	int len;
	minHeap() {
		len = 0;
		for (int i = 0; i < 100000; i++) {
			jobs[i] = new j;
			jobs[i]->jobID = jobs[i]->execute_time = jobs[i]->total_time = 0;
		}
	}
	void ShiftDown(int);
	void ShiftUp(int);
	void InsertHeap(int, int);
	int Parent(int);
	int LeftChild(int);
	int RightChild(int);
	bool Empty();
	job Pop();
	void swap(int,int);
	job top();
	job empty_job() {return jobs[0];}
private:
	int _v(int);
};
job minHeap::top() {
	return jobs[ROOT];
}
void minHeap::InsertHeap(int jobID, int total_time) {
	job jo = new j;
	jo->jobID = jobID;
	jo->total_time = total_time;
	jo->execute_time = 0;
	jobs[++len] = jo;
	ShiftUp(len);
}
job minHeap::Pop() {
	job j = jobs[ROOT];
	swap(ROOT, len);
	jobs[len]->jobID = jobs[len]->execute_time = jobs[len]->total_time = 0;
	len--;
	ShiftDown(ROOT);
	return j;
}
void minHeap::swap(int a, int b) {
	job t = jobs[a];
	jobs[a] = jobs[b];
	jobs[b] = t;
}
int minHeap::_v(int index) {
	return jobs[index]->execute_time;
}
void minHeap::ShiftUp(int index) {
	while (_v(index) < _v(Parent(index)) && index > 1) {
		swap(index, Parent(index));
		index = Parent(index);
	}
}
void minHeap::ShiftDown(int index) {
	while (index < len) {
		int v = _v(index);
		int location = index;
		if (LeftChild(index) <= len) {
			if (_v(LeftChild(index)) <= _v(location)) {
				if (_v(LeftChild(index)) == _v(location)) {
					if (jobs[LeftChild(index)]->jobID < jobs[location]->jobID) {
						v = _v(LeftChild(index));
						location = LeftChild(index);
					}
				}
				else {
					v = _v(LeftChild(index));
					location = LeftChild(index);
				}
			}
		}

		if (RightChild(index) <= len) {
			if (_v(RightChild(index)) < _v(location)) {
				if (_v(RightChild(index)) == _v(location)) {
					if (jobs[RightChild(index)]->jobID < jobs[location]->jobID) {
						v = _v(RightChild(index));
						location = RightChild(index);
					}
				}
				else {
					v = _v(RightChild(index));
					location = RightChild(index);
				}
			}
		}

		if (location == index)break;
		swap(index, location);
		index = location;
	}
}

int minHeap::Parent(int index) {
	return index / 2;
}
int minHeap::LeftChild(int index) {
	return index * 2;
}

int minHeap::RightChild(int index) {
	return index * 2 + 1;
}
bool minHeap::Empty() {
	return len == 0;
}


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
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<fstream>
#include"minheap.h"
#include"rbtree.h"
using namespace std;

RBtree rbt;
minHeap heap;
tree mark=NULL;
job running_job=NULL;
int cur_time = 0;
int running_time = 0;
fstream output("output.txt", fstream::out | ios_base::trunc);



void out(tree t) {
	//cout << "(" << t->key << "," << t->execute_time << "," << t->total_time << ")\n";
	output << "(" << t->key << "," << t->execute_time << "," << t->total_time << ")";
}
void out(job j) {
	//cout << "(" << j->jobID << "," << j->execute_time << "," << j->total_time << ")\n";
	output << "(" << j->jobID << "," << j->execute_time << "," << j->total_time << ")";
}

void Run() {
	if (heap.Empty())
		return;

	if (running_job == NULL) {
		running_job = heap.jobs[ROOT];
	}
	if (mark == NULL || mark->key != running_job->jobID)
		mark = rbt.search(running_job->jobID);
	running_job->execute_time++;
	mark->execute_time++;
	running_time++;

	if (running_job->execute_time == running_job->total_time) {
		running_time = 0;
		running_job = NULL;
		rbt.delnode(mark);
		heap.Pop();
	}
	else if(running_time==5){
		running_time = 0;
		heap.ShiftDown(ROOT);
		running_job = heap.jobs[ROOT];
	}
	//printf("At %d ms: Run!\n", cur_time);
}

void InsertJob(int jobID, int total_time) {
	heap.InsertHeap(jobID, total_time);
	rbt.insert(jobID, total_time);
	if (running_time == 0) {
		running_job = heap.jobs[ROOT];
	}
	//printf("At %d ms: Insert! jobID: %d  total_time: %d\n", cur_time, jobID, total_time);
}

void PrintJob(int jobID) {
	tree res = rbt.search(jobID);
	out(res);
	output << "\n";
	//printf("At %d ms: PrintJob! jobID: %d\n", cur_time, jobID);
}

void PrintJob(int low, int high) {
	vector<Job*> output_vec;
	int tot = 0;
	for (int i = ROOT; i <= heap.len; i++) {
		if (low <= heap.jobs[i]->jobID&&heap.jobs[i]->jobID <= high) {
			output_vec.push_back(heap.jobs[i]);
			tot++;
		}
	}
	for (int i = 0; i < output_vec.size(); i++){
		out(output_vec[i]);
		if (i == (output_vec.size()-1)) break;
		output << ",";
	}
	if (!tot)
		out(empty_node());
		output << "\n";
}

void NextJob(int jobID) {
	tree res = rbt.upper_bound(jobID);
	out(res);
	output << "\n";
	//printf("At %d ms: NextJob! jobID: %d\n", cur_time, jobID);
}

void PreviousJob(int jobID) {
	tree res = rbt.lower_bound(jobID);
	out(res);
	output << "\n";
	//printf("At %d ms: PreviousJob! jobID: %d\n", cur_time, jobID);
}

bool in(string s,string p) {
	return s.find(p) != -1;
}

void run2present(int run_time) {
	while (cur_time < run_time)
		Run(), cur_time++;
}

void LoadJobs(const string &path) {
	string s;
	ifstream input(path);
	int jobID, low, high, total_time, run_time;
	while (getline(input, s)) {
		if (in(s, "Insert")) {
			sscanf(s.c_str(), "%d: Insert(%d,%d)", &run_time, &jobID, &total_time);
			run2present(run_time);
			InsertJob(jobID, total_time);
		}
		else if (in(s, "PreviousJob")) {
			sscanf(s.c_str(), "%d: PreviousJob(%d)", &run_time, &jobID);
			run2present(run_time);
			PreviousJob(jobID);
		}
		else if (in(s, "NextJob")) {
			sscanf(s.c_str(), "%d: NextJob(%d)", &run_time, &jobID);
			run2present(run_time);
			NextJob(jobID);
		}
		else if (in(s, "PrintJob")) {
			if (in(s, ",")) { 
				sscanf(s.c_str(), "%d: PrintJob(%d,%d)", &run_time, &low, &high);
				run2present(run_time);
				PrintJob(low, high);
			}
			else {
				sscanf(s.c_str(), "%d: PrintJob(%d)", &run_time, &jobID);
				run2present(run_time);
				PrintJob(jobID);
			}
		}
	}
}

int main(int argc, char* argv[]){
	LoadJobs(argv[1]);
	output.close();
	//getchar();
	return 0;
}

Hi dhayden,

It is a problem in line 88, thank you. But it's not serious to my code, because every time I delete a node, because a node has already finished its job, that means the node I will deleted must be a node in the current RBTree. But thank you anyway, correct this part can make my code be more generic.
1
2
int main(int argc, char* argv[]){
	LoadJobs(argv[1]);
¿how are we supposed to run your code?
sorry guys, I forget to post the input file on,

Here is the sample input as a txt file. And the first number is the global time.

0: Insert(50,60000)
49950: Insert(19,55000)
99950: Insert(30,58000)
125900: PrintJob(19)
199500: Insert(1250,47000)
229500: Insert(1350,37000)
230000: PrintJob(30,5200)
235000: NextJob(1350)
236000: PreviousJob(1350)

Is there anyone figure this problem out? I can't find where is my code wrong yet..
You have really not made this easy for us. We have to basically recreate your program and input files to make it run.

You could have run this under a debugger which would have told you the exact line causing the crash. For the sake of ten minutes learning to use the debugger, you've wasted a day.


Here's what the debugger shows, as soon as its run: https://imgur.com/a/uiuEj

Bad pointer. The pointer 's' is NULL, but you're trying to use it anyway. You can see the call stack at the bottom right to follow the flow of code to this point.
Last edited on
Hello cabbdeng,

I agree with what Repeater is saying. The program starts with "LoadJobs(argv[1])" in main, I think that "argv[1]" not having a value when I first started caused the "Segmentation fault", after I set it up right it worked.

The "LoadJobs" function works up to a point. After you get so far in this function you call other function(s) that eventually end up at the "RBtree::delfix" function and as Repeater showed you in his link "s" has a pointer value of "null" because "p->parent->left" has a value of "null".

I have not traced down every function call yet, but I do wonder based on the function name why is the program in the "delfix" function.

Also thinking that it may be a good idea to check "s" for being "null" and deal with that before entering the next if statement.

Still lookong.

Hope that helps,

Andy
Your RBTree::delnode follows my very old copy of Introduction to Algorithms by Cormen, Leiserson and Rivest. But there are some problems:

Line 99: what if successor(p) is NULL?
Line 119: This isn't in my copy of the book.
Line 120. You should copy the total_time and execute_time fields too.

Yesterday I wrote:
write a method to validate the tree and try calling it after you call each method. That should help you locate the problem.
Did you do that? When I wrote a validate method I found that insertfix() is setting the parent field incorrectly in some case. Comparing your insertfix() to the one in the book, I see differences.
Thx, guys, followed by your instruction, I finally figure this out. There are some problems in function Insertfix and delfix. Appreciate your helps.
Further investigation shows that your rotate methods are wrong.
Hi dhayden, thx for your comments, I noticed this problem also, and I have rewritten these two part already.

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
void RBtree::leftrotate(tree pt){
	if (pt->right == NULL)
		return;
	else{
		tree pt_right = pt->right;
 
	    pt->right = pt_right->left;
	 
	    if (pt->right != NULL)
	        pt->right->parent = pt;
	 
	    pt_right->parent = pt->parent;
	 
	    if (pt->parent == NULL)
	        root = pt_right;
	 
	    else if (pt == pt->parent->left)
	        pt->parent->left = pt_right;
	 
	    else
	        pt->parent->right = pt_right;
	 
	    pt_right->left = pt;
	    pt->parent = pt_right;
	}
}

void RBtree::rightrotate(tree pt){
	if (pt->left == NULL)
		return;
	else{
		tree pt_left = pt->left;
 
	    pt->left = pt_left->right;
	 
	    if (pt->left != NULL)
	        pt->left->parent = pt;
	 
	    pt_left->parent = pt->parent;
	 
	    if (pt->parent == NULL)
	        root = pt_left;
	 
	    else if (pt == pt->parent->left)
	        pt->parent->left = pt_left;
	 
	    else
	        pt->parent->right = pt_left;
	 
	    pt_left->right = pt;
	    pt->parent = pt_left;
	}
}
Does your code work after the change? If not then what are the symptoms? Did you write a validator method?
Topic archived. No new replies allowed.