double deletion problem

I have a class call Roster which consists name of the course and instructor and a pointer points to a array of pointers to Student objects (Student** slist) as private data. In a member function called delstudent() which delete students object by pointers. The program compiles and runs, but throw me a double deletion messenge whenever I try to delete a student. Can anybody take a look of my delstudent() function code, point to me where I did wrong? Thank you.


Here is the whole program of mine: http://www.mediafire.com/?jvdmxam3xz3

platform: Mac, xcode

problem occurs at the excution of the: delete slist[i] in delstudent() function.

Here is the error messenger I get:

hw6(370) malloc: *** error for object 0x7fff5fbff770: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal: “SIGABRT”.


Here is my private data, constructor, addstudent(Student* s) function, delstudent() function and destructor

1
2
3
4
5
6
7
8
9
10
private:
	string courseN;
	string instrN;
	int courseCode;
	int credits;
	int size;   //keep track of the size of the array of pointers
	int capacity;
	void sort();
	void grow();
	Student** slist;  //a list of pointers point to Student object who take a certian class 


1
2
3
4
5
6
7
8
9
10
Roster::Roster(string cn, int cc, int cs, string instructor)
{
	courseN=cn;
	courseCode=cc;
	credits=cs;
	instrN=instructor;
	capacity=20;
	size=0;
	slist=new Student*[capacity];
}


1
2
3
4
5
6
7
8
9
void Roster::addstudent(Student* s)
{
	if (size==capacity) 
		grow();
	
	slist[size++]=s;
	slist[size]=NULL;
	sort();
}



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
void Roster::delstudent()
{
	string first, last;
	
	cout << "Which student do u want to delete?\n";
	cout << "Please Enter the last name of this student\n";
	cin>>last;
	//last[0]=toupper(last[0]);
	
	cout << "Enter the first name of this student.\n";
	cin>>first;
	//first[0]=toupper(first[0]);
		
	
	for (int i=0; i < size; i++)
	{
		if( slist[i]->getlname()==last && slist[i]->getfname()==first )
		{

			delete slist[i];    //if found, delete the memory allocation pointed by this pointer
			
			for (int j=i; j < size; j++)
			{
				
				slist[j] = slist[j+1];   //after deletion, points to the next pointer points
			}
			
			size--;
			return;
		}
	}
			
	cout << "Student not found!!!\n";
	cout <<"Programme will terminate now\n";
	exit(0);

}


1
2
3
4
5
6
7
Roster::~Roster()
{

		for(int i=0; i<size; i++)
			delete slist[i];
		delete [] slist;
}
Last edited on
You should run the code through a debugger and see what conditions are causing the error.
delete slist[i]; //if found, delete the memory allocation pointed by this pointer

it occurs when trying to delete the memory location pointed by slist[i];
What do slist, size, and i look like at this point? Clearly slist[i] is not being allocated by new, or has already been deleted previously. I assume this doesn't occur on the first attempt to delete?
it does occur on the first attempt to delete
What do slist, size and i look at that point?
I input 2 students, so the size is 2, slist[0] points to the first student object, slist[1] points to second student object, slist[2] points to NULL, and try to delete the second one(slist[1]).

when the names checks, at this point i=1, size=2, slist[1] is to be deleted, at this point I placed a break-point just before the deletion, everything is fine, when comes to the deletion statement, it throws me a double deletion messenger.
Last edited on
You are allocating the Student* you pass to addstudent() with new, correct?
as a pointer.

int main()
{
Student s1, s2;

s1.input();
s2.input();

Student *st1, *st2;

st1=&s1;
st2=&s2;

Roster r;

r.addstudent(st1);
r.addstudent(st2);
r.delstudent();

return 0;

}
Last edited on
Is that your exact code? If so, I don't see you actually adding the students with addstudent(). And also, those are statically allocated variables so if you pass their address then try to delete them, it will fail as you are not allowed to delete a variable allocated on the stack.
then how do I resolve the problem if I still want to add delete students by pointers?

If I want to add students then in the main the students has to be in a dynamic array?
You have to new them, then add them.

Rather than:
1
2
3
Student s1;
//...
r.addstudent(&s1);


You have to use:
1
2
3
Student* s1 = new Student;
//...
r.addstudent(s1);
Thank you very much~!

I get the picture now.
Topic archived. No new replies allowed.