Segmentation fault

The great people of this forum please help i'm at my wits end trying to find why i got segmentation fault. It only errors when i print record with coordinates (1,1)(1,16),(16,16)(16,1) or any number with higher than 16 .

Display.cpp
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
bool sortByAreaAscend( unique_ptr<ShapeTwoD>& lhs, unique_ptr<ShapeTwoD>& rhs)
{
	return lhs->getArea()< rhs->getArea();
	
}

bool sortByAreaDescend( unique_ptr<ShapeTwoD>& lhs, unique_ptr<ShapeTwoD>& rhs)
{
	return lhs->getArea()> rhs->getArea();
	
}

bool sortByTypeAndArea( unique_ptr<ShapeTwoD>& lhs, unique_ptr<ShapeTwoD>& rhs)
{
	return lhs->getContainsWarpSpace()> rhs->getContainsWarpSpace()&& lhs->getArea()> rhs->getArea();
	
}
void print(vector<unique_ptr<ShapeTwoD>>& newCoords)
{
	string s;
	for (int i=0;i<newCoords.size();i++)
	{
		s=newCoords[i]->toString();
		cout<<s<<endl;
	}
}

void compute(vector<unique_ptr<ShapeTwoD>>& newCoords,int count)
{
	ShapeTwoD sh;
	double a;
	for (int i=0;i<newCoords.size();i++)
	{
		newCoords[i]->computeArea();
		
	}
	cout<<"Computation completed! ( "<<count<<" records were updated )"<<endl;
}

void input(vector<unique_ptr<ShapeTwoD>>& data)
{
	string s;
	double a;
	bool b;
	string bs;
		cout<<"[ Input sensor data ]"<<endl;
		cout<<"Please enter name of shape : "<<endl;
		getline(cin,s);
		
		
		if(s=="Rectangle")
		{
			int coordX[4];
			int coordY[4];
		cout<<"Please enter special type : "<<endl;
		cin.clear();
		getline(cin,bs);
		if(bs=="WS")
		{
			b = true;
		}
		else 
		{
			b = false;
		}
		for(int i=0;i<4;i++)
		{
		cout<<"Please enter x-ordinate of pt."<<i+1<<endl;
		cin>>coordX[i];
		cout<<"Please enter y-ordinate of pt."<<i+1<<endl;
		cin>>coordY[i];
		
	
		}

		data.emplace_back(new Rectangle(s,b,a,coordX,coordY));
		cout<<data.size()<<endl;
		
	}

	cout<<"Record successfully stored.Going back to main menu...."<<endl;
}
void sortMenu(vector<unique_ptr<ShapeTwoD>>& newCoords)
{
	char sortChoice;
	cout <<endl;
	cout << "a)	Sort by area (ascending)" << endl;
	cout << "b)	Sort by area (for all records)" << endl;
	cout << "c) Sort by special type" << endl;
	cout <<endl;
	cout <<"Please select sort option ('q' to go main menu) : ";
	cin >>sortChoice;
	
	switch(sortChoice)
	{
		case 'a':
			{
				sort(newCoords.begin(),newCoords.end(),sortByAreaAscend);
				print(newCoords);
			}break;
		case 'b':
			{
				sort(newCoords.begin(),newCoords.end(),sortByAreaDescend);
				print(newCoords);
			}break;
		case 'c':
			{
				sort(newCoords.begin(),newCoords.end(),sortByTypeAndArea);
				print(newCoords);
			}break;
		case 'q':
			{
			 
			}break;
		}
}	





Rectangle.h
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
#include "ShapeTwoD.h"

class Rectangle:public ShapeTwoD{
		private:
		int PointOnShape [100][2];
		int PointInShape [100][2];

	
		double area;
		int x[4];
		int y[4];
		public:
		Rectangle();
		Rectangle(string,bool,double,int[],int[]);

		double computeArea();
		string toString();
		int getPointInShape();
		int getPointOnShape();
		void setArea(double);
		double getArea();
		int getX();
		int getY();
		void setX(int[]);
		void setY(int[]);
	};

Rectangle.cpp
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
#include "Rectangle.h"


Rectangle::Rectangle()
{
}
Rectangle::Rectangle(string sName,bool contain,double sArea, int coordX[4], int coordY[4] ):ShapeTwoD(sName,contain,sArea)
{
    /*for(int i=0;i<4;i++)
    {
    	x[i]=coordX[i];
    	y[i]=coordY[i];
	}*/
	setX(coordX);
	setY(coordY);
	//setArea(sArea);
		
}

/*int Rectangle::getX()
{
	return x;
}
int Rectangle::getY()
{
	return y;
}*/



double Rectangle::getArea()
{
	return area;
}



void Rectangle::setArea(double sArea)
{
	area=sArea;
}

void Rectangle::setX(int coordX[4])
{
	    for(int i=0;i<4;i++)
    {
    	x[i]=coordX[i];

	}
}
void Rectangle::setY(int coordY[4])
{
	    for(int i=0;i<4;i++)
    {
    
    	y[i]=coordY[i];
	}
}

double Rectangle::computeArea()
{
	double area =0;
	int j=3;
	cout<<"I'm a rectangle"<<endl;
	for(int i = 0; i<4; i++)
	{
	
	area = area + ((x[j]+x[i])*(y[j]-y[i]));
	j=i;
	}
	area = area/2;
	cout<<area<<endl;
	setArea(area);
	return area;
}

