Segmentation fault

May 21, 2014 at 7:47pm
closed account (EwCjE3v7)
Here is a simple program that I got as an exercise from my book. I get the following error when I run it:
Segmentation fault


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
// Keyboard keys \ |
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include "Screen.h"
#include "Person.h"
#include "Sales_data.h"
using std::cout; using std::cerr; using std::endl; using std::cin;
using std::ifstream; using std::ofstream;
using std::istringstream; using std::ostringstream;
using std::string; using std::vector; using std::list; using std::deque;

int main()
{
	int is[]  = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
	vector<int> vi;
	list<int> li;

	for (auto elem : is) {
		vi.push_back(elem);
		li.push_back(elem);
	}

	auto begvi = vi.begin();

	while (begvi != vi.end()) {
		if (*begvi % 2) {
			vi.erase(begvi);
		}
		else
			++ begvi;
	}

	auto begli = li.begin();

	while (begli != li.end()) {
		if (*begli % 2 != 0) {
			li.erase(begli);
		}
		else 
			++begli;
	}
	
	for (auto elem : vi) {
		cout << elem << " ";
	}
	cout << endl;

	for (auto elem : li) {
		cout << elem << " ";
	}
	cout << endl;
	return 0;
} 
Last edited on May 21, 2014 at 7:52pm
May 21, 2014 at 7:56pm
erase method of both list and vector invalidates iterators to the erased element, that means it is illegal to use them again. Still you are doing exactly that: first you compare it to another iterator and then dereference it.

References and iterators to the erased elements are invalidated.
http://en.cppreference.com/w/cpp/container/list/erase

Invalidates iterators and references at or after the point of the erase, including the end() iterator.
http://en.cppreference.com/w/cpp/container/vector/erase
Last edited on May 21, 2014 at 7:56pm
May 21, 2014 at 8:07pm
closed account (EwCjE3v7)
Thank you MiiNiPaa

So I just added begvi/begli = before it and that fixed it

I forgot about that thank you :)

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
// Keyboard keys \ |
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include "Screen.h"
#include "Person.h"
#include "Sales_data.h"
using std::cout; using std::cerr; using std::endl; using std::cin;
using std::ifstream; using std::ofstream;
using std::istringstream; using std::ostringstream;
using std::string; using std::vector; using std::list; using std::deque;

int main()
{
	int is[]  = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
	vector<int> vi;
	list<int> li;

	for (auto elem : is) {
		vi.push_back(elem);
		li.push_back(elem);
	}

	auto begvi = vi.begin();

	while (begvi != vi.end()) {
		if (*begvi % 2) {
			begvi = vi.erase(begvi);
		}
		else
			++begvi;
	}

	auto begli = li.begin();

	while (begli != li.end()) {
		if (!(*begli % 2)) {
			begli = li.erase(begli);
		}
		else 
			++begli;
	}

	for (auto elem : vi) {
		cout << elem << " ";
	}
	cout << endl;

	for (auto elem : li) {
		cout << elem << " ";
	}
	cout << endl;
	return 0;
} 
Topic archived. No new replies allowed.