Thread: *** glibc detected *** ./a.out: double free or corruption

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    1

    *** glibc detected *** ./a.out: double free or corruption

    Anybody pls help me fix this....I get the following error message when I execute the program given below.

    Thanks in advance.
    The Error message is :

    *** glibc detected *** ./a.out: double free or corruption (top): 0x09182008 ***
    ======= Backtrace: =========
    /lib/libc.so.6[0x69ea68]
    /lib/libc.so.6(__libc_free+0x78)[0x6a1f6f]
    /lib/libc.so.6(fclose+0x12d)[0x68ef6d]
    ./a.out[0x80486d2]
    /lib/libc.so.6(__libc_start_main+0xdc)[0x6504e4]
    ./a.out[0x80485a1]
    ======= Memory map: ========
    0012e000-0012f000 r-xp 0012e000 00:00 0 [vdso]
    0061e000-00637000 r-xp 00000000 16:01 454020 /lib/ld-2.4.so
    00637000-00638000 r-xp 00018000 16:01 454020 /lib/ld-2.4.so
    00638000-00639000 rwxp 00019000 16:01 454020 /lib/ld-2.4.so
    0063b000-00768000 r-xp 00000000 16:01 454051 /lib/libc-2.4.so
    00768000-0076a000 r-xp 0012d000 16:01 454051 /lib/libc-2.4.so
    0076a000-0076b000 rwxp 0012f000 16:01 454051 /lib/libc-2.4.so
    0076b000-0076e000 rwxp 0076b000 00:00 0
    00af0000-00afb000 r-xp 00000000 16:01 454023 /lib/libgcc_s-4.1.1-20070108.so .1
    00afb000-00afc000 rwxp 0000a000 16:01 454023 /lib/libgcc_s-4.1.1-20070108.so .1
    08048000-0804a000 r-xp 00000000 00:11 373199 /home2/shakir/Templates/cprogra ms/new/phonebook/late/new/a.out
    0804a000-0804b000 rw-p 00001000 00:11 373199 /home2/shakir/Templates/cprogra ms/new/phonebook/late/new/a.out
    09182000-091a3000 rw-p 09182000 00:00 0
    b7e00000-b7e21000 rw-p b7e00000 00:00 0
    b7e21000-b7f00000 ---p b7e21000 00:00 0
    b7fb8000-b7fb9000 rw-p b7fb8000 00:00 0
    b7fc9000-b7fca000 rw-p b7fc9000 00:00 0
    bfd65000-bfd7a000 rw-p bfd65000 00:00 0 [stack]
    Aborted


    My program is :
    Code:
     #include<stdio.h>
       #include<string.h>
    
       FILE *f1;
       struct addr
        {
         char name[15];
         double number;
        }rec[100];
    
       main()
      {
       int select,i=0,max=0,contnt=1;
       int smatch[15],snum;
       char sname[15];
       int search(char *,int *,int);
       void writeto(int),readall(int);
       void rmv(char *,int *,int);
       if((f1=fopen("book","r")) == NULL)
        contnt=0;
       else
        for(i=0;!feof(f1);i++) {
          fscanf(f1,"%s%lf",rec[i].name,&rec[i].number);
          fclose(f1);   }
       max=i;
       printf("The value of max is %d\n",max);
       printf("1 Add an Entry\n2 Remove an Entry\n3 Search by name\n4 Read all\n0 Quit\n");
       printf("Please select an option : ");
       scanf("%d",&select);
       switch(select)
        {
         case 0:
                printf("\nGood bye !!\n\n");
                exit(0);
                break;
         case 1:
                writeto(max);
                break;
         case 2:
                if(contnt != 0)
                 {
                   printf("Please Enter the name : ");
                   scanf("%s",sname);
                   rmv(sname,smatch,max);
                   printf("The entry has been removed\n");
                 }
                else
                 printf("There is nothing to remove\n");
                break;
         case 3:
                if(contnt != 0)
                 {
                   printf("Please Enter the name : ");
                   scanf("%s",sname);
                   snum=search(sname,smatch,max);
                   if(snum>0)
                    {
                     printf("The matching entries are \n");
                     for(i=0;i<snum;i++)
                     {
                      fscanf(f1,"%s%lf",rec[smatch[i]].name,&rec[smatch[i]].number);
                      fprintf(stdout,"%s   %.0lf\n",rec[smatch[i]].name,rec[smatch[i]].number);
                     }
                    }
                 else
                   printf("Could not find any matching Entries\n");
                 }
                else
                  printf("The Address book contains no entries\n");
                break;
         case 4:
                if(contnt != 0)
                  readall(max);
                else
                  printf("The file contains no entries, Please enter first\n");
                break;
         default:
                 printf("Please Enter the correct code\n");
        }
      }
    
       void writeto(int lim)
      {
       int i,c=1;
       printf("Enter the name and number\n");
       f1=fopen("book","a");
       for(i=lim;i< 100 && c != 0;i++)
        {
         fscanf(stdin,"%s%lf",rec[i].name,&rec[i].number);
         fprintf(f1,"%s %.0lf",rec[i].name,rec[i].number);
         printf("Any more : (press any key to continue or 0 to quit) : ");
         scanf("%d",&c);
        }
       fclose(f1);
      }
    
    
       void rmv(char find[],int count[],int lim)
      {
       int i,j,send=0,k;
       struct addr cpy[lim];
       for(i=0;i<lim;i++)
        {
         if((k=(strcmp(rec[i].name,find))) == 0)
           {   count[send]=i;
               send++;      }
        }
       f1=fopen("book","w");
       k=0;
       for(i=0;i<lim;i++)
        if(i != count[k])
         fprintf(f1,"%s  %.0lf",rec[i].name,rec[i].number);
        else
         k++;
       fclose(f1);
      }
    
       void readall(int lim)
      {
         int i,j;
         double number;
         char sml[15];
         f1=fopen("book","r");
         printf("Total Number of Entries : %d\n",lim);
         for(i=0;i<lim;i++)
          fscanf(f1,"%s%lf",rec[i].name,&rec[i].number);
         for(i=0;i<lim;i++)
          for(j=i;j<lim;j++)
           if(strcmp(rec[i].name,rec[j].name)>0)
            {
             strcpy(sml,rec[j].name);
             number=rec[j].number;
             strcpy(rec[j].name,rec[i].name);
             rec[j].number=rec[i].number;
             strcpy(rec[i].name,sml);
             rec[i].number=number;
            }
         for(i=0;i<lim;i++)
          printf("%s     %.0lf\n",rec[i].name,rec[i].number);
         fclose(f1);
    
      }
    
       int search(char find[],int count[],int lim)
      {
       int i,send=0,k;
       for(i=0;i<lim;i++)
        {
         fscanf(f1,"%s%lf",rec[i].name,&rec[i].number);
         if((k=(strcmp(rec[i].name,find))) == 0)
           {   count[send]=i;
               send++;      }
        }
       return(send);
      }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    for(i=0;!feof(f1);i++) {
          fscanf(f1,"&#37;s%lf",rec[i].name,&rec[i].number);
          fclose(f1);   }
    do not use feof to control loop - read FAQ
    if you close file after the first read - second will definitely fail
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  2. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  3. *** glibc detected *** free(): invalid next size
    By icebabe in forum C Programming
    Replies: 2
    Last Post: 05-24-2006, 12:09 PM
  4. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  5. *** glibc detected *** double free or corruption
    By 3saul in forum C Programming
    Replies: 8
    Last Post: 02-06-2006, 12:26 PM