Why does my program crash?

Hello all,

I'm having some trouble getting my program to run. I don't get any errors when I try to run it. It just crashes.

Anyway, I've got a class with public and private functions each with their own assigned task. I have a feeling that the problem is somewhere in the setScore function but I'm not sure. Any ideas as to why this might be happening?

Thanks!

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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

// Student class declaration
class Student
{
	private:
		string name;
		int id;
		int *test;
		int num;
		void makeArray();
	public:
		Student();
		Student(int n);
		Student(string nm, int i, int n);
		void setName(string nm);
		void setID(int i);
		void setScore(int i, int s);
		string getName() const;
		int getID() const;
		void showScore();
		void display();
		~Student();			
};

//************************************************** 
// FUNCTION DEFINITION                             *  
//**************************************************
void Student::makeArray()
{
	int size = Student::num;
	int *studentArray;
	studentArray = new int[num];
	Student::test = studentArray;

	test = 0;
}

//************************************************** 
// FUNCTION DEFINITION                             *  
//**************************************************
Student::Student()
{
	setName("NONE");
	setID(10);
	Student::num = 3;
	makeArray();

}

//************************************************** 
// FUNCTION DEFINITION                             *  
//**************************************************
Student::Student(int n)
{
	setName("None");
	setID(10);
	if(n > 0){
		Student::num = n;
	}else{
		Student::num = 3;
	}
	makeArray();
}

//************************************************** 
// FUNCTION DEFINITION                             *  
//**************************************************
Student::Student(string nm, int i, int n)
{
	setName(nm);
	setID(i);
	if(n > 0)
	{
		Student::num = n;
	}
	else
	{
		Student::num = 3;
	}
	makeArray();
}

//************************************************** 
// FUNCTION DEFINITION                             *  
//**************************************************
void Student::setName(string nm)
{
	Student::name = nm;
}

//************************************************** 
// FUNCTION DEFINITION                             *  
//**************************************************
void Student::setID(int i)
{
	if(i >= 10 && i <= 99)
	{
		Student::id = i;
	}
	else
	{
		Student::id = 10;
		cout << "Invalid.  Can not set id to " << i << " for " << getName() << endl;
	}
}

//************************************************** 
// FUNCTION DEFINITION                             *  
//**************************************************
void Student::setScore(int i, int s)
{
	if(i < Student::num)
	{
		if(s >= 0 && s <= 100)
		{
			test[i] = s;
		}
		else
		{
			cout << "Invalid.  Can not set test " << i << " to " << s << " for " << getName() << endl;
		}
	}
	else
	{
		cout << "Invalid.  Can not set test " << i << " to " << s << " for " << getName() << endl;
	}
}

//************************************************** 
// FUNCTION DEFINITION                             *  
//**************************************************
string Student::getName() const
{
	return Student::name;
}

//************************************************** 
// FUNCTION DEFINITION                             *  
//**************************************************
int Student::getID() const
{
	return Student::id;
}

//************************************************** 
// FUNCTION DEFINITION                             *  
//**************************************************
void Student::showScore()
{
	for(int count = 0; count < Student::num; count++)
	{
		cout << "Test " << count << "had a score of " << test[count] << endl;
	}
}

//************************************************** 
// FUNCTION DEFINITION                             *  
//**************************************************
void Student::display()
{
	cout << "The Name: " << getName();
	cout << "The ID: " << getID();
	showScore();
	cout << endl;
	cout << endl;
}

//************************************************** 
// FUNCTION DEFINITION                             *  
//**************************************************
Student::~Student()
{
	free(test);
}

int main()
{
	Student a;
	Student b(4);
	Student c("Joe", 40, 5);

	cout << "Calling the set functions";

	a.setName("Tom");
	a.setID(200);
	a.setID(20);
	a.setScore(0, 75);
	a.setScore(1, 85);
	a.setScore(2, 95);

	b.setName("John");
	b.setID(30);
	b.setScore(0, 70);
	b.setScore(1, 80);
	b.setScore(2, 90);
	b.setScore(3, 100);

	c.setScore(0, 90);
	c.setScore(1, 91);
	c.setScore(2, 92);
	c.setScore(3, 93);
	c.setScore(4, 94);
	c.setScore(5, 95);
	c.setScore(4, 105);
	c.setScore(5, 105);

	a.display();
	b.display();
	c.display();

	system("pause");
	return 0;
}
> I don't get any errors when I try to run it. It just crashes.
Unless the crash is intended, I'll say that's an error.

$ gdb a.out

Program received signal SIGSEGV, Segmentation fault.
0x00000000004010fb in Student::setScore (this=0x7fffffffe250, i=0, s=75) at foo.cpp:121
121                             test[i] = s;

(gdb) p test
$1 = (int *) 0x0
Does that mean that i is outside the acceptable range of indexes? How would I fix this problem?
Keep reading, `test' is NULL.

1
2
3
4
5
6
7
8
9
void Student::makeArray()
{
	int size = Student::num;
	int *studentArray;
	studentArray = new int[num];
	Student::test = studentArray;

	test = 0; //¿?
}
By the way, you must deallocate with delete[] if you allocate with new[]
Topic archived. No new replies allowed.