Heap Corruption detected on delete [] L

I'm getting this weird error I don't know how to interpret or solve when I try to delete my dynamic array in the destructor. Can anyone help me out?

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
//List.h
class List
{
public:
	List();
	List(const List &);
	~List();

	bool IsEmpty() { return count == 0; };
	bool IsFull() { return count == size; };

	void Display() const;
	void Add(const int &);
	int Subtract(const List & L2);

	int Find(const int &);

	friend ostream & operator<<(ostream & out, const List & Org);


private:
	int *L;
	int size;
	int count;
};


//List.cpp
 List::List()
{
	cout << "Default constructor invoked" << endl;
	L = new int[size];
	size = 1;
	count = 0;
	for (int i = 0; i < size; i++)
	{
		L[i] = 0;
	}
}


List::~List()
{
	cout << "destructor invoked" << endl;
	delete[] L;
}
In the constructor you are using the size variable before it has been initialized.

You have not defined a copy assignment operator so if are assigning a List object to an existing List object in your program that is also a problem.
Last edited on
Topic archived. No new replies allowed.