int Rectangle::getPointInShape()
{
	int maxX=x[0];
	int minX=x[0];
	int maxY=y[0];
	int minY=y[0];
	int j=0;
	
	for (int i = 0; i < 4; i++)
    {
      if (x[i] > maxX)
        {
          maxX = x[i];
        }
      else if (x[i] < minX)
        {
          minX = x[i];
        }
	}
	
	for (int i = 0; i < 4; i++)
    {
      if (y[i] > maxY)
        {
          maxY = y[i];
        }
      else if (y[i] < minY)
        {
          minY = y[i];
        }
	}

    
	for (int i = minX+1; i < maxX; i++)
    {
        for (int k = minY+1; k < maxY; k++)
        {
            PointInShape[j][0] = i;
            PointInShape[j][1] = k;
            j++;
        }
    }

    return j;
	
	
}
int Rectangle::getPointOnShape()
{
	int maxX=x[0];
	int minX=x[0];
	int maxY=y[0];
	int minY=y[0];
	int j=0;
	for (int i = 0; i < 4; i++)
    {
      if (x[i] > maxX)
        {
          maxX = x[i];
        }
      else if (x[i] < minX)
        {
          minX = x[i];
        }
	}
	
	for (int i = 0; i < 4; i++)
    {
      if (y[i] > maxY)
        {
          maxY = y[i];
        }
      else if (y[i] < minY)
        {
          minY = y[i];
        }
	}
	
	for (int i = minX; i <= maxX; i++)
    {
        for (int k = minY; k <= maxY; k++)
        {
            if (i == minX)
            {
                if (k != minY && k != maxY)
                {
                    PointOnShape[j][0] = i;
                    PointOnShape[j][1] = k;
                    j++;

                }


            }//end if i == smallest X

            else if (i == maxX)
            {
                if (k != minY && k != maxY)
                {
                    PointOnShape[j][0] = i;
                    PointOnShape[j][1] = k;
                    j++;

                }
            }// else if i == largest X

            else
            {
                if (k == minY || k == maxY)
                {
                    PointOnShape[j][0] = i;
                    PointOnShape[j][1] = k;
                    j++;
                }
            }// else X in between mininumX and maximumX


        }

    }

    return j;

}
string Rectangle::toString()
{
	int num,num2;
	
	ostringstream os;
	
	string type;
	if(containsWarpspace==true)
	{
		type="WS";
	}
	else
	{
		type="NS";
	}
	
	os<<"Name :"<<name<<endl;
	os<<"Special Type : "<<type<<endl;
	os<<"Area : "<<area<<" units square"<<endl;
	os<<"Vertices : "<<endl;
	os<<"Point[0] : ("<<x[0]<<", "<<y[0]<<")"<<endl;
	os<<"Point[1] : ("<<x[1]<<", "<<y[1]<<")"<<endl;
	os<<"Point[2] : ("<<x[2]<<", "<<y[2]<<")"<<endl;
	os<<"Point[3] : ("<<x[3]<<", "<<y[3]<<")"<<endl;
	os<<"Points on Perimeter : ";
	num = getPointOnShape();
	for(int i =0;i<num;i++)
	{
		os<<"(";
		os<<PointOnShape[i][0];
		os<<",";
		os<<PointOnShape[i][1];
		os<<")";
		
	}
	os<<"\n";
	os<<"Points within shape : ";
	num = getPointInShape();
	for(int k=0; k<num;k++)
	{
		os<<"(";
		os<<PointInShape[k][0];
		os<<",";
		os<<PointInShape[k][1];
		os<<")";
		
	}
	os<<endl;
	string var =os.str();
	
	return var;
}
Last edited on
TBH, this is incredibly complicated code and I can't get it to compile because of the features beyond my current compiler.

If I was simply betting, though, I'd suggest a prime candidate would be the first array index of PointOnShape[][] or PointInShape[][], and occurring somewhere in the loops within rectangle.cpp (lines 110-118, 155-194).

Could you 'cout' the value of j at these points, and make sure it doesn't go to 100 or above (the array dimension). It seems to get up to values of the order of (xmax-xmin)*(ymax-ymin),
although I really don't follow what your code is doing.

You could (temporarily) increase the first-index size of arrays PointOnShape[][] and PointInShape[][] and see if it will cope with larger shapes.

Alternatively, run it through a debugger and let that tell you where the code crashes.
Last edited on
> I can't get it to compile because of the features beyond my current compiler.
you can't compile it because the testcase is incomplete. The OP is missing files and function definitions.

> Alternatively, run it through a debugger and let that tell you where the code crashes.
¿Alternatively? That's the first thing to do.
you can't compile it because the testcase is incomplete. The OP is missing files and function definitions.


Ah well, this is true. It's missing at least ShapeTwoD.h; probably some headers in display.cpp; maybe some other things as well. @CTLZ please provide the whole code.

In fairness, @ne555, I attempted to deal with the first error that the compiler threw back at me and in this case it was, sadly, the absence of unique_ptr. Please correct me if I'm wrong but I thought this would be in header <memory>, although a C++11 extension. Even if I include this header I still find this undeclared, so I may not be able to compile even the complete code.

¿Alternatively? That's the first thing to do. 

It would be nice to learn to use a debugger well, but they aren't the easiest of tools for beginners or justify the time needed for those whose day job isn't programming. It would be good to have an article on their use on this site ... I, for one, would find it helpful. On the whole, though, I have usually managed quite happily with (i) looking for common errors (array bounds, uninitialised variables, divide by zero); (ii) stepping through code with a pen and paper; (iii) printing out values of everything under the sun.

So ... I had better leave it to you.
Last edited on
Ok, i debugged the program and it said that the error begin at the second for loop for my print, it said that k no symbol k in current context and Attempt to use a type name as an expression. but why did it not crash when the number is small? BTW i did a cout for j and it is 196 so i increase the size of the array but still failed
¿the hell are you talking about?
the only print() function present on your code has only one single loop (there is no second loop), using `i' as index (no `j', no `k')

but well, glad that you solve it.
Topic archived. No new replies allowed.