Resizing of array - Giving Run time error at deletion of memory

May 8, 2019 at 3:43pm
Following is my code,


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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student {
private:
	string name; 
	string roll; 
public:
	Student() {}
	void input()
	{
		cout << "Enter Name: "; 
		getline(cin, name);
		cout << "Enter Roll Number: "; 
		getline(cin, roll); 
		savestudent(); 
	}
	void savestudent()
	{
			ofstream WriteStudent;
			WriteStudent.open("Student.FILE", ios::in | ios::app);
			WriteStudent << name << "\n";
			WriteStudent << roll << "\n";
			WriteStudent.close(); 
			cout << "Success! Data has been saved!" << endl;
	}
	void print()
	{
		cout << "Name: " << name << endl
			<< "Roll Number: " << roll << endl; 
	}
	void setname(string a)
	{
		name = a;
	}
	void setroll(string a) { roll = a;  }
};
Student **p = NULL; 
int studentsize = 0;
void resize(); 
void LoadStudent(); 
int main()
{
	bool fh = true; 
	while (fh)
	{
		cout << "Pick your option" << endl
			<< "1) Add a New Student" << endl
			<< "2) Display The Students" << endl
			<< "3) Quit the Program " << endl
			<< "Enter a Number: ";
		int choice;
		cin >> choice;
		cin.ignore(); 
		switch (choice)
		{
		case 1:
			resize();
			p[studentsize - 1] = new Student;
			p[studentsize - 1]->input();
			break;
		case 2:
			LoadStudent();
			for (int i = 0; i < studentsize; i++)
				p[i]->print();
			break;
		case 3:
			fh = false;
			break;
		default:
			fh = false; 
		}
	}
	cout << endl;
	system("pause");
	return 0;
}
void resize()
{
	Student **temp = new Student*[studentsize + 1];
	for (int i = 0; i < studentsize; i++)
		temp[i] = p[i]; 
	delete[]p;
	p = temp; 
	studentsize++; 
}
void LoadStudent()
{
	int a = 0; 
	ifstream LoadStuff; 
	LoadStuff.open("Student.FILE", ios::in);
	delete[]p;
	studentsize = 0; 
	string temp; 
	while (getline(LoadStuff, temp))
	{	
		resize(); 
		p[studentsize - 1] = new Student; 
		p[studentsize - 1]->setname(temp); 
		getline(LoadStuff, temp); 
		p[studentsize - 1]->setroll(temp); 
	}
}


What I did here is make a simple Student class with two data members of string datatype.
Then I have made a double pointer which will point to an array of single pointers... This array will be resized on every new entry of student the user asks for.
I have made a function to write this data into a file (which is working fine).
I have also made a function which will read this data from a file into the same double pointer array and then display the data (this also works fine if you select this option first)


So, as a test program, I wrote this data into the file and then closed the program,
1
2
3
4
5
6
7
 
None
12F-0128
Ashas
12F-014444
Bool
12F-23232


Then, I ran the program again and this time, instead of adding a new student, I went for display data and the program read the data from the file and displayed it on console - this worked perfectly again.

However, when I tried to display this data now, it gives runtime error in the resize function (which is as follows,)
1
2
3
4
5
6
7
8
9
10
11
void resize()
{
	Student **temp = new Student*[studentsize + 1];
	for (int i = 0; i < studentsize; i++)
		temp[i] = p[i]; 
	for (int i = 0; i < studentsize; i++)
		delete[i]p;
	delete[]p;
	p = temp; 
	studentsize++; 
}


Even if I delete the for loop in it, it still gives error on "delete[]p;"

However, If remove both of these lines, and make the resize function as follows,
1
2
3
4
5
6
7
8
9
void resize()
{
	Student **temp = new Student*[studentsize + 1];
	for (int i = 0; i < studentsize; i++)
		temp[i] = p[i]; 
	
	p = temp; 
	studentsize++; 
}


Then the code works.
Now, I don't get as to why this deletion of pointer array is not working.. As far as my logic is concerned, I think the code should be working. Can anyone explain this, please? Thank you.

