Here is the code..
#include <iostream>
using namespace std;
class LinkedList
{
struct node
{
int data;
node *next;
};
node* header;
public:
LinkedList():header(NULL) {}
LinkedList(const LinkedList &cp)
{
node *t = cp.header, *t2;
header = NULL;
while(t != NULL)
{
t2 = new node;
t2->data = t->data;
t2->next = header;
header = t2;
t = t->next;
}
}
LinkedList& operator=(const LinkedList &cp)
{
if(&cp == this)
return *this;
//LinkedList();
node *t = cp.header, *t2;
header = NULL;
while(t != NULL)
{
t2 = new node;
t2->data = t->data;
t2->next = header;
header = t2;
t = t->next;
}
return *this;
}
~LinkedList()
{
node * t;
while(header != NULL)
{
t = header;
header = header->next;
delete t;
}
header = NULL;
}
void push(int data)
{
node *t = new node;
t->next = header;
t->data = data;
header = t;
}
void printList()
{
node *t = header;
while(t != NULL)
{
cout<<t->data<<"->";
t = t->next;
}
cout<<endl;
}
};
int main()
{
LinkedList list1;
list1.push(10);
list1.push(30);
list1.push(12);
list1.push(19);
list1.push(78);
cout<<"List1 : ";
list1.printList();
LinkedList list2;
list2 = list1;
list2 = list1;
cout<<"List2 : ";
list2.printList();
return 0;
}
Here is how DrMemory screams at me..
Dr. Memory version 1.4.6 build 2 built on Mar 7 2012 10:14:04
Application cmdline: ""C:\web\LoopLinkedList\bin\Debug\LoopLinkedList.exe""
Recorded 62 suppression(s) from default C:\Program Files\Dr. Memory/bin/suppress-default.txt
Error #1: UNINITIALIZED READ: reading 0x0022fd38-0x0022fd3c 4 byte(s)
# 0 ntdll.dll!TpDisassociateCallback +0x382 (0x7730f641 <ntdll.dll+0x2f641>)
# 1 ntdll.dll!TpCheckTerminateWorker +0x11 (0x7733b9ae <ntdll.dll+0x5b9ae>)
# 2 KERNELBASE.dll!TerminateThread +0x50 (0x7561dc35 <KERNELBASE.dll+0xdc35>)
# 3 profile_ctl
# 4 profil
# 5 moncontrol
# 6 _mcleanup
# 7 msvcrt.dll!isspace
# 8 msvcrt.dll!cexit
# 9 mainCRTStartup
#10 KERNEL32.dll!BaseThreadInitThunk
#11 ntdll.dll!RtlInitializeExceptionChain
Note: @0:00:01.789 in thread 4812
Note: instruction: cmp 0xfffffff8(%ebp) %ebx
Error #2: LEAK 8 direct bytes 0x003e14d8-0x003e14e0 + 32 indirect bytes
# 0 operator new()
# 1 operator new()
# 2 LinkedList:
perator=() [C:/web/LoopLinkedList/main.cpp:40]
# 3 main [C:/web/LoopLinkedList/main.cpp:93]
DUPLICATE ERROR COUNTS:
SUPPRESSIONS USED:
ERRORS FOUND:
0 unique, 0 total unaddressable access(es)
1 unique, 1 total uninitialized access(es)
0 unique, 0 total invalid heap argument(s)
0 unique, 0 total warning(s)
1 unique, 1 total, 40 byte(s) of leak(s)
0 unique, 0 total, 0 byte(s) of possible leak(s)
ERRORS IGNORED:
68 still-reachable allocation(s)
(re-run with "-show_reachable" for details)
Details: C:\Users\asit\AppData\Roaming/Dr. Memory/DrMemory-LoopLinkedList.exe.1280.000/results.txt
Please help me to resolve this.