overloading operator + to add 2 dynamic arrays

hey guys
so am trying to study how to overload an operator but i keep getting this error
HEAP CORRUPTION DETECTED

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

class ListType
{
	int size,length,*list;
public:
	ListType(int S)
{
	if (S<0)
		S=100;
	length=0;
	size=S;
	list=new int [S];
}


~ListType()
{
	delete []list;
}


ListType operator+ (ListType obj1)
{
	int k=0;
	ListType Temp;
	delete []Temp.list;
	Temp.size=size+obj1.size;
	Temp.length=length+obj1.length;	
	Temp.list=new int [size];

	for(int i=0;i<size;i++)
		Temp.list[k++]=list[i];
	for(int i=0;i<obj1.size;i++)
		Temp.list[k++]=obj1.list[i];
return Temp;
	}
void ListType::insert(int x)
{
	if(length==size)
		cout<<"invalid, The list is full"<<endl;
	else
		list[length]=x;
	length++;
}
};
void main()
{
	ListType obj1(4),obj2(3),obj3;
	obj1.insert(6);
	obj1.insert(44);
	obj2.insert(6);
	obj3=obj1+obj2;
}





can anyone please help me??? what does that error mean??? why does it appear??
what can i do to fix this error?

thanks so much in advance ^_^
27
28
	ListType Temp;
	delete []Temp.list;
Temp was default constructed. You have not defined your default constructor to do anything, so Temp.list points to nothing, which you promptly delete[]
ooh actually the constructor is default i just forgot to put it
1
2
3
4
5
6
7
8
	ListType(int S=100)
{
	if (S<0)
		S=100;
	length=0;
	size=S;
	list=new int [S];
}



but i tried removing
delete []Temp.list;

then this message appeared
heap corruption detected



what does that mean?!?!
i googled it but didn't really understand!!
You did not define the copy assignement operator. So you have 1)leak of memory 2) heap corruption.

Here the copy assignment operator is used


obj3=obj1+obj2;
Last edited on
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
#include<iostream>
using namespace std;

class ListType
{
	int size,length,*list;
public:
	ListType(int S=100)
{
	if (S<0)
		S=100;
	length=0;
	size=S;
	list=new int [S];
}


~ListType()
{
	delete []list;
}


ListType  operator+ (ListType obj1)
{
	int k=0;
	ListType Temp;
	delete []Temp.list;
	Temp.size=size+obj1.size;
	Temp.length=length+obj1.length;	
	Temp.list=new int [Temp.size];

	for(int i=0;i<length;i++)
	Temp.list[k++]=list[i];
	for(int i=0;i<obj1.length;i++)
	Temp.list[k++]=obj1.list[i];
return Temp;
	}

ListType operator= (ListType obj)
{
	delete []list;
	size=obj.size;
	length=obj.length;
	for(int i=0;i<length;i++)
		list[i]=obj.list[i];
	
return *this;	
}
void ListType::insert(int x)
{
	if(length==size)
		cout<<"invalid, The list is full"<<endl;
	else
		list[length]=x;
	length++;
}

};
void main()
{
	ListType obj1(4),obj2(3),obj3;
	obj1.insert(6);
	obj1.insert(44);
	obj2.insert(6);
	obj3=obj1+obj2;
}



ok so i edited it
now it works fine but at the end of execution it tells me that the program stopped working O.o

why is that?!?!
Where are you coping elements of obj.list? You deleted already the original list. You shall allocate memory anew.

1
2
3
4
5
6
7
8
9
10
ListType operator= (ListType obj)
{
	delete []list;
	size=obj.size;
	length=obj.length;
	for(int i=0;i<length;i++)
		list[i]=obj.list[i];
	
return *this;	
}
thank you soooooo much it works now ^_^
Topic archived. No new replies allowed.