Thread: works fine upto k=2 in main . k=3 in main after mergelen>66 free function gives error

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    18

    works fine upto k=2 in main . k=3 in main after mergelen>66 free function gives error

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    char **L;
    char **R;
    int *left;
    int *right;
    int mergelen;
    int overlap_last(char *st)
    {
      int i,k,j,length;
      char last;
      int overlap_at;
      overlap_at=0;
      length=strlen(st);
      last=st[length-1];
      for(k=0;k<=((length-3)/2);k++)
       {
         if(st[length-1-k-1]==last && st[length-1-k-1-k-1]==last)
           {
             overlap_at=length-1-k-1;
             for(j=0;j<k;j++)
               {
                if(st[length-1-k-1-k+j]!=st[length-1-k+j])
                  {
                    overlap_at=0;
            break;
                  }
               }
              if(overlap_at!=0)
                 break;
           }  
       }
      return overlap_at;
    }
    
    void muk(char *u, int k)
    {
    int i,j,p2m;
    char *h1;
    char *h2;
    p2m=pow(2,k);
    h1=(char *)malloc(sizeof(char)*p2m*strlen(u));
    strcpy(h1,u);
    u[0]='\0';
    for(i=1;i<=k;i++)
    {
    for(j=0;j<strlen(h1);j++)
    {
    if(h1[j]=='a') strcat(u,"ab");
    if(h1[j]=='b') strcat(u,"ba");
    }
    strcpy(h1,u);
    u[0]='\0';
    if(i==k) strcpy(u,h1);
    }
    free(h1);
    }
    
    int ov_free_concat(char *u, char *v)
    {
    int k,i,j,ov_free=1;
    char *l;
    char *t;
    char *m;
    k=strlen(u);
    l=(char *)malloc(sizeof(char)*((strlen(u)+strlen(v))+1));
    t=(char *)malloc(sizeof(char)*((strlen(u)+strlen(v))+1));
    m=(char *)malloc(sizeof(char)*2);
    strcpy(l,u);
    strcpy(t,u);
    strcat(l,v);
    for(i=k;i<=(strlen(l)-1);i++)
    {
    m[0]=l[i];
    m[1]='\0';
    strcat(t,m);
    if(overlap_last(t)!=0) {ov_free=0;break;}
    }
    free(l);
    free(t);
    free(m);
    return ov_free;
    }
    
    int unique(char **M, char *u)
    {
    int i,k=1;
    for(i=0;i<mergelen;i++)
    {
    if((strcmp(M[i],u))==0){k=0;break;}
    }
    return k;
    }
    
    
    void addL(char *u)
    {
    if((unique(L,u)==1))
    {
    L[mergelen]=(char *)malloc(sizeof(char)*strlen(u));
    strcpy(L[mergelen],u);
    mergelen++;
    }
    }
    
    void Lmuk(int k)
    {
    int i,j,m;
    char **s;
    char **u;
    char *s1;
    int p2m,lmergelen;
    s=(char **)malloc(sizeof(char *)*4);
    s[0]="a";
    s[1]="b";
    s[2]="aa";
    s[3]="bb";
    L[0]="";
    L[1]="a";
    L[2]="aa";
    L[3]="b";
    L[4]="bb";
    mergelen=5;
    u=(char **)malloc(sizeof(char *)*4);
    lmergelen=mergelen;
    for(m=1;m<=k;m++)
    {
    for(i=0;i<4;i++)
    {
    u[i]=(char *)malloc(sizeof(char)*pow(2,m)*strlen(s[i]));
    strcpy(u[i],s[i]);
    }
    ///overlap free concatenation add in L//
    for(i=0;i<4;i++)
    {
    muk(u[i],m);
    for(j=0;j<lmergelen;j++)
    {
    if(ov_free_concat(L[j],u[i])!=0) 
    {
    s1=(char *)malloc(sizeof(char)*(strlen(L[j])+strlen(u[i]))); 
    strcpy(s1,L[j]);
    strcat(s1,u[i]);
    addL(s1);
    free(s1);
    }
    }
    }
    /// finished overlap free concatenation add in L//
    for(i=0;i<4;i++)
    {
    free(u[i]);
    }
    lmergelen=mergelen;
    } //m loop
    free(u);
    left=(int *)malloc(sizeof(int)*mergelen);
    for(i=0;i<mergelen;i++)
    {
    left[i]=strlen(L[i]);
    }
    }
    
    int main()
    { 
    int m=5,k=3,i;
    for(i=0;i<k;i++){m=4*m+4;}
    
    L=(char **)malloc(sizeof(char *)*(m+1));
    Lmuk(k);
    printf("\nLmergelen=%d",mergelen);
    for(i=0;i<mergelen;i++)
    {
    printf("\n%s",L[i]);
    }
      printf("\n");
      return 0;
    }
    We have given two sets of strings. Initial number of element in these two sets are four. We are processing second set elements concatenating one element of one set to another element of second set. If resulting concatenation satisfies some criteria. Then we include that element in the first set. The string must not contain string of the form xvxvx where x is letter and v is any string. The ov_free_concat function checks that concatenation of these two strings satisfies that spoecific criteria or not. muk function do some processing to strings. The overlap_last function checkes above criteria at last letter of string.

    Error is comeing in free(s1) of Lmuk function. Initially it don't give any error. But after mergelen>66. It gives error. Which is as follwing.

    *** glibc detected *** /home/mrityunjay/Desktop/Combinatorics on Words/computation/a.out: free(): invalid next size (fast): 0x0804ca90 ***
    ======= Backtrace: =========
    /lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb7e68ee2]
    /home/mrityunjay/Desktop/Combinatorics on Words/computation/a.out[0x8048dc2]
    /home/mrityunjay/Desktop/Combinatorics on Words/computation/a.out[0x8048efa]
    /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb7e0c4d3]
    /home/mrityunjay/Desktop/Combinatorics on Words/computation/a.out[0x80484f1]
    ======= Memory map: ========
    08048000-0804a000 r-xp 00000000 08:05 5767418 /home/mrityunjay/Desktop/Combinatorics on Words/computation/a.out
    0804a000-0804b000 r--p 00001000 08:05 5767418 /home/mrityunjay/Desktop/Combinatorics on Words/computation/a.out
    0804b000-0804c000 rw-p 00002000 08:05 5767418 /home/mrityunjay/Desktop/Combinatorics on Words/computation/a.out
    0804c000-0806d000 rw-p 00000000 00:00 0 [heap]
    b7dc1000-b7ddd000 r-xp 00000000 08:05 4086676 /lib/i386-linux-gnu/libgcc_s.so.1
    b7ddd000-b7dde000 r--p 0001b000 08:05 4086676 /lib/i386-linux-gnu/libgcc_s.so.1
    b7dde000-b7ddf000 rw-p 0001c000 08:05 4086676 /lib/i386-linux-gnu/libgcc_s.so.1
    b7df1000-b7df3000 rw-p 00000000 00:00 0
    b7df3000-b7f97000 r-xp 00000000 08:05 4086400 /lib/i386-linux-gnu/libc-2.15.so
    b7f97000-b7f99000 r--p 001a4000 08:05 4086400 /lib/i386-linux-gnu/libc-2.15.so
    b7f99000-b7f9a000 rw-p 001a6000 08:05 4086400 /lib/i386-linux-gnu/libc-2.15.so
    b7f9a000-b7f9d000 rw-p 00000000 00:00 0
    b7f9d000-b7fc7000 r-xp 00000000 08:05 4086395 /lib/i386-linux-gnu/libm-2.15.so
    b7fc7000-b7fc8000 r--p 00029000 08:05 4086395 /lib/i386-linux-gnu/libm-2.15.so
    b7fc8000-b7fc9000 rw-p 0002a000 08:05 4086395 /lib/i386-linux-gnu/libm-2.15.so
    b7fda000-b7fdd000 rw-p 00000000 00:00 0
    b7fdd000-b7fde000 r-xp 00000000 00:00 0 [vdso]
    b7fde000-b7ffe000 r-xp 00000000 08:05 4086390 /lib/i386-linux-gnu/ld-2.15.so
    b7ffe000-b7fff000 r--p 0001f000 08:05 4086390 /lib/i386-linux-gnu/ld-2.15.so
    b7fff000-b8000000 rw-p 00020000 08:05 4086390 /lib/i386-linux-gnu/ld-2.15.so
    bffdf000-c0000000 rw-p 00000000 00:00 0 [stack]

    Program received signal SIGABRT, Aborted.
    0xb7fdd424 in __kernel_vsyscall ()

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You seem to have lost most of your indentation - you should fix this so people can read the code.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    18
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    char **L;
    char **R;
    int *left;
    int *right;
    int mergelen;
    int overlap_last(char *st)
    {
      int i,k,j,length;
      char last;
      int overlap_at;
      overlap_at=0;
      length=strlen(st);
      last=st[length-1];
      for(k=0;k<=((length-3)/2);k++)
       {
         if(st[length-1-k-1]==last && st[length-1-k-1-k-1]==last)
           {
             overlap_at=length-1-k-1;
             for(j=0;j<k;j++)
               {
                if(st[length-1-k-1-k+j]!=st[length-1-k+j])
                  {
                    overlap_at=0;
            break;
                  }
               }
              if(overlap_at!=0)
                 break;
           } 
       }
      return overlap_at;
    }
     
    void muk(char *u, int k)
    {
      int i,j,p2m;
      char *h1;
      char *h2;
      p2m=pow(2,k);
      h1=(char *)malloc(sizeof(char)*p2m*strlen(u));
      strcpy(h1,u);
      u[0]='\0';
      for(i=1;i<=k;i++)
        {
          for(j=0;j<strlen(h1);j++)
            {
              if(h1[j]=='a') strcat(u,"ab");
              if(h1[j]=='b') strcat(u,"ba");
            }
          strcpy(h1,u);
          u[0]='\0';
          if(i==k) strcpy(u,h1);
       }
      free(h1);
    }
     
    int ov_free_concat(char *u, char *v)
    {
      int k,i,j,ov_free=1;
      char *l;
      char *t;
      char *m;
      k=strlen(u);
      l=(char *)malloc(sizeof(char)*((strlen(u)+strlen(v))+1));
      t=(char *)malloc(sizeof(char)*((strlen(u)+strlen(v))+1));
      m=(char *)malloc(sizeof(char)*2);
      strcpy(l,u);
      strcpy(t,u);
      strcat(l,v);
      for(i=k;i<=(strlen(l)-1);i++)
        {
          m[0]=l[i];
          m[1]='\0';
          strcat(t,m);
          if(overlap_last(t)!=0) 
            {
              ov_free=0;break;
            }
        }
      free(l);
      free(t);
      free(m);
      return ov_free;
    }
     
    int unique(char **M, char *u)
    {
      int i,k=1;
      for(i=0;i<mergelen;i++)
        {
          if((strcmp(M[i],u))==0)
            {
               k=0;
               break;
            }
        }
    return k;
    }
     
     
    void addL(char *u)
    {
      if((unique(L,u)==1))
        {
           L[mergelen]=(char *)malloc(sizeof(char)*strlen(u));
           strcpy(L[mergelen],u);
           mergelen++;
        }
    }
     
    void Lmuk(int k)
    {
       int i,j,m;
       char **s;
       char **u;
       char *s1;
       int p2m,lmergelen;
       s=(char **)malloc(sizeof(char *)*4);
       s[0]="a";
       s[1]="b";
       s[2]="aa";
       s[3]="bb";
       L[0]="";
       L[1]="a";
       L[2]="aa";
       L[3]="b";
       L[4]="bb";
       mergelen=5;
       u=(char **)malloc(sizeof(char *)*4);
       lmergelen=mergelen;
       for(m=1;m<=k;m++)
          {
             for(i=0;i<4;i++)
                {
                   u[i]=(char *)malloc(sizeof(char)*pow(2,m)*strlen(s[i]));
                   strcpy(u[i],s[i]);
                }
    ///overlap free concatenation add in L//
             for(i=0;i<4;i++)
                {
                   muk(u[i],m);
                   for(j=0;j<lmergelen;j++)
                      {
                         if(ov_free_concat(L[j],u[i])!=0)
                           {
                              s1=(char *)malloc(sizeof(char)*(strlen(L[j])+strlen(u[i])));
                              strcpy(s1,L[j]);
                              strcat(s1,u[i]);
                              addL(s1);
                              free(s1);
                           }
                      }
                 }
    /// finished overlap free concatenation add in L//
             for(i=0;i<4;i++)
                {
                   free(u[i]);
                }
             lmergelen=mergelen;
          } //m loop
      free(u);
      left=(int *)malloc(sizeof(int)*mergelen);
      for(i=0;i<mergelen;i++)
        {
           left[i]=strlen(L[i]);
        }
    }
     
    int main()
    {
      int m=5,k=3,i;
    //higher bound of L size estimation
      for(i=0;i<k;i++)
         {
            m=4*m+4;
         }
      L=(char **)malloc(sizeof(char *)*(m+1));
      Lmuk(k);
      printf("\nLmergelen=%d",mergelen);
      for(i=0;i<mergelen;i++)
         {
            printf("\n%s",L[i]);
         }
      printf("\n");
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    18
    We have given two sets of strings. Initial number of element in these two sets are four. We are processing second set elements concatenating one element of one set to another element of second set. If resulting concatenation satisfies some criteria. Then we include that element in the first set. The string must not contain string of the form xvxvx where x is letter and v is any string. The ov_free_concat function checks that concatenation of these two strings satisfies that spoecific criteria or not. muk function do some processing to strings. The overlap_last function checkes above criteria at last letter of string.

    Error is comeing in line 154 free(s1) of Lmuk function.
    For k=1 and 2 in main, it don't give any error. But as we set k=3 after mergelen>66. It gives error. Which is as follwing.


    *** glibc detected *** /home/mrityunjay/Desktop/Combinatorics on Words/computation/a.out: free(): invalid next size (fast): 0x0804ca90 ***
    ======= Backtrace: =========
    /lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb7e68ee2]
    /home/mrityunjay/Desktop/Combinatorics on Words/computation/a.out[0x8048dc2]
    /home/mrityunjay/Desktop/Combinatorics on Words/computation/a.out[0x8048efa]
    /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb7e0c4d3]
    /home/mrityunjay/Desktop/Combinatorics on Words/computation/a.out[0x80484f1]
    ======= Memory map: ========
    08048000-0804a000 r-xp 00000000 08:05 5767418 /home/mrityunjay/Desktop/Combinatorics on Words/computation/a.out
    0804a000-0804b000 r--p 00001000 08:05 5767418 /home/mrityunjay/Desktop/Combinatorics on Words/computation/a.out
    0804b000-0804c000 rw-p 00002000 08:05 5767418 /home/mrityunjay/Desktop/Combinatorics on Words/computation/a.out
    0804c000-0806d000 rw-p 00000000 00:00 0 [heap]
    b7dc1000-b7ddd000 r-xp 00000000 08:05 4086676 /lib/i386-linux-gnu/libgcc_s.so.1
    b7ddd000-b7dde000 r--p 0001b000 08:05 4086676 /lib/i386-linux-gnu/libgcc_s.so.1
    b7dde000-b7ddf000 rw-p 0001c000 08:05 4086676 /lib/i386-linux-gnu/libgcc_s.so.1
    b7df1000-b7df3000 rw-p 00000000 00:00 0
    b7df3000-b7f97000 r-xp 00000000 08:05 4086400 /lib/i386-linux-gnu/libc-2.15.so
    b7f97000-b7f99000 r--p 001a4000 08:05 4086400 /lib/i386-linux-gnu/libc-2.15.so
    b7f99000-b7f9a000 rw-p 001a6000 08:05 4086400 /lib/i386-linux-gnu/libc-2.15.so
    b7f9a000-b7f9d000 rw-p 00000000 00:00 0
    b7f9d000-b7fc7000 r-xp 00000000 08:05 4086395 /lib/i386-linux-gnu/libm-2.15.so
    b7fc7000-b7fc8000 r--p 00029000 08:05 4086395 /lib/i386-linux-gnu/libm-2.15.so
    b7fc8000-b7fc9000 rw-p 0002a000 08:05 4086395 /lib/i386-linux-gnu/libm-2.15.so
    b7fda000-b7fdd000 rw-p 00000000 00:00 0
    b7fdd000-b7fde000 r-xp 00000000 00:00 0 [vdso]
    b7fde000-b7ffe000 r-xp 00000000 08:05 4086390 /lib/i386-linux-gnu/ld-2.15.so
    b7ffe000-b7fff000 r--p 0001f000 08:05 4086390 /lib/i386-linux-gnu/ld-2.15.so
    b7fff000-b8000000 rw-p 00020000 08:05 4086390 /lib/i386-linux-gnu/ld-2.15.so
    bffdf000-c0000000 rw-p 00000000 00:00 0 [stack]

    Program received signal SIGABRT, Aborted.
    0xb7fdd424 in __kernel_vsyscall ()

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There are a few things that could be cleaned up in the code.
    Code:
    $ gcc -Wall -Wextra -O2 bar.c -lm
    bar.c: In function ‘overlap_last’:
    bar.c:12:7: warning: unused variable ‘i’ [-Wunused-variable]
    bar.c: In function ‘muk’:
    bar.c:49:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    bar.c:42:9: warning: unused variable ‘h2’ [-Wunused-variable]
    bar.c: In function ‘ov_free_concat’:
    bar.c:74:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    bar.c:63:11: warning: unused variable ‘j’ [-Wunused-variable]
    bar.c: In function ‘Lmuk’:
    bar.c:121:8: warning: unused variable ‘p2m’ [-Wunused-variable]
    You have lots of buffer overruns in your arrays. You need to fix these.
    If you write past the end of your allocated block, you risk trashing the hidden data that malloc/free need to manage memory properly.
    Code:
    $ valgrind  ./a.out
    ==2958== Memcheck, a memory error detector
    ==2958== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
    ==2958== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
    ==2958== Command: ./a.out
    ==2958== 
    ==2958== Invalid write of size 1
    ==2958==    at 0x400972: muk (bar.c:51)
    ==2958==    by 0x400F6B: Lmuk (bar.c:145)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x5456df2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400F03: Lmuk (bar.c:139)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x4C29798: strcpy (mc_replace_strmem.c:311)
    ==2958==    by 0x400A0E: muk (bar.c:54)
    ==2958==    by 0x400F6B: Lmuk (bar.c:145)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x5456df2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400F03: Lmuk (bar.c:139)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid write of size 1
    ==2958==    at 0x4C2979F: strcpy (mc_replace_strmem.c:311)
    ==2958==    by 0x400A0E: muk (bar.c:54)
    ==2958==    by 0x400F6B: Lmuk (bar.c:145)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x5456f32 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x4008F1: muk (bar.c:44)
    ==2958==    by 0x400F6B: Lmuk (bar.c:145)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x4C29798: strcpy (mc_replace_strmem.c:311)
    ==2958==    by 0x400A30: muk (bar.c:56)
    ==2958==    by 0x400F6B: Lmuk (bar.c:145)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x5456f32 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x4008F1: muk (bar.c:44)
    ==2958==    by 0x400F6B: Lmuk (bar.c:145)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid write of size 1
    ==2958==    at 0x4C2979F: strcpy (mc_replace_strmem.c:311)
    ==2958==    by 0x400A30: muk (bar.c:56)
    ==2958==    by 0x400F6B: Lmuk (bar.c:145)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x5456df2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400F03: Lmuk (bar.c:139)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x400AD2: ov_free_concat (bar.c:68)
    ==2958==    by 0x400FA9: Lmuk (bar.c:148)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x5456df2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400F03: Lmuk (bar.c:139)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x400B33: ov_free_concat (bar.c:69)
    ==2958==    by 0x400FA9: Lmuk (bar.c:148)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x5456df2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400F03: Lmuk (bar.c:139)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x4C2948E: strcat (mc_replace_strmem.c:176)
    ==2958==    by 0x400B98: ov_free_concat (bar.c:73)
    ==2958==    by 0x400FA9: Lmuk (bar.c:148)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x5456df2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400F03: Lmuk (bar.c:139)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x401013: Lmuk (bar.c:150)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x5456df2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400F03: Lmuk (bar.c:139)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x4C2948E: strcat (mc_replace_strmem.c:176)
    ==2958==    by 0x401078: Lmuk (bar.c:152)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x5456df2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400F03: Lmuk (bar.c:139)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid write of size 1
    ==2958==    at 0x4C29495: strcat (mc_replace_strmem.c:176)
    ==2958==    by 0x401078: Lmuk (bar.c:152)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x5457072 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x401029: Lmuk (bar.c:150)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x400D10: addL (bar.c:109)
    ==2958==    by 0x401084: Lmuk (bar.c:153)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x5457072 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x401029: Lmuk (bar.c:150)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x4C29798: strcpy (mc_replace_strmem.c:311)
    ==2958==    by 0x400D4F: addL (bar.c:110)
    ==2958==    by 0x401084: Lmuk (bar.c:153)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x5457072 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x401029: Lmuk (bar.c:150)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid write of size 1
    ==2958==    at 0x4C2979F: strcpy (mc_replace_strmem.c:311)
    ==2958==    by 0x400D4F: addL (bar.c:110)
    ==2958==    by 0x401084: Lmuk (bar.c:153)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x54570c2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400D23: addL (bar.c:109)
    ==2958==    by 0x401084: Lmuk (bar.c:153)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x4007E4: overlap_last (bar.c:20)
    ==2958==    by 0x400BDC: ov_free_concat (bar.c:79)
    ==2958==    by 0x400FA9: Lmuk (bar.c:148)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x545715f is 1 bytes before a block of size 4 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400B4D: ov_free_concat (bar.c:69)
    ==2958==    by 0x400FA9: Lmuk (bar.c:148)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid write of size 1
    ==2958==    at 0x4009BF: muk (bar.c:52)
    ==2958==    by 0x400F6B: Lmuk (bar.c:145)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x5456e42 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400F03: Lmuk (bar.c:139)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x4C2A00D: strcmp (mc_replace_strmem.c:538)
    ==2958==    by 0x400C8F: unique (bar.c:95)
    ==2958==    by 0x400CD3: addL (bar.c:107)
    ==2958==    by 0x401084: Lmuk (bar.c:153)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x54577f2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x401029: Lmuk (bar.c:150)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x4C2A008: strcmp (mc_replace_strmem.c:538)
    ==2958==    by 0x400C8F: unique (bar.c:95)
    ==2958==    by 0x400CD3: addL (bar.c:107)
    ==2958==    by 0x401084: Lmuk (bar.c:153)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x54570c2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400D23: addL (bar.c:109)
    ==2958==    by 0x401084: Lmuk (bar.c:153)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x400A81: ov_free_concat (bar.c:67)
    ==2958==    by 0x400FA9: Lmuk (bar.c:148)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x54570c2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400D23: addL (bar.c:109)
    ==2958==    by 0x401084: Lmuk (bar.c:153)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x400AAB: ov_free_concat (bar.c:68)
    ==2958==    by 0x400FA9: Lmuk (bar.c:148)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x54570c2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400D23: addL (bar.c:109)
    ==2958==    by 0x401084: Lmuk (bar.c:153)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x400B0C: ov_free_concat (bar.c:69)
    ==2958==    by 0x400FA9: Lmuk (bar.c:148)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x54570c2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400D23: addL (bar.c:109)
    ==2958==    by 0x401084: Lmuk (bar.c:153)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x4C29798: strcpy (mc_replace_strmem.c:311)
    ==2958==    by 0x400B72: ov_free_concat (bar.c:71)
    ==2958==    by 0x400FA9: Lmuk (bar.c:148)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x54570c2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400D23: addL (bar.c:109)
    ==2958==    by 0x401084: Lmuk (bar.c:153)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x4C29798: strcpy (mc_replace_strmem.c:311)
    ==2958==    by 0x400B85: ov_free_concat (bar.c:72)
    ==2958==    by 0x400FA9: Lmuk (bar.c:148)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x54570c2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400D23: addL (bar.c:109)
    ==2958==    by 0x401084: Lmuk (bar.c:153)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x400FE0: Lmuk (bar.c:150)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x54570c2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400D23: addL (bar.c:109)
    ==2958==    by 0x401084: Lmuk (bar.c:153)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x4C29798: strcpy (mc_replace_strmem.c:311)
    ==2958==    by 0x401056: Lmuk (bar.c:151)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x54570c2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400D23: addL (bar.c:109)
    ==2958==    by 0x401084: Lmuk (bar.c:153)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ==2958== Invalid read of size 1
    ==2958==    at 0x401166: Lmuk (bar.c:169)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958==  Address 0x54570c2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400D23: addL (bar.c:109)
    ==2958==    by 0x401084: Lmuk (bar.c:153)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    
    Lmergelen=135
    
    a
    aa
    b
    bb
    ==2958== Invalid read of size 1
    ==2958==    at 0x50FE297: vfprintf (vfprintf.c:1629)
    ==2958==    by 0x5106AD8: printf (printf.c:35)
    ==2958==    by 0x401234: main (bar.c:186)
    ==2958==  Address 0x54570c2 is 0 bytes after a block of size 2 alloc'd
    ==2958==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2958==    by 0x400D23: addL (bar.c:109)
    ==2958==    by 0x401084: Lmuk (bar.c:153)
    ==2958==    by 0x4011E5: main (bar.c:182)
    ==2958== 
    ab
    << snipped output for brevity >>
    bbabaabbaababbabaababba
    ==2958== 
    ==2958== HEAP SUMMARY:
    ==2958==     in use at exit: 5,384 bytes in 133 blocks
    ==2958==   total heap usage: 1,164 allocs, 1,031 frees, 16,988 bytes allocated
    ==2958== 
    ==2958== LEAK SUMMARY:
    ==2958==    definitely lost: 32 bytes in 1 blocks
    ==2958==    indirectly lost: 0 bytes in 0 blocks
    ==2958==      possibly lost: 0 bytes in 0 blocks
    ==2958==    still reachable: 5,352 bytes in 132 blocks
    ==2958==         suppressed: 0 bytes in 0 blocks
    ==2958== Rerun with --leak-check=full to see details of leaked memory
    ==2958== 
    ==2958== For counts of detected and suppressed errors, rerun with: -v
    ==2958== ERROR SUMMARY: 3866 errors from 27 contexts (suppressed: 4 from 4)

    You can use the debugger to go through them one at a time, like so.
    Code:
    $ valgrind --db-attach=yes ./a.out
    ==2946== Memcheck, a memory error detector
    ==2946== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
    ==2946== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
    ==2946== Command: ./a.out
    ==2946== 
    ==2946== Invalid write of size 1
    ==2946==    at 0x400972: muk (bar.c:51)
    ==2946==    by 0x400F6B: Lmuk (bar.c:145)
    ==2946==    by 0x4011E5: main (bar.c:182)
    ==2946==  Address 0x5456df2 is 0 bytes after a block of size 2 alloc'd
    ==2946==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==2946==    by 0x400F03: Lmuk (bar.c:139)
    ==2946==    by 0x4011E5: main (bar.c:182)
    ==2946== 
    ==2946== 
    ==2946== ---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ---- y
    ==2946== starting debugger with cmd: /usr/bin/gdb -nw /proc/2949/fd/1024 2949
    GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
    << snipped >>
    0x0000000000400972 in muk (u=0x5456df0 "ab", k=1) at bar.c:51
    51                if(h1[j]=='a') strcat(u,"ab");
    What this is telling you is that you allocated 2 bytes on line 139, but the function call through line 145 and onto line 51 tried to write 3 bytes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    18
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    char **L;
    char **R;
    int *left;
    int *right;
    int mergelen;
    int overlap_last(char *st)
    {
      int k,j,length;
      char last;
      int overlap_at;
      overlap_at=0;
      length=strlen(st);
      last=st[length-1];
      for(k=0;k<=((length-3)/2);k++)
       {
         if(st[length-1-k-1]==last && st[length-1-k-1-k-1]==last)
           {
             overlap_at=length-1-k-1;
             for(j=0;j<k;j++)
               {
                if(st[length-1-k-1-k+j]!=st[length-1-k+j])
                  {
                    overlap_at=0;
            break;
                  }
               }
              if(overlap_at!=0)
                 break;
           }
       }
      return overlap_at;
    }
     
    void muk(char *u, int k)
    {
      int i,j,p2m,length;
      char *h1;
      p2m=pow(2,k);
      h1=(char *)malloc(sizeof(char)*(p2m+1)*strlen(u));
      strcpy(h1,u);
      u[0]='\0';
      length=strlen(h1);
      for(i=1;i<=k;i++)
        {
          for(j=0;j<length;j++)
            {
              if(h1[j]=='a') strcat(u,"ab");
              if(h1[j]=='b') strcat(u,"ba");
            }
          strcpy(h1,u);
          u[0]='\0';
          if(i==k) strcpy(u,h1);
       }
      free(h1);
    }
     
    int ov_free_concat(char *u, char *v)
    {
      int k,i,ov_free=1,length;
      char *l;
      char *t;
      char *m;
      k=strlen(u);
      l=(char *)malloc(sizeof(char)*((strlen(u)+strlen(v))+1));
      t=(char *)malloc(sizeof(char)*((strlen(u)+strlen(v))+1));
      m=(char *)malloc(sizeof(char)*2);
      strcpy(l,u);
      strcpy(t,u);
      strcat(l,v);
      length=strlen(l);
      for(i=k;i<length;i++)
        {
          m[0]=l[i];
          m[1]='\0';
          strcat(t,m);
          if(overlap_last(t)!=0)
            {
              ov_free=0;break;
            }
        }
      free(l);
      free(t);
      free(m);
      return ov_free;
    }
     
    int unique(char **M, char *u)
    {
      int i,k=1;
      for(i=0;i<mergelen;i++)
        {
          if((strcmp(M[i],u))==0)
            {
               k=0;
               break;
            }
        }
    return k;
    }
     
     
    void addL(char *u)
    {
      if((unique(L,u)==1))
        {
           L[mergelen]=(char *)malloc(sizeof(char)*(strlen(u)+1));
           strcpy(L[mergelen],u);
           mergelen++;
        }
    }
     
    
    void addR(char *u)
     {
       if((unique(R,u)==1))
         {
           R[mergelen]=(char *)malloc(sizeof(char)*(strlen(u)+1));
           strcpy(R[mergelen],u);
           mergelen++;
         }
     }
    
    void Lmuk(int k)
    {
       int i,j,m;
       char **s;
       char **u;
       char *s1;
       int lmergelen;
       s=(char **)malloc(sizeof(char *)*4);
       s[0]="a";
       s[1]="b";
       s[2]="aa";
       s[3]="bb";
       L[0]="";
       L[1]="a";
       L[2]="aa";
       L[3]="b";
       L[4]="bb";
       mergelen=5;
       u=(char **)malloc(sizeof(char *)*4);
       lmergelen=mergelen;
       for(m=1;m<=k;m++)
          {
             for(i=0;i<4;i++)
                {
                   u[i]=(char *)malloc(sizeof(char)*(pow(2,m)+1)*strlen(s[i]));
                   strcpy(u[i],s[i]);
                }
    ///overlap free concatenation add in L//
             for(i=0;i<4;i++)
                {
                   muk(u[i],m);
                   for(j=0;j<lmergelen;j++)
                      {
                         if(ov_free_concat(L[j],u[i])!=0)
                           {
                              s1=(char *)malloc(sizeof(char)*(strlen(L[j])+strlen(u[i])+1));
                              strcpy(s1,L[j]);
                              strcat(s1,u[i]);
                              addL(s1);
                              free(s1);
                           }
                      }
                 }
    /// finished overlap free concatenation add in L//
             for(i=0;i<4;i++)
                {
                   free(u[i]);
                }
             lmergelen=mergelen;
          } //m loop
      free(u);
      left=(int *)malloc(sizeof(int)*mergelen);
      for(i=0;i<mergelen;i++)
        {
           left[i]=strlen(L[i]);
        }
    }
    
    void mukR(int k)
    {
      int i,j,m,rmergelen;
      char **s;
      char **u;
      char *s1;
      s=(char **)malloc(sizeof(char *)*4);
      s[0]="a";
      s[1]="b";
      s[2]="aa";
      s[3]="bb";
      u=(char **)malloc(sizeof(char *)*4);
      rmergelen=mergelen;
      R[0]="";
      for(i=0;i<4;i++)
        {
           u[i]=(char *)malloc(sizeof(char)*(pow(2,k)+1)*strlen(s[i]));
           strcpy(u[i],s[i]);
           muk(u[i],k);
           R[i+1]=(char *)malloc(sizeof(char)*(pow(2,k)+1)*strlen(s[i]));
           strcpy(R[i+1],u[i]);
        }
      mergelen=5;
      for(i=0;i<4;i++)
         {
           free(u[i]);
         }
      rmergelen=mergelen;
      for(m=k-1;m>0;m--)
        {
           for(i=0;i<4;i++)
             {
                u[i]=(char *)malloc(sizeof(char)*(pow(2,k)+1)*strlen(s[i]));
                strcpy(u[i],s[i]);
             }
    ///overlap free concatenatioan and addition in R///
          for(i=0;i<4;i++)
             {
                muk(u[i],m);
                for(j=0;j<rmergelen;j++)
                   {
                      if(ov_free_concat(R[j],u[i])!=0)
                        {
                           s1=(char *)malloc(sizeof(char)*(strlen(R[j])+strlen(u[i])+1));
                           strcpy(s1,R[j]);
                           strcat(s1,u[i]);
                           addR(s1);
                           free(s1);
                        }
                   }
             } //addition in R finished
          rmergelen=mergelen;
          for(i=0;i<4;i++)
             {
                free(u[i]);
             }
        }//m loop finished
      free(u);
    //concatenation with s[]
      for(i=0;i<4;i++)
        {
           for(j=0;j<rmergelen;j++)
              {
                 if(ov_free_concat(R[j],s[i])!=0)
                   {
                      s1=(char *)malloc(sizeof(char)*(strlen(R[j])+strlen(s[i])+1));
                      strcpy(s1,R[j]);
                      strcat(s1,s[i]);
                      addR(s1);
                      free(s1);
                   }
              }
        }
    
      right=(int *)malloc(sizeof(int)*mergelen);
      for(i=0;i<mergelen;i++)
         {
            right[i]=strlen(R[i]);
         }
    }
     
    int main()
    {
      int m=5,k=5,i;
    //higher bound of L size estimation
      for(i=0;i<k;i++)
         {
            m=4*m+4;
         }
      L=(char **)malloc(sizeof(char *)*(m+1));
      Lmuk(k);
      printf("\nLmergelen=%d",mergelen);
     /* for(i=0;i<mergelen;i++)
         {
            printf("\n%s",L[i]);
         }*/
    
    mergelen=0;
    R=(char **)malloc(sizeof(char *)*(m+1));
    mukR(k);
    printf("\nRmergelen=%d",mergelen);
    /*for(i=0;i<mergelen;i++)
    {
    printf("\n%s",R[i]);
    }*/
      printf("\n");
      return 0;
    }
    I have removed memory write errors and wall warning from programme. But still 3 or 4 read errors are present in the programme. Now my programme is giving out put. Thank You. But can you help me to remove theses read errors i am not getting how to remove these read errors show in this code snippet. Give some tips about leakage reduction. Thank You
    Code:
    valgrind --db-attach=yes --leak-check=full --show-reachable=yes ./a.out
    ==2503== Memcheck, a memory error detector
    ==2503== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
    ==2503== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
    ==2503== Command: ./a.out
    ==2503== 
    ==2503== Invalid read of size 1
    ==2503==    at 0x80485FB: overlap_last (restivo_salemi.c:20)
    ==2503==    by 0x804899B: ov_free_concat (restivo_salemi.c:80)
    ==2503==    by 0x8048D20: Lmuk (restivo_salemi.c:160)
    ==2503==    by 0x80494FD: main (restivo_salemi.c:275)
    ==2503==  Address 0x4224897 is 1 bytes before a block of size 4 alloc'd
    ==2503==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==2503==    by 0x80488F5: ov_free_concat (restivo_salemi.c:69)
    ==2503==    by 0x8048D20: Lmuk (restivo_salemi.c:160)
    ==2503==    by 0x80494FD: main (restivo_salemi.c:275)
    ==2503== 
    ==2503== 
    ==2503== ---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ---- n
    
    ==2503== Invalid read of size 1
    ==2503==    at 0x80485FB: overlap_last (restivo_salemi.c:20)
    ==2503==    by 0x804899B: ov_free_concat (restivo_salemi.c:80)
    ==2503==    by 0x8049356: mukR (restivo_salemi.c:248)
    ==2503==    by 0x8049541: main (restivo_salemi.c:284)
    ==2503==  Address 0x42b09a7 is 1 bytes before a block of size 3 alloc'd
    ==2503==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==2503==    by 0x80488F5: ov_free_concat (restivo_salemi.c:69)
    ==2503==    by 0x8049356: mukR (restivo_salemi.c:248)
    ==2503==    by 0x8049541: main (restivo_salemi.c:284)
    ==2503== 
    ==2503== 
    ==2503== ---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ---- n
    Lmergelen=329
    Rmergelen=329
    ==2503== 
    ==2503== HEAP SUMMARY:
    ==2503==     in use at exit: 64,004 bytes in 658 blocks
    ==2503==   total heap usage: 9,622 allocs, 8,964 frees, 154,390 bytes allocated
    ==2503== 
    ==2503== 16 bytes in 1 blocks are definitely lost in loss record 1 of 10
    ==2503==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==2503==    by 0x8048B69: Lmuk (restivo_salemi.c:134)
    ==2503==    by 0x80494FD: main (restivo_salemi.c:275)
    ==2503== 
    ==2503== 16 bytes in 1 blocks are definitely lost in loss record 2 of 10
    ==2503==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==2503==    by 0x8048EC1: mukR (restivo_salemi.c:191)
    ==2503==    by 0x8049541: main (restivo_salemi.c:284)
    ==2503== 
    ==2503== 198 bytes in 4 blocks are still reachable in loss record 3 of 10
    ==2503==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==2503==    by 0x8049063: mukR (restivo_salemi.c:204)
    ==2503==    by 0x8049541: main (restivo_salemi.c:284)
    ==2503== 
    ==2503== 1,316 bytes in 1 blocks are still reachable in loss record 4 of 10
    ==2503==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==2503==    by 0x8048E4F: Lmuk (restivo_salemi.c:178)
    ==2503==    by 0x80494FD: main (restivo_salemi.c:275)
    ==2503== 
    ==2503== 1,316 bytes in 1 blocks are still reachable in loss record 5 of 10
    ==2503==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==2503==    by 0x804943C: mukR (restivo_salemi.c:259)
    ==2503==    by 0x8049541: main (restivo_salemi.c:284)
    ==2503== 
    ==2503== 1,818 bytes in 126 blocks are still reachable in loss record 6 of 10
    ==2503==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==2503==    by 0x8048B1E: addR (restivo_salemi.c:121)
    ==2503==    by 0x80492A8: mukR (restivo_salemi.c:231)
    ==2503==    by 0x8049541: main (restivo_salemi.c:284)
    ==2503== 
    ==2503== 2,810 bytes in 198 blocks are still reachable in loss record 7 of 10
    ==2503==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==2503==    by 0x8048B1E: addR (restivo_salemi.c:121)
    ==2503==    by 0x8049403: mukR (restivo_salemi.c:253)
    ==2503==    by 0x8049541: main (restivo_salemi.c:284)
    ==2503== 
    ==2503== 4,634 bytes in 324 blocks are still reachable in loss record 8 of 10
    ==2503==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==2503==    by 0x8048A8B: addL (restivo_salemi.c:110)
    ==2503==    by 0x8048DCD: Lmuk (restivo_salemi.c:165)
    ==2503==    by 0x80494FD: main (restivo_salemi.c:275)
    ==2503== 
    ==2503== 25,940 bytes in 1 blocks are still reachable in loss record 9 of 10
    ==2503==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==2503==    by 0x80494EC: main (restivo_salemi.c:274)
    ==2503== 
    ==2503== 25,940 bytes in 1 blocks are still reachable in loss record 10 of 10
    ==2503==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==2503==    by 0x8049530: main (restivo_salemi.c:283)
    ==2503== 
    ==2503== LEAK SUMMARY:
    ==2503==    definitely lost: 32 bytes in 2 blocks
    ==2503==    indirectly lost: 0 bytes in 0 blocks
    ==2503==      possibly lost: 0 bytes in 0 blocks
    ==2503==    still reachable: 63,972 bytes in 656 blocks
    ==2503==         suppressed: 0 bytes in 0 blocks
    ==2503== 
    ==2503== For counts of detected and suppressed errors, rerun with: -v
    ==2503== ERROR SUMMARY: 24 errors from 4 contexts (suppressed: 0 from 0)
    Last edited by mrityunjay23; 05-08-2014 at 06:12 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ==2503== 16 bytes in 1 blocks are definitely lost in loss record 1 of 10
    > ==2503== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    > ==2503== by 0x8048B69: Lmuk (restivo_salemi.c:134)
    > ==2503== by 0x80494FD: main (restivo_salemi.c:275)
    You look at line 134 (it calls malloc and assigns a pointer).
    Then you follow the code through until that pointer is lost.

    But you also need to fix your remaining two buffer overruns as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error "in function 'main' syntax error before 'int' Help Please
    By blackhat11907 in forum C Programming
    Replies: 5
    Last Post: 08-20-2011, 07:05 PM
  2. Replies: 3
    Last Post: 06-01-2011, 03:08 AM
  3. Read from serial port only works in main... ??
    By chris24300 in forum Linux Programming
    Replies: 6
    Last Post: 06-19-2009, 01:22 PM
  4. Circular main <- main.o dependency dropped.
    By Queatrix in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2005, 02:32 PM
  5. void main(), int main(), argc, argv[]????
    By Jonny M in forum C Programming
    Replies: 3
    Last Post: 03-06-2002, 09:12 AM