Segmentation Fault

The two lines with "error: segmentation fault" comments are causing segmentation fault. Does someone know why?
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
#include <iostream>

#include <algorithm>

#include <vector>



int main()

{

	int* numbers;

	bool loop = false;

	char tempChar = ' ';

	int tempInt = 0;

	int tempInt2 = 0;

	int size = 0;

	std::cout << "Add numbers? (Y/n): ";

	std::cin >> tempChar;

	if (tempChar == 'y' || tempChar == 'Y')

	{

		loop = true;

	}

	else if (tempChar == 'N' || tempChar == 'n')

	{

		loop = false;

	}

	while (loop)

	{

		std::cout << "Enter how many numbers to enter:";

		std::cin >> tempInt;	

		size = size + tempInt;

		for (int i = 0; i < tempInt; i++)

		{

			std::cin >> tempInt2;

			*numbers = tempInt2; //error: segmentation fault

			numbers++;

		}

		std::cout << "Add numbers? (Y/n): ";

		std::cin >> tempChar;

		if (tempChar == 'y' || tempChar == 'Y')

		{

			loop = true;

		}

		else if (tempChar == 'N' || tempChar == 'n')

		{

			loop = false;

		}

	}

	numbers = numbers-size;

	for (int i = 0; i < size; i++)

	{

		std::cout << *numbers << std::endl;

		numbers++;

	}
	numbers = numbers-size;	
	std::vector<int> v; //error: segmentation fault
	//v.resize(size);
	//copy(numbers, numbers+size, v.begin());
	//std::vector<int>::iterator vi;
	//for (vi = v.begin(); vi != v.end(); vi++)
	//{
	//	std::cout << *vi << std::endl;
	//}
	

}
compile with warnings.
read the warnings.
fix the warnings.
I get this error now: *** stack smashing detected ***: <unknown> terminated
Aborted (core dumped).

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

#include <algorithm>

#include <vector>



int main()

{
	int numbers[] = {};

	int* number = numbers;

	bool loop = false;

	char tempChar = ' ';

	int tempInt = 0;

	int tempInt2 = 0;

	int sz = 0;

	std::cout << "Add number? (Y/n): ";

	std::cin >> tempChar;

	if (tempChar == 'y' || tempChar == 'Y')

	{

		loop = true;

	}

	else if (tempChar == 'N' || tempChar == 'n')

	{

		loop = false;

	}

	while (loop)

	{

		std::cout << "Enter how many number to enter:";

		std::cin >> tempInt;	

		sz = sz + tempInt;

		for (int i = 0; i < tempInt; i++)

		{

			std::cin >> tempInt2;

			*number = tempInt2; 

			number++;

		}

		std::cout << "Add number? (Y/n): ";

		std::cin >> tempChar;

		if (tempChar == 'y' || tempChar == 'Y')

		{

			loop = true;

		}

		else if (tempChar == 'N' || tempChar == 'n')

		{

			loop = false;

		}

	}

	number = number-sz;

	for (int i = 0; i < sz; i++)

	{

		std::cout << *number << std::endl;

		number++;

	}

	number = number-sz;	

	std::vector<int> v;

	v.resize(sz);

	copy(numbers, numbers+sz, v.begin());

	std::vector<int>::iterator vi;

	for (vi = v.begin(); vi != v.end(); vi++)

	{

		std::cout << *vi << std::endl;

	}

}


The error happens when I add multiple numbers.
The error message you are getting now is a clear indication that you're writing past the bounds of an object (esp. an array) with automatic storage duration.

numbers is a fixed-size array with automatic storage duration. How many elements does it have? How large is it?
Last edited on
This program works but how come this works? I think this should be an error. I thought I was supposed to delete the original array, then copy the original array to the new array, finally add additional numbers
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
#include <iostream>
#include <algorithm>
#include <vector>

int main()
{
	int* numbers;
	bool loop = false;
	char tempChar = ' ';
	int tempInt = 0;
	int tempInt2 = 0;
	int sz = 0;
	std::cout << "Add number? (Y/n): ";
	std::cin >> tempChar;
	if (tempChar == 'y' || tempChar == 'Y')
	{
		loop = true;
	}
	else if (tempChar == 'N' || tempChar == 'n')
	{
		loop = false;
	}
	while (loop)
	{
		std::cout << "Enter how many number to enter:";
		std::cin >> tempInt;
		sz = sz + tempInt;	
		if (sz - tempInt == 0)
             	{
			numbers = new int[sz];
		}
		for (int i = 0; i < tempInt; i++)
		{
			std::cin >> tempInt2;
			*numbers = tempInt2;
			numbers++; 
		}
		std::cout << "Add number? (Y/n): ";
		std::cin >> tempChar;
		if (tempChar == 'y' || tempChar == 'Y')
		{
			loop = true;
		}
		else if (tempChar == 'N' || tempChar == 'n')
		{
			loop = false;
		}
	}
	numbers = numbers - sz;
	for (int i = 0; i < sz; i++)
	{
		std::cout << *numbers << std::endl;
		numbers++;
	}
	numbers = numbers - sz;
	std::vector<int> v;
	v.resize(sz);
	copy(numbers, numbers+sz, v.begin());
	std::vector<int>::iterator vi;
	for (vi = v.begin(); vi != v.end(); vi++)
	{
		std::cout << *vi << std::endl;
	}
}
Last edited on
This program works but how come this works?
Purely by accident.

