fatal error condition: SIGSEGV - Segmentation violation signal

Hello I need help figuring out why I get SIGSEGV error.

My classroom.cpp utilizes my UniqueVector class as the underlying data structure, and my UniqueVector passed all the test cases, but I can't get my ClassroomTester to run because it gives me an error.

Classroom.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef CLASSROOM_H
#define CLASSROOM_H
#include "UniqueVector.h"

class Classroom{
	
	public:
		Classroom(){};
		bool addStudent(const string& name);
		bool removeStudent(const string& name);
		bool containsStudent(const string& name);
		string listAllStudents();
		
	private:
		UniqueVector<string>classRoster;
};

#endif 


Classroom.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
#ifndef CLASSROOM_CPP
#define CLASSROOM_CPP
#include "Classroom.h"
#include <iostream>
#include <string>
using namespace std;

bool Classroom::addStudent(const string& name){
	
	return classRoster.insert(name);		
}

bool Classroom::removeStudent(const string& name){

	return classRoster.remove(name);		
}

bool Classroom::containsStudent(const string& name){
	
	return classRoster.contains(name);
}

string Classroom::listAllStudents(){

	string studentName = "";

	for(int index=0; index < classRoster.size(); index++){
		studentName += classRoster.at(index,studentName);
			if(index!=classRoster.size()-1){
				studentName += ",";
			}
		}
		

	return studentName;
}

#endif 


ClassroomTester.cpp

1
2
3
4
5
6
7
#include <iostream>
#include <string>
#include "Classroom.h"
#include "catch.h"

