pop_back Operation causing SIGSEGV

I have this code, which I'm testing to serve as a test bed for my homemade data structures.

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

using std::cin;
using std::cout;
using std::endl;
using std::flush;
using std::string;
using std::vector;

template <class T>
std::ostream& operator << (std::ostream& o, const std::vector<T>& v)
{
    o << v[0];
    for(int i = 1; i < v.size(); i++)
    { o << ' ' << v[i]; }

    return o;
}

string hello()
{
	string hello = "Hello World.";
	return hello;
}

vector<int> sort(vector<int> ary) //based on Shell Sort from geeksforgeeks.org
{
    int n = ary.size();

    for(int gap = n/2; gap > 0; gap /= 2)
    {
        for (int i = gap; i < n; i++)
        {
            int temp = ary[i];
            int j;
            for (j = i; j >= gap && ary[j - gap] > temp; j -= gap)
            { ary[j] = ary[j - gap]; }

            ary[j] = temp;
        }
    }
    return ary;
}

vector<int> primes(int b) //based on code from algolist.net
{
    vector<int> bucket = vector<int>(b);
    vector<bool> sieve = vector<bool>(b);

    for(int i = 0; i < b; i++)
    { sieve.push_back(false); }

    for(int p = 2; p < sqrt(b); p++)
    {
        if(sieve.at(p) == false)
        {
            for(int k = p * p; k < b; k += p)
            {
				sieve.insert(sieve.begin() + k, true);
            }
        }
    }

	for(int i = 0; i < bucket.capacity(); i++)
	{
		if(sieve.at(i) == false)
		{ bucket.push_back(i); }
	}
	return bucket;
}