This is a valuable lesson. Just because a program works doesn't mean that it's correctly formed.

I made a small change to the program:
1
2
3
4
        if (sz - tempInt == 0) {
            numbers = new int[sz];
            std::cout << "Created array of " << sz << " numbers\n";
        }

Now when I run it I get this:
$ ./foo
Add number? (Y/n): y
Enter how many number to enter:10
Created array of 10 numbers
1
2
3
4
5
6
7
8
9
10
Add number? (Y/n): y
Enter how many number to enter:3
1
2
3
Add number? (Y/n): n
1
2
3
4
5
6
7
8
9
10
1
2
3
1
2
3
4
5
6
7
8
9
10
83
0
0

Where the heck does 83 come from???

The problem is that you create an array of 10 items and then stuff 13 numbers into it. So you're using memory that wasn't allocated to the array. To fix this, each time you add more numbers to the array. you need to:
- allocate enough space to hold the old numbers and the new ones.
- copy the old numbers into the new space.
- delete the old space.
This is what I came up with:
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
#include <iostream>
#include <algorithm>
#include <vector>

int main()
{
	int* numbers = NULL;
	int* numbers2 = NULL;
	bool loop = false;
	char tempChar = ' ';
	int tempInt = 0;
	int tempInt2 = 0;
	int sz = 0;
	bool flag = false;
	bool flag2 = false;
	std::cout << "Add number? (Y/n): ";
	std::cin >> tempChar;
	if (tempChar == 'y' || tempChar == 'Y')
	{
		loop = true;
	}
	else if (tempChar == 'N' || tempChar == 'n')
	{
		loop = false;
	}
	while (loop)
	{
		std::cout << "Enter how many numbers to enter:";
		std::cin >> tempInt;
		sz = sz + tempInt;	
		if (sz - tempInt == 0)
		{
			flag = true;
			numbers = new int[sz];
			std::cout << "Created array of " << sz << " numbers\n";
			for (int i = 0; i < tempInt; i++)
			{
				std::cin >> tempInt2;
				*numbers = tempInt2;
				numbers++; 
			}
		}
		else if (sz - tempInt > 0 && flag == true) 
		{
			flag = false;
			numbers = numbers - (sz - tempInt);
			numbers2 = new int[sz];
			std::cout << "Created array of " << sz << " numbers\n";
			for (int i = 0; i < sz-tempInt; i++)
			{
				*numbers2 = *numbers;
				numbers2++;
				numbers++;
			}
			numbers = numbers - (sz - tempInt);
			delete [] numbers;
			for (int i = 0; i < tempInt; i++)
			{
				std::cin >> tempInt2;
				*numbers2 = tempInt2;
				numbers2++; 
			}
		}
		else if (sz - tempInt > 0 && flag == false) 
		{
			flag = false;
			if (flag2 == false)
			{
				flag2 = true;
				numbers2 = numbers2 - (sz - tempInt);
				numbers = new int[sz];
				std::cout << "Created array of " << sz << " numbers\n";
				for (int i = 0; i < sz-tempInt; i++)
				{
					*numbers = *numbers2;
					numbers2++;
					numbers++;
				}
				numbers2 = numbers2 - (sz - tempInt);
				delete [] numbers2;
				for (int i = 0; i < tempInt; i++)
				{
					std::cin >> tempInt2;
					*numbers = tempInt2;
					numbers++; 
				}
			}
			else
			{
				flag2 = false;
				numbers = numbers - (sz - tempInt);
				numbers2 = new int[sz];
				std::cout << "Created array of " << sz << " numbers\n";
				for (int i = 0; i < sz-tempInt; i++)
				{
					*numbers2 = *numbers;
					numbers2++;
					numbers++;
				}
				numbers = numbers - (sz - tempInt);
				delete [] numbers;
				for (int i = 0; i < tempInt; i++)
				{
					std::cin >> tempInt2;
					*numbers2 = tempInt2;
					numbers2++; 
				}
			}
		}
		std::cout << "Add number? (Y/n): ";
		std::cin >> tempChar;
		if (tempChar == 'y' || tempChar == 'Y')
		{
			loop = true;
		}
		else if (tempChar == 'N' || tempChar == 'n')
		{
			loop = false;
		}
	}
	if (flag == true || flag2 == true)
	{
		numbers = numbers - sz;
		for (int i = 0; i < sz; i++)
		{
			std::cout << *numbers << std::endl;
			numbers++;
		}
		numbers = numbers - sz;
		std::vector<int> v;
		v.resize(sz);
		copy(numbers, numbers+sz, v.begin());
		std::vector<int>::iterator vi;
		for (vi = v.begin(); vi != v.end(); vi++)
		{
			std::cout << *vi << std::endl;
		}
	}
	else 
	{
		numbers2 = numbers2 - sz;
		for (int i = 0; i < sz; i++)
		{
			std::cout << *numbers2 << std::endl;
			numbers2++;
		}
		numbers2 = numbers2 - sz;
		std::vector<int> v;
		v.resize(sz);
		copy(numbers2, numbers2+sz, v.begin());
		std::vector<int>::iterator vi;
		for (vi = v.begin(); vi != v.end(); vi++)
		{
			std::cout << *vi << std::endl;
		}
	}
	return 0;
}
¿why do you have so many special cases?
at the end you use std::vector, ¿what was the purpose of this program?
Just practicing data structures. Just wanted to create an array of user defined amount of numbers. It's pretty bad :(
Last edited on
Topic archived. No new replies allowed.