Thread: error: double free or corruption

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    4

    error: double free or corruption

    Hi,

    can someone plss tell me what i'm doing wrong..

    error:
    Code:
    *** glibc detected *** double free or corruption
    (fasttop): 0x0804a008 ***
    Aborted
    code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void clearInput();
    long *allocate_numberarray(long len);
    void our_free(long *mem);
    void print(long *mem, long len);
    void einlesen(long *mem, long len);
    long *change_size(long *mem, long old_size, long new_size);
    long append (long *mem1, long size1, long *mem2, long size2, long
     *mem3, long size3);
    
    
    int main(int argc, char *argv[])
    {
      long *test;
      long *testneu;
      long *wert1;
      long *wert2;
      long *wert3;
      long anzahl,anzahlneu,anzahl1,anzahl2,anzahl3,a;
      
      printf("Geben Sie eine Anzahl ein: ");
      scanf("%ld",&anzahl);
      clearInput();
      test=allocate_numberarray(anzahl);
    
      einlesen(test, anzahl);
    
      printf("\n\nWerte ausgeben\n\n");
      print(test,anzahl);
    
      printf("\nGeben Sie eine neue Anzahl ein: ");
      scanf("%ld",&anzahlneu);
      clearInput();
      testneu=change_size(test, anzahl, anzahlneu);
      einlesen(testneu,anzahlneu);
      printf("\n\nNeue Werte ausgeben\n\n");
      print(testneu,anzahlneu);
    
      our_free(test);
      our_free(testneu);
      
      printf("\n\n");
      printf("Geben Sie eine Anzahl1 ein: ");
      scanf("%ld",&anzahl1);
      clearInput();
      wert1=allocate_numberarray(anzahl1);
      einlesen(wert1,anzahl1);
      printf("\n\nWerte1 ausgeben\n\n");
      print(wert1,anzahl1);
      
      printf("\nGeben Sie eine Anzahl2 ein: ");
      scanf("%ld",&anzahl2);
      clearInput();
      wert2=allocate_numberarray(anzahl2);
      einlesen(wert2,anzahl2);
      printf("\n\nWerte2 ausgeben\n\n");
      print(wert2,anzahl2);
      
      anzahl3=anzahl1+anzahl2;
      wert3=allocate_numberarray(anzahl3);  
      a=append (wert1,anzahl1,wert2,anzahl2,wert3,anzahl3);
      if(a==0)
      {
        fprintf(stderr,"+++ Fehler +++\n");
        our_free(wert1);
        our_free(wert2);
        our_free(wert3);
        exit(-1);
      }
      else
      {
        printf("\n\nWerte3 ausgeben\n\n");
        print(wert3,anzahl3);
        printf("\n\n");
      }  
      our_free(wert1);
      our_free(wert2);
      our_free(wert3);
    
      printf("Press ENTER to continue...\n");
      getchar();
      return 0;
    }
    
    
    
    
    void clearInput()
    {
    while(getchar() != '\n');
    }
    
    long *allocate_numberarray(long len)
    {
      long *ptr;
      ptr=calloc(len,(sizeof(long)));
      if(ptr==0)     
      {
        fprintf(stderr,"+++ Fehler +++\n");
        exit(-1);
      }
      return ptr;
    }
    
    void our_free(long *mem)
    {
      if(mem)
      {
        free(mem);
        mem=0;
      }
    }
    
    void print(long *mem, long len)
    {
      long i;
      for(i=len-1;i>=0;i--)
        printf("%ld. Zahl = %ld\n",i+1,mem[i]);
      printf("\n");
    }
    
    void einlesen(long *mem, long len)
    {
      long i;
      printf("\nGeben Sie %ld Werte ein:\n\n",len);
      for(i=0;i<len;i++)
      {
        printf("%ld. Wert: ",i+1);
        scanf("%ld",&mem[i]);
        clearInput();
        if((mem[i]<(-9))||(mem[i]>9))
        {
          fprintf(stderr,"+++ Fehler +++\n");
          exit(-1);
        }
      }
    }
    
    
    long *change_size(long *mem, long old_size, long new_size)
    {
      long *memnew;
      memnew=realloc(mem,new_size*sizeof(long));
      if(memnew==0)
      {
        fprintf(stderr,"+++ Fehler +++\n");
        exit(-1);
      }
      mem=0;
      return memnew;     
    }
    
    long append (long *mem1, long size1, long *mem2, long size2, long
     *mem3, long size3)
    {
      long i;
      if((size1>size3)||(size2>size3))
      {
        fprintf(stderr,"+++ Fehler +++\n");
        return 0;
      }
      for(i=0;i<size1;i++)
        mem3[i]=mem1[i];
      for(i=0;i<size2;i++)
        mem3[size1+i]=mem2[i];
      return 1;
    }
    thx in advance

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > our_free(test);
    You don't need this free, as the memory for test has already been freed by the realloc() in change_size(). However you would need to add a free(mem) within change_size(), within the if() error block, right before the exit(-1).

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    4

    Talking

    Quote Originally Posted by swoopy View Post
    > our_free(test);
    You don't need this free, as the memory for test has already been freed by the realloc() in change_size(). However you would need to add a free(mem) within change_size(), within the if() error block, right before the exit(-1).
    Thx swoopy ... the code works fine now

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You might consider changing our_free to look like this:
    Code:
    void *our_free(long *mem)
    {
      if(mem)
      {
        free(mem);
        mem=0;
      }
      return mem;
    }
    This way the pointer gets nulled and the result is seen in the calling function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. double free or corruption (fasttop)
    By yougene in forum C Programming
    Replies: 6
    Last Post: 01-17-2009, 06:44 PM
  2. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  3. double free or corruption???
    By hwttdz in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2006, 03:02 PM
  4. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 06:28 PM