TEST_CASE("adding/removing/containment of students preserves uniqueness","[student]"){
	Classroom myClass; //This line gives the error 



My Classroom.cpp uses my UniqueVector.cpp as the underlying data structure

UniqueVector.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
#ifndef UNIQUE_VECTOR_CPP
#define UNIQUE_VECTOR_CPP
#include "UniqueVector.h"
#include <iostream>
using namespace std;
//Default constructor
template<typename T>
UniqueVector<T>::UniqueVector(): arrayCapacity(3), array(new T[arrayCapacity]), arraySize(0){
	//blank
}
//Function to double to capacity in temp then delete initial array
//and point array to temp
template<typename T>
void UniqueVector<T>::doubleCapacity(){
		//Create temp array with double the capacity
		//delete array in heap and points array to temp
		arrayCapacity*=2;
		T* temp = new T[arrayCapacity];
		for(int index=0; index<arraySize; index++){
			temp[index] = array[index];
		}
		
		delete [] array;
		array = temp;
}
//Returns capacity of the array
template<typename T>
unsigned int UniqueVector<T>::capacity(){
	
	return arrayCapacity;
}
//returns size of the array
template<typename T>
unsigned int UniqueVector<T>::size() const{

	return arraySize;
}
//Checks if size is 0
template<typename T>
bool UniqueVector<T>::empty(){

	if(arraySize==0){
		return true;
	}
	
	return false;
}

template<typename T>
bool UniqueVector<T>::contains(const T& data){
	//loops through array to check if the index has element data
	for(int index=0; index < arraySize; index++){
		if(array[index] == data){
			return true;
		}
	}
	
	return false;
}

template<typename T>
bool UniqueVector<T>::at(unsigned int pos, T& data){
	//If position is less than the size of the array
	//stores the element of the array position into data
	if(pos < arraySize){
		data = array[pos];
		return true;
	}
	return false;
}

template<typename T>
bool UniqueVector<T>::insert(const T& data){
	//insert data to the first pos
	if(arraySize==0){
		arraySize++;
		array[0] = data;
		return true;
	}	
	//return false if index contains data
	for(int index=0; index < arraySize; index++){
		if(array[index] == data){
			return false;
		}
	}
	//if size of array is equal to the capacity size, double capacity size
	if(arraySize == arrayCapacity){
		doubleCapacity();
	}
	//The last index of the array now contains data
	array[arraySize] = data;
	arraySize++;
	return true;
}

template<typename T>
bool UniqueVector<T>::insert(const T& data, unsigned int pos){
	
	//Checks if the position is valid
	if(pos>arraySize){
		return false;
	}
	//insert data into the 1st position
	if(arraySize==0){
		arraySize++;
		array[0] = data;
		return true;
	}
	//return false if data is in array
	for(int index=0; index < arraySize; index++){
		if(array[index] == data){
			return false;
		}
	}
	//if size of array is equal to the capacity size, double capacity size
	if(arraySize == arrayCapacity){
		doubleCapacity();
	}
	//Shifts the element of the index right by 1
	arraySize++;  
	for(int index=arraySize-1; index>pos; index=index-1){
            	array[index] = array[index-1];
            }
			 
    array[pos] = data;
    return true;
}

template<typename T>
bool UniqueVector<T>::push_front(const T& data){
	//If the array size is 0, put data into the first position
	if(arraySize==0){
		array[0] = data;
		arraySize++;
		return true;	
	}	
	//If data is in the index return false
	for(int index=0; index < arraySize; index++){
		if(array[index] == data)
			return false;
	}
	//if size of array is equal to the capacity size, double capacity size
	if(arraySize == arrayCapacity){
		doubleCapacity();
	}
	//Shifts element of the index right by 1
	arraySize++;
	for(int index=arraySize-1; index>0; index=index-1){
            array[index] = array[index-1];
    }

	array[0] = data;
	return true;
}

template<typename T>
bool UniqueVector<T>::remove(const T& data){
	//Makes sure the array size is not 0
	if(arraySize==0){
		return false;
	}
	//check if the element of index is the same as data, then shift
	//elements starting from index to are shifted left by 1 and decrease size
	for(int index=0; index < arraySize; index++){
		if(array[index] == data){
			//Shifts element of the array to the right
			for(int index2=index; index2<arraySize-1; index2++)
				array[index2] = array[index2+1];
			arraySize--;
			return true;
			}
	}
	
	return false;
}

template<typename T>
bool UniqueVector<T>::remove(unsigned int pos, T& data){
	//if position is greater than size return false
	if(pos>arraySize){
		return false;
	}
	//If size of array is 0 return false
	if(arraySize==0){
		return false;
	}
	//the element in position is stored in data
	data = array[pos];
		//overwrite element of the array by shifting by 1,
		// where position is the first index to be overwritten
	for(int index=pos; index<arraySize-1; index++){
		array[index] = array[index+1];	
	}
	
	arraySize--;
	return true;
}

template<typename T>
bool UniqueVector<T>::pop_back(T& data){	
	//if the array is not empty the last element is store in data
	//then decease size by 1 and return true
	if(empty()!=true){
		data = array[arraySize-1];
		arraySize--;
		return true;
	}
	
	return false;
}

template<typename T>
void UniqueVector<T>::clear(){
	//sets array capacity to 3 and size to 1, makes a new temp array with the capacity
	//delete the old array and points the array to temp
	arrayCapacity =3;
	T* temp = new T[arrayCapacity];
	arraySize=0;
	delete [] array;
	array = temp;
}

template<typename T2>
bool UniqueVector<T2>::operator==( const UniqueVector<T2> &rhs) const{
	//If the array size is equivalent to the other vector's array size
	//return true
	if(arraySize == rhs.size()){
		return true;
	}
	
	return false;
}

#endif 


Anyone have any idea why I'm getting an error?
Last edited on
classRoster.at(index,studentName);

Should be :
classRoster.at(index);
Last edited on
Oh let me clarify, I used my own UniqueVector as the under lying data structure, so the functions are different. Edit'd my post.
As I asked in a previous thread, have you implemented a proper copy constructor and copy assignment operator for your UniqueVector type?
I think I might have figured out my error. I might have been an out of bound error for my

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
 
template<typename T>
bool UniqueVector<T>::remove(const T& data){
	//Makes sure the array size is not 0
	if(arraySize==0){
		return false;
	}
	//check if the element of index is the same as data, then shift
	//elements starting from index to are shifted left by 1 and decrease size
	for(int index=0; index < arraySize; index++){
		if(array[index] == data){
			//Shifts element of the array to the right
			for(int index2=index; index2<arraySize-1; index2++)
				array[index2] = array[index2+1];
			arraySize--;
			return true;
			}
	}
	
	return false;
}

template<typename T>
bool UniqueVector<T>::remove(unsigned int pos, T& data){
	//if position is greater than size return false
	if(pos>arraySize){
		return false;
	}
	//If size of array is 0 return false
	if(arraySize==0){
		return false;
	}
	//the element in position is stored in data
	data = array[pos];
		//overwrite element of the array by shifting by 1,
		// where position is the first index to be overwritten
	for(int index=pos; index<arraySize-1; index++){
		array[index] = array[index+1];	
	}
	
	arraySize--;
	return true;
}


I have to set
1
2
3
4
 if(arraySize==1){
           arraySize--;
           return true;
}


Inside the function, because index2 @ 1 will force the for loop to compute
for(int index2=0; index2<0; index2++)
Last edited on
Inside the function, because index2 @ 1 will force the for loop to compute
for(int index2=0; index2<0; index2++)


It seems unlikely that a for loop not executing is the source of your memory corruption.
Topic archived. No new replies allowed.