Run time errors

After compiling my big integer class, using list base description, without any error, then I run the program and have the following error:


Enter the first Integer
19
Enter the second Integer
2
Enter 1 to add these two Integers
Enter 2 to multiply the Integers
Enter 3 to stop
2
The multiplication is 
I'm in copy
I'm in the while loop
I'm out of copy
*** glibc detected *** ./test: double free or corruption (fasttop): 0x09b7e038 ***
======= Backtrace: =========
/lib/libc.so.6[0x5dfac1]
/lib/libc.so.6(cfree+0x90)[0x5e30f0]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x5dc46f1]
./test[0x804917f]
./test[0x80491b9]
./test[0x80497a9]
./test(__gxx_personality_v0+0x38e)[0x8048aea]
/lib/libc.so.6(__libc_start_main+0xe0)[0x58c390]
./test(__gxx_personality_v0+0x45)[0x80487a1]
======= Memory map: ========
00110000-00111000 r-xp 00110000 00:00 0          [vdso]
00557000-00572000 r-xp 00000000 fd:00 49347      /lib/ld-2.7.so
00572000-00573000 r--p 0001a000 fd:00 49347      /lib/ld-2.7.so
00573000-00574000 rw-p 0001b000 fd:00 49347      /lib/ld-2.7.so
00576000-006c9000 r-xp 00000000 fd:00 49348      /lib/libc-2.7.so
006c9000-006cb000 r--p 00153000 fd:00 49348      /lib/libc-2.7.so
006cb000-006cc000 rw-p 00155000 fd:00 49348      /lib/libc-2.7.so
006cc000-006cf000 rw-p 006cc000 00:00 0 
006d8000-006ff000 r-xp 00000000 fd:00 49350      /lib/libm-2.7.so
006ff000-00700000 r--p 00026000 fd:00 49350      /lib/libm-2.7.so
00700000-00701000 rw-p 00027000 fd:00 49350      /lib/libm-2.7.so
05d11000-05df1000 r-xp 00000000 fd:00 95543      /usr/lib/libstdc++.so.6.0.8
05df1000-05df5000 r--p 000df000 fd:00 95543      /usr/lib/libstdc++.so.6.0.8
05df5000-05df6000 rw-p 000e3000 fd:00 95543      /usr/lib/libstdc++.so.6.0.8
05df6000-05dfc000 rw-p 05df6000 00:00 0 
073a3000-073ae000 r-xp 00000000 fd:00 49351      /lib/libgcc_s-4.1.2-20070925.so.1
073ae000-073af000 rw-p 0000a000 fd:00 49351      /lib/libgcc_s-4.1.2-20070925.so.1
08048000-0804a000 r-xp 00000000 fd:00 2031743    /home/mojeboski/Documents/programming/cplusplus/ninteger/test
0804a000-0804b000 rw-p 00002000 fd:00 2031743    /home/mojeboski/Documents/programming/cplusplus/ninteger/test
09b7e000-09b9f000 rw-p 09b7e000 00:00 0          [heap]
b7e00000-b7e21000 rw-p b7e00000 00:00 0 
b7e21000-b7f00000 ---p b7e21000 00:00 0 
b7f90000-b7f92000 rw-p b7f90000 00:00 0 
b7fa6000-b7fa8000 rw-p b7fa6000 00:00 0 
bfa93000-bfaa8000 rw-p bffeb000 00:00 0          [stack]
Aborted


I had used couple of cout statements and realized that the error possibly occured in copy_nint function or during a call to the destructor of returned nint intB value. I have attached the parts of the code as below:

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
//the destructor
nint::~nint(){
  //calling the cleannode function;
  clear_nint();
}//end of destructor;

//copy function does copying when called
void nint::copy_nint(nint intA){
  cout<<"I'm in copy"<<endl;
  head = NULL;
  moving = NULL;
  intA.moving = intA.head;
  while (intA.moving != NULL) {
    //copying to the first node;
    if(head == NULL){
      head = new listnode;
      head->item = intA.head->item;
      head->next = NULL;
      moving = head;
    }//end of if;
    //copying into the other nodes;
    else{
      moving->next = new listnode;
      moving = moving->next;
      moving->item = intA.moving->item;
      moving->next = NULL;
    }//end of else if;
    listlen++;
    intA.moving = intA.moving->next; //intA.moving moved to the next node;
    cout<<"I'm in the while loop"<<endl;
  }//end of while;
  cout<<"I'm out of copy"<<endl;
}

void nint::clear_nint(){
  while(head != NULL){
    moving = head;
    while(moving->next != NULL){
      moving = moving->next;
    }
    delete moving;
  }
  moving = NULL;
  head = NULL;
  listlen = 0;
}

I will appreciate if you can help me asap.
Topic archived. No new replies allowed.