PLEASE NOTE: WE HAVEN'T LEARNED VECTORS YET AND THAT'S WHY WE CAN'T USE THEM. Hence, I must make this resize function for my code to properly work
May 8, 2019 at 4:13pm
you delete the same pointer twice
1
2
3
4
5
6
7
8
9
10
11
void LoadStudent()
{
	int a = 0; 
	ifstream LoadStuff; 
	LoadStuff.open("Student.FILE", ios::in);
	delete[]p; //first delete
	studentsize = 0; 
	string temp; 
	while (getline(LoadStuff, temp))
	{	
		resize(); //second delete 
the first time there is no problem because `p' is null.
Last edited on May 8, 2019 at 4:14pm
May 8, 2019 at 4:22pm
And, following the above code snip shows that your logic says
delete p
call resize which has this gem:
temp[i] = p[i]; //p be deleted here. this can't be good.
since studentsize is zero, it won't explode here. But the code is fragile. Let resize delete p, don't do it outside of it, or you may get into trouble. If you must do it outside of resize for other reasons like end of program delete, make a routine that both deletes it AND sets the size to zero to preserve integrity.
Last edited on May 8, 2019 at 4:24pm
May 8, 2019 at 5:49pm
Ok, thanks a lot, I got what I was doing wrong.
I have removed the delete[]p from the LoadStuff function.

This has fixed the runtime error on the "delete[]p" in the resize function, thankfully...

However, I am still getting error on the for loop, :/
From what I understand, it should be deleting the memory of student objects (which I have allocated dynamically) to which the pointers in the array are pointing.

Here is the updated code (which is nothing different from the main post's code except that it doesn't have a delete[]p in the LoadStudent function)

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student {
private:
	string name; 
	string roll; 
public:
	Student() {}
	void input()
	{
		cout << "Enter Name: "; 
		getline(cin, name);
		cout << "Enter Roll Number: "; 
		getline(cin, roll); 
		savestudent(); 
	}
	void savestudent()
	{
			ofstream WriteStudent;
			WriteStudent.open("Student.FILE", ios::in | ios::app);
			WriteStudent << name << "\n";
			WriteStudent << roll << "\n";
			WriteStudent.close(); 
			cout << "Success! Data has been saved!" << endl;
	}
	void print()
	{
		cout << "Name: " << name << endl
			<< "Roll Number: " << roll << endl; 
	}
	void setname(string a)
	{
		name = a;
	}
	void setroll(string a) { roll = a;  }
};
Student **p = NULL; 
int studentsize = 0;
void resize(); 
void LoadStudent(); 
int main()
{
	bool fh = true; 
	while (fh)
	{
		cout << "Pick your option" << endl
			<< "1) Add a New Student" << endl
			<< "2) Display The Students" << endl
			<< "3) Quit the Program " << endl
			<< "Enter a Number: ";
		int choice;
		cin >> choice;
		cin.ignore(); 
		switch (choice)
		{
		case 1:
			resize();
			p[studentsize - 1] = new Student;
			p[studentsize - 1]->input();
			break;
		case 2:
			LoadStudent();
			for (int i = 0; i < studentsize; i++)
				p[i]->print();
			break;
		case 3:
			fh = false;
			break;
		default:
			fh = false; 
		}
	}
	cout << endl;
	system("pause");
	return 0;
}
void resize()
{
	Student **temp = new Student*[studentsize + 1];
	for (int i = 0; i < studentsize; i++)
		temp[i] = p[i]; 
	for (int i = 0; i < studentsize; i++)
		delete[i]p; // This is still giving Run Time Error
	delete[]p; // Error fixed here. 
	p = temp; 
	studentsize++; 
}
void LoadStudent()
{
	int a = 0; 
	ifstream LoadStuff; 
	LoadStuff.open("Student.FILE", ios::in);
	studentsize = 0; 
	string temp; 
	while (getline(LoadStuff, temp))
	{	
		resize(); 
		p[studentsize - 1] = new Student; 
		p[studentsize - 1]->setname(temp); 
		getline(LoadStuff, temp); 
		p[studentsize - 1]->setroll(temp); 
	}
}
May 8, 2019 at 6:51pm
delete[i]p; // This is still giving Run Time Error

syntax problem that happened to compile. I think you want:

delete[] p[i];
Last edited on May 8, 2019 at 6:52pm
May 9, 2019 at 12:47am
> delete[] p[i];
`p[i]' does not point to memory allocated with new[]

I'm not sure what you want to do in lines 84--85, ¿do you want to kill all the students? ¿why?
May 10, 2019 at 5:11am
Oh, not only was I writing the wrong syntax but also my logic was wrong here.
Thank you jonnin and ne555, really appreciate your help ^_^
Topic archived. No new replies allowed.