SIGSEGV Error Cause??????

Help me figure out why I am getting a SIGSEGV error while submitting at SPOJ.
Also is there a utility by which we can check the cause of such signals in our program. For eg: - i use "g++ -Wall -ansi -pedantic filename.cpp"

Okay so my code is:

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
#include <iostream>
#include <vector>

using namespace std;

void merge(vector<int> &a, int, int, int);

void merge_sort(vector<int> &A, int left, int right)
{
	int mid;
	if (left >= right) {
		return;
	}
	else {
		mid = (left + right) / 2;
		merge_sort(A, left, mid);
		merge_sort(A, mid + 1, right);

		merge(A, left, mid, right);
	}
}

void merge(vector<int> &a, int left, int mid, int right)
{
	int b[100];

	int i;
	int j;
	int k = left;

	for (i = left; i <= right; i++) {
		b[i] = a[i];
	}

	i = left;
	j = mid + 1;

	while (i <= mid && j <= right) {
		if (b[i] < b[j]) {
			a[k] = b[i];
			i = i + 1;
		}

		else {
			a[k] = b[j];
			j = j + 1;
		}
		k = k + 1;
	}

	while (i <= mid) {
		a[k] = b[i];
		i = i + 1;
		k = k + 1;
	}

	while (j <= right) {
		a[k] = b[j];
		j = j + 1;
		k = k + 1;
	}
} 

void answer(vector<int> a, int left, int right, int k, vector<int> &v)
{
	int b[100];
	int ctr = 0;		// ctr is counter for b[100];	
	merge_sort(a, left, right);
	for (int i = left; i <= right; i++) {
		b[ctr] = a[i];
		ctr = ctr + 1;
	}
	
	v.push_back(b[k]);
}		
int main()
{
	int size, temp;
	int num_ques;

	int left, right, k;

	vector<int> v;

	cin >> size;
	cin >> num_ques;

	vector<int> arr;

	for (int i = 0; i < size; i++) {
		cin >> temp;
		arr.push_back(temp);
	}
	

	for (int i = 0; i < num_ques; i++) {
		cin >> left;
		cin >> right;
		cin >> k;
		
		answer(arr, left - 1, right - 1, k - 1, v);
	}

	for (int i = 0; i < v.size(); i++) {
		cout << v[i] << endl;
	}

	return 0;
}
My first guess is your running into a memory issue with the recursion. The other option I see could be something in the Vector class are loosing track of something. Have you traced through the program to see where it crashes?

The other note I make is most places you pass by reference except on the Answer function, it makes a copy each time it goes through things. This might be the source of the memory overload, considering how many times it copies the for each call of Answer.
Last edited on
Yes it may be a cause!!! i will check it once again
What values are you entering? A quick look through indicates that for some input values you'll read and write unallocated memory.
Last edited on
SAMPLE INPUT
7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3
The above was the sample input given for the spoj question
https://www.spoj.pl/problems/MKTHNUM/

My program runs absolutely fine for the test case.

Now i also replaced vectors with array still i'm facing the same problem.

Here' s the new 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
#include <iostream>
#include <vector>

using namespace std;

void merge(int a[], int, int, int);

void merge_sort(int A[], int left, int right)
{
	int mid;
	if (left >= right) {
		return;
	}
	else {
		mid = (left + right) / 2;
		merge_sort(A, left, mid);
		merge_sort(A, mid + 1, right);

		merge(A, left, mid, right);
	}
}

void merge(int a[], int left, int mid, int right)
{
	int b[100];

	int i;
	int j;
	int k = left;

	for (i = left; i <= right; i++) {
		b[i] = a[i];
	}

	i = left;
	j = mid + 1;

	while (i <= mid && j <= right) {
		if (b[i] < b[j]) {
			a[k] = b[i];
			i = i + 1;
		}

		else {
			a[k] = b[j];
			j = j + 1;
		}
		k = k + 1;
	}

	while (i <= mid) {
		a[k] = b[i];
		i = i + 1;
		k = k + 1;
	}

	while (j <= right) {
		a[k] = b[j];
		j = j + 1;
		k = k + 1;
	}
} 

void answer(int a[], int left, int right, int k, vector<int> &v)
{
	int b[100];
	int ctr = 0;		// ctr is counter for b[100];	
	merge_sort(a, left, right);
	for (int i = left; i <= right; i++) {
		b[ctr] = a[i];
		ctr = ctr + 1;
	}
	
	v.push_back(b[k]);
}		
int main()
{
	int size;
	int num_ques;

	int left, right, k;

	vector<int> v;

	cin >> size;
	cin >> num_ques;

	int arr[size];
	int temp[size];

	for (int i = 0; i < size; i++) {
		cin >> arr[i];
	}

	for (int i = 0; i < size; i++) {
		temp[i] = arr[i];
	}
	
	for (int i = 0; i < num_ques; i++) {
		cin >> left;
		cin >> right;
		cin >> k;
		
		answer(arr, left - 1, right - 1, k - 1, v);

		for (int j = 0; j < size; j++) {
			arr[j] = temp[j];
		}
	}

	for (int i = 0; i < v.size(); i++) {
		cout << v[i] << endl;
	}

	return 0;
}

My program runs absolutely fine for the test case.

To fix the bug, it helps if we get to see the bug. Giving us the case which does not exhibit the bug isn't going to help.


Also is there a utility by which we can check the cause of such signals in our program.


Build it with debugging symbols included and then run it under valgrind.
Last edited on
Topic archived. No new replies allowed.