Bad pointer error in linked list

I'm learning linked list and I have a problem with this code.
If I write line "delete add;" (***) in this code, the programme complains " Bad pointer error" with the pointer curr->name, but if i delete it, everything is ok, so do I have to delete add ?, if yes, how can I delete it?
thanks so much.

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

int count=0;
class List
{
public:
	List();
	void addNew();
	void del();
	void print();
private:
	struct student 
	{     
		int ID;
		char name[256];     
		student *next ;
	};  
	student* head;
	student* curr;
	student* temp;

};
 
List::List()
{
	head=NULL;
	curr=NULL;
	temp=NULL;
}

void List::addNew()//=============add a new student=============
{
	student* add=new student;
	add->next=NULL;
	cout << "\nStudent number "<< count+1<<endl ;
	cout << "Name : " ;cin.getline(add->name,256); 
	cout << "ID= " ; fflush(stdin); cin >> add->ID ; cin.ignore() ; 
	count++;
	if (head!=NULL)
	{
		curr=head;
		while (curr->next!=NULL)
		{
			curr=curr->next;
		}
		curr->next=add;
	}
	else
	{
		head=add;
	}
	//delete add; ***<=====this line
}

void List::del()//==================delete a student==========================
{
	int _del;
	cout<<"Delete ID=";
	fflush(stdin);
	cin>>_del;
	student* delPrt=NULL;
	temp=head;
	curr=head;
	while (curr!=NULL&&curr->ID!=_del)
	{
		curr=curr->next;
	}
	if (curr==NULL)
	{
		cout<<"Data doesn't exist!"<<endl;
		delete delPrt;
	}
	else
	{
		delPrt=curr;
		if (delPrt==head)
		{
			head=head->next;
			temp=NULL;
			
		}
		else
		{
			curr=curr->next;
			temp->next=curr;
                        delete delPrt; 
		}
		
		
	}
	
}
void List::print()//=============print the list=============================
{
	curr=head;
	while (curr!=NULL)
	{
		cout<<curr->ID<<"\t"<<curr->name<<endl;
		curr=curr->next;
	}
}
int main()
{
	List ex;
	for (int i=0;i<2;i++)
	{
		ex.addNew();
	}
	ex.print();
	ex.del();
	ex.print();
	system("pause");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
student * head = 0;

student * add = new student;
// there is now a student and 'add' knows where it is

head = add;
// now 'head' knows the location of that student too

delete add; // the student is no more
// the 'add' still has an address pointing to deallocated memory
// the 'head' still has an address pointing to deallocated memory

dereference head // kaboom 

In other words, do you really want to erase the element of the list that you have just created?
I think that I use student * add = new student; so I have to use delete add;
So I don't need to delete? Does the leaked memory occur?
Yes, there is a leak when 'ex' goes out of scope at the end of main(). It is the duty of the destructor of a List object to appropriately deallocate the resources that the object manages.

Your List::del() doesn't look flawless either.
Topic archived. No new replies allowed.