string rot(string ins)
{
	string ous = string();
	char ltrsFst[13] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'};
	char ltrsFstC[13] ={'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'};
	char ltrsLst[13] = {'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
	char ltrsLstC[13] ={'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

	for(int i = 0; i < ous.size(); i++)
	{
		for(int j = 0; j < 13; j++)
		{
			if(ins[i] == ltrsFst[j])
			{ ous[i] = ltrsLst[j]; }
			else if(ins[i] == ltrsFstC[j])
			{ ous[i] = ltrsLstC[j]; }
			else if(ins[i] == ltrsLst[j])
			{ ous[i] = ltrsFst[j]; }
			else if(ins[i] == ltrsLstC[j])
			{ous[i] = ltrsFstC[j]; }
		}
	}
	return ous;
}

void showArr(vector<int> a)
{
	for(int i = 0; i < a.size(); i++)
	{
		cout << '[' << a[i] << ']';
	}
	for(int i = 0; i < a.capacity(); i++)
	{
		cout << "[ ]";
	}
	cout << endl;
}

int test1()
{
	int se;
	string inp;
	vector<int> ray;

	cout << "Welcome to the Interactive ArrayList." << endl;
	cout << "What size of array do you want?" << endl;

	cin >> se;
	ray = vector<int>(se);

	for(bool close = false; close != true;)
	{ try {
		cout << "Commands are: " << endl
			 << "append `x`, insert `x` `p`, popend, remove `p`, sort, swap `p1` `p2`" << endl;
		showArr(ray);

		cin >> inp;
		if(inp == "X")
		{
			std::cout << "Are you sure?" << endl
				<< "Y/n" << endl;
			cin >> inp;

			if(inp == "Y")
			{
				cout << "Testing Complete" << endl;
				close = true;
			}
			else
			{
				cout << "Resuming Test" << endl;
			}
		}
		else if(inp == "append")
		{
			int num;
			cin >> num;
			ray.push_back(num);
		}
		else if(inp == "insert")
		{
			int num;
			int pos;
			cin >> num >> pos;
			ray.insert(ray.begin() + pos, num);
		}
		else if(inp == "popend")
		{
			ray.pop_back();
		}
		else if(inp == "remove")
		{
			int pos;
			cin >> pos;
			ray.erase(ray.begin() + pos);
		}
		else if(inp == "swap")
		{
			int p1;
			int p2;
			cin >> p1 >> p2;
			std::swap(p1, p2);
		}
		else
		{ cout << "Command not recognized. Try again." << endl; }

		cout << flush;
	}
	catch(std::exception& e)
	{
		cout << "ERROR: " << e.what() << endl;
	}
	}
	return 0;
}

int test2()
{
	string inp;
	vector<int> bucket = vector<int>();
	int num = 0;

	cout << "Welcome to Array Test Auto" << endl;
	cout << endl;
	cout << "--- Printing Test ---" << endl;
	cout << hello() << endl;
	cout << endl;
	cout << "--- Sort Test ---" << endl;

	while(cin.get() != '\n')
	{
		cin >> num;
		bucket.push_back(num);
	}

	cout <<	sort(bucket) << endl;
	cout << endl;
	cout << "--- Primes Test ---" << endl
		 << "Enter a number to count to..." << endl;
	cin >> num;
	cout << "Primes from one to " << num;
	cout << primes(num) << endl;
	cout << endl;
	cout << "--- Rot13 Test ---" << endl
		 << "Enter a string." << endl;
	cin >> inp;
	cout << rot(inp) << endl;
	cout << endl;
	cout << "Testing Complete" << endl;
	return 0;
}

int main()
{
	char inp;
	vector<int> bucket;
	int num = 0;
	int rtn = 0;
	cout << "How do you want to test ArrayList:" << endl
		 << "\t 1: Manipulate an ArrayList of integers." << endl
		 << "\t 2: Print from an ArrayList" << endl
		 << "\t 3: Sort an ArrayList of Integers" << endl
		 << "\t 4: Generate a sequence of prime numbers" << endl
		 << "\t 5: Run Rot13 on a string" << endl
		 << "\t 6: Run all four automatic tests" << endl
		 << endl
		 << "Enter X to quit.";

	cin >> inp;

	for(bool close = false; close != true;)
	{
		if(inp == 'X')
		{
			cout << "Are you sure?" << endl
				 << "Y/n" << endl;
			cin >> inp;

			if(inp == 'Y')
			{
				cout << "Testing Complete" << endl
					 << "Press ENTER to continue...";
				close = true;
				rtn = 0;
				cin.ignore();
			}
			else
			{ cout << "Resuming Test" << endl; }
		}
		else if(inp == '1')
		{
			cout.clear();
			rtn = test1();
			cin.clear();
		}
		else if(inp == '2')
		{
			cout.clear();
			cout << hello() << endl;
			cin.clear();
		}
		else if(inp == '3')
		{
			cout.clear();
			bucket = vector<int>();
			cout << "Enter a list of integers:" << endl
				 << endl;
			while(cin.get() != '\n')
			{
				cin >> num;
				bucket.push_back(num);
			}
			cout << "This is your list:" << endl
				 << bucket << endl
				 << endl;
			bucket = sort(bucket);
			cout << "This is your list sorted:" << endl
				 << bucket << endl;
			cin.clear();
		}
		else if(inp == '4')
		{
			cout.clear();
			cout << "What number do you want to count to?" << endl;
			cin >> num;
			cout << primes(num) << endl;
			cin.clear();
		}
/* ignore this for now it's not done
		else if(inp == '5')
		{
			cout.clear();
			cout << "Enter a string" << endl;
			cin.clear();
		}
*/
		else if(inp == '6')
		{
			cout.clear();
			rtn = test2();
			cin.clear();
		}
		else
		{ cout << "Command not recognized. Try again." << endl; }
	}
	return rtn;
}


I'm running with the MSVC++ compiler on Windows 10 (64-bit) and very time the code tries to call vector::pop_back, I get a segfault. Any idea what's wrong?
Last edited on
All of these have the same fundamental problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
		else if(inp == "insert")
		{
			int num;
			int pos;
			cin >> num >> pos;
			ray.insert(ray.begin() + pos, num);
		}
		else if(inp == "popend")
		{
			ray.pop_back();
		}
		else if(inp == "remove")
		{
			int pos;
			cin >> pos;
			ray.erase(ray.begin() + pos);
		}

insert & erase:
What if pos == -42?

pop_back:
If the container is not empty, the function never throws exceptions (no-throw guarantee).
Otherwise, it causes undefined behavior.


What is common to all three is that you don't check whether the actions that you attempt are actually valid. Do not trust the user input. Validate, before using.
Last edited on
Topic archived. No new replies allowed.