*** glibc detected *** ./a.out: corrupted double-linked list: 0x0000000000a5e240 ***

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deepikarc14
    New Member
    • Jul 2010
    • 1

    #1

    *** glibc detected *** ./a.out: corrupted double-linked list: 0x0000000000a5e240 ***

    Hi i am Deepika. am new to this forum. I am working on ubuntu 10.04 LTS and using C lang. to write prgrms.
    I am trying to read strings from a file where i am using malloc concept to declare and initialize string arrays. After execution i get the above mentioned error. I don't know where i am going wrong and i don't know how to solve the problem. Help would be greatly appreciated. My program code(C language) is as follows :


    Code:
    #include<stdio.h>
    #include<string.h>
    #include<malloc.h>
    int main()
    {
    char **a, **b, str1[10],str2[10],s[80];
    int i=0,nl=0;
    FILE *fp;
    
    fp=fopen("abc.c","r"); 
    
    while(fgets(s,79,fp)!=NULL)               //Counts the no. of lines in a file
           nl++;
    printf("\n The no. of lines are : %d ",nl); 
    
    rewind(fp);
    a=(char **)malloc(nl);
    if(a==NULL)
      {
        printf("\n Not enough memory ");
        return 0;
      }
    b=(char **)malloc(nl);
    if(b==NULL)
      {
        printf("\n Not enough memory ");
        return 0;
      }
    
    for(i=0;i<nl;i++)
      {
        fscanf(fp,"%s%s",str1,str2);
      
        a[i]=(char *)malloc(strlen(str1)+1);
        b[i]=(char *)malloc(strlen(str2)+1);
        strcpy(a[i],str1);
        strcpy(b[i],str2);
        
      }
    
    fclose(fp);
    
    printf("\n");
    for(i=0;i<nl;i++)
     {
       printf("\n%s\t\t%s",a[i],b[i]);
     }
    
    for(i=0;i<nl;i++)
     {
        free(a[i]);
        free(b[i]);
     }
    free(a);
    free(b);         
    
    printf("\n");
    return 0;
    }
    After execution the output displays :

    *** glibc detected *** ./a.out: corrupted double-linked list: 0x000000000187d 240 ***
    ======= Backtrace: =========
    /lib/libc.so.6(+0x77 5b6)[0x7ff28bc005b6]
    /lib/libc.so.6(+0x7a a25)[0x7ff28bc03a25]
    /lib/libc.so.6(cfree +0x73)[0x7ff28bc06e53]
    /lib/libc.so.6(fclos e+0x14d)[0x7ff28bbf0abd]
    ./a.out[0x400ad2]
    /lib/libc.so.6(__lib c_start_main+0x fd)[0x7ff28bba7c4d]
    ./a.out[0x4007f9]
    ======= Memory map: ========
    00400000-00401000 r-xp 00000000 08:08 123 /home/deepika/prg/a.out
    00600000-00601000 r--p 00000000 08:08 123 /home/deepika/prg/a.out
    00601000-00602000 rw-p 00001000 08:08 123 /home/deepika/prg/a.out
    0187d000-0189e000 rw-p 00000000 00:00 0 [heap]
    7ff284000000-7ff284021000 rw-p 00000000 00:00 0
    7ff284021000-7ff288000000 ---p 00000000 00:00 0
    7ff28b972000-7ff28b988000 r-xp 00000000 08:07 21469 /lib/libgcc_s.so.1
    7ff28b988000-7ff28bb87000 ---p 00016000 08:07 21469 /lib/libgcc_s.so.1
    7ff28bb87000-7ff28bb88000 r--p 00015000 08:07 21469 /lib/libgcc_s.so.1
    7ff28bb88000-7ff28bb89000 rw-p 00016000 08:07 21469 /lib/libgcc_s.so.1
    7ff28bb89000-7ff28bd01000 r-xp 00000000 08:07 19153 /lib/libc-2.11.1.so
    7ff28bd01000-7ff28bf01000 ---p 00178000 08:07 19153 /lib/libc-2.11.1.so
    7ff28bf01000-7ff28bf05000 r--p 00178000 08:07 19153 /lib/libc-2.11.1.so
    7ff28bf05000-7ff28bf06000 rw-p 0017c000 08:07 19153 /lib/libc-2.11.1.so
    7ff28bf06000-7ff28bf0b000 rw-p 00000000 00:00 0
    7ff28bf0b000-7ff28bf2b000 r-xp 00000000 08:07 6347 /lib/ld-2.11.1.so
    7ff28c10b000-7ff28c10e000 rw-p 00000000 00:00 0
    7ff28c127000-7ff28c128000 rw-p 00000000 00:00 0
    7ff28c129000-7ff28c12b000 rw-p 00000000 00:00 0
    7ff28c12b000-7ff28c12c000 r--p 00020000 08:07 6347 /lib/ld-2.11.1.so
    7ff28c12c000-7ff28c12d000 rw-p 00021000 08:07 6347 /lib/ld-2.11.1.so
    7ff28c12d000-7ff28c12e000 rw-p 00000000 00:00 0
    7fffe73e5000-7fffe73fa000 rw-p 00000000 00:00 0 [stack]
    7fffe73ff000-7fffe7400000 r-xp 00000000 00:00 0 [vdso]
    ffffffffff60000 0-ffffffffff60100 0 r-xp 00000000 00:00 0 [vsyscall]
    The no. of lines are : 9 Aborted
    Last edited by Banfa; Jul 4 '10, 10:44 PM. Reason: Added [code] ... [/code] tags
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I expect the heap is trashed and you crash making the calls to free() to clean up your mallocs.

    I would use my debugger and carefully record the mallocs and verify all of the strcpy fits in those allocation.

    Also be sure you don't free the same memory twice.

    Comment

    • rockcesar
      New Member
      • Apr 2014
      • 1

      #3
      I post the answer, compiling in Debian GNU/Linux 8 testing, and also in Debian 7 stable, both:

      When you do a malloc, you have to take in consideration all the array, in case of char**:

      (char **)malloc(nl*si zeof(char *));

      Because is an array of char*. nl is the size of the array, and sizeof(char *) means that each position have an char *.

      In the case of an array of char, you have to malloc like this;

      (char *)malloc((strle n(str1)+1)*size of(char));

      (strlen(str1)+1 ) is the size of the array, and sizeof(char) means that each position have an char.

      Code:
      #include<stdio.h>
          #include<string.h>
          #include<malloc.h>
          int main()
          {
          char **a, **b, str1[10],str2[10],s[80];
          int i=0,nl=0;
          FILE *fp;
           
          fp=fopen("abc.c","r"); 
           
          while(fgets(s,79,fp)!=NULL)               //Counts the no. of lines in a file
                 nl++;
          printf("\n The no. of lines are : %d ",nl); 
           
          rewind(fp);
          a=(char **)malloc(nl*sizeof(char *));//(char **)malloc(nl);
          if(a==NULL)
            {
              printf("\n Not enough memory ");
              return 0;
            }
          b=(char **)malloc(nl*sizeof(char *));
          if(b==NULL)
            {
              printf("\n Not enough memory ");
              return 0;
            }
           
          for(i=0;i<nl;i++)
            {
              fscanf(fp,"%s%s",str1,str2);
      
              a[i]=(char *)malloc((strlen(str1)+1)*sizeof(char));
              b[i]=(char *)malloc((strlen(str2)+1)*sizeof(char));
              strcpy(a[i],str1);
              strcpy(b[i],str2);
           
            }
           
          fclose(fp);
           
          printf("\n");
          for(i=0;i<nl;i++)
           {
             printf("\n%s\t\t%s",a[i],b[i]);
           }
           
          for(i=0;i<nl;i++)
           {
              free(a[i]);
              free(b[i]);
           }
          free(a);
          free(b);
           
          printf("\nSaliendo\n");
          return 0;
          }

      Comment

      Working...