segmentation fault(core dumped)

closed account (1vf9z8AR)
I am getting the error mentioned in the title

question link-
https://www.spoj.com/problems/HS12MBR/

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
  #include<iostream>
#include<vector>
#include<algorithm>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
	vector<int> arr;
	int temp,t,n;
	cin>>t;
	while(t>0)
	{
		vector<int> x1,y1,x2,y2;
		cin>>n;
		while(n>0)
		{
			do
			{
				cin>>temp;
				arr.push_back((int)temp);
			}while(cin && cin.peek()!='\n');
			if(arr[0]==112)
			{
				x1.push_back(arr[1]);
			y1.push_back(arr[2]);
			x2.push_back(arr[1]);
			y2.push_back(arr[2]);
			}
			else if(arr[0]==108)
			{
				x1.push_back(arr[1]);
			y1.push_back(arr[2]);
			x2.push_back(arr[3]);
			y2.push_back(arr[4]);
			}
			else if(arr[0]==99)
			{
				x1.push_back(arr[1]-arr[3]);
			y1.push_back(arr[2]-arr[3]);
			x2.push_back(arr[1]+arr[3]);
			y2.push_back(arr[2]+arr[3]);
			}
			cout<<*max_element(x1.begin(),x1.end())-'0'<<"\t";
			cout<<*max_element(y1.begin(),y1.end())-'0'<<"\t";
			cout<<*max_element(x2.begin(),x2.end())-'0'<<"\t";
			cout<<*max_element(y2.begin(),y2.end())-'0'<<endl;
			arr.clear();
			n-=1;
		}
		x1.clear();
		y1.clear();
		x2.clear();
		y2.clear();
		t-=1;
	}
}
Last edited on
x1, x2, y1 and y2 can be empty, so the return from max_element can return an invalid iterator. When you dereference that invalid iterator, boom.
Some comments in your code would be a good start.
There's a few mistakes in your code.
1. You are trying to read lines like "p 52 13" as all integers. But cin won't read a 'p' as an integer, so cin goes bad and the input loop ends (and you segfault when you try to access arr past the end since you haven't read any x's or y's).
2. When processing a point ('p') the code is assuming 4 integers were read but p is only followed by two integers.
3. The -'0' after the max_element calls shouldn't be there. And two of those calls should probably be min_element.

BTW, you should just say 'p' instead of 112, same for 'l' and 'c'.

So the structure should be more like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    std::cin >> t;
    while (t-- > 0) {
        int n;
        std::cin >> n;
        while (n-- > 0) {
            char ch;
            std::cin >> ch;
            switch (ch) {
            case 'p':
                std::cin >> x >> y;
                ...
                break;
            case 'l':
                std::cin >> x >> y >> x2 >> y2;
                ...
                break;
            case 'c':
                std::cin >> x >> y >> r;
                ...
                break;
            }
        }
        //... max_element, min_element stuff ...
    }

You don't need to keep all the points in vectors. You could "accumulate" the result as you go along in a final boundary rectangle:

 
int low_left_x=1000, low_left_y=1000, up_right_x=-1000, up_right_y=-1000;

Or keep it in a struct:

1
2
3
struct Point { int x, y; };
struct Rect { Point low_left, up_right; };
Rect bound = {1000,1000,-1000,-1000};

Last edited on
Topic archived. No new replies allowed.