Thread: segmentation fault in free()

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    163

    segmentation fault in free()

    I got segmentation fault when I tried to call function free. may i know what is the most likely problem?

    Example of my code:

    unsigned int *temp;
    temp = calloc(128,sizeof(unsigned int));

    free(temp);

    Below is the actual code
    Code:
    void countingsort2(unsigned int *occ, unsigned int *a, unsigned int length) 
    {
      unsigned int i, left=0, right=0, j, LCP;
      unsigned int *temp, *b=NULL, *tempa=NULL;
      void *t;
    
      temp = calloc(128,sizeof(unsigned int)); 
      //  temp = (unsigned int*) malloc (128*sizeof(unsigned int));
      t = realloc(tempa, length*sizeof(unsigned int));  // create an array which stores the char position in the string
      if (t != NULL)
        tempa = t;
    
      for (i=0; i<128; i++)
        {
          if (occ[i] >= 1)
        {    
          //      printf("occ[%c]%d\n",i,occ[i]);
          right = left + occ[i] - 1;  //right is the right boundary of the alphabet
          t = realloc(b, occurrence[i]*sizeof(unsigned int));  // create an array which stores the char position in the string
          if (t != NULL)
            b = t;
    
          for (j=0; j<128; j++) // initialise temp array                 
            temp[j] = 0;
    
          for (j=left; j<=right; j++)  // counting the occurrences of the 3rd char belonging to a char
            {                 
              if ((a[j]+1)!=filelength)
                {
                  temp[(unsigned int)str[a[j]+1]&0x7F] =  temp[(unsigned int)str[a[j]+1]&0x7F] + 1;
                }        
            }
    
          for (j=1; j<128;j++)        
            temp[j] =  temp[j] + temp[j-1];
    
          for (j=(right+1); j>=(left+1); j--)
            {        
              if ((a[j-1]+1)!=filelength)
            {
              b[temp[(unsigned int)str[a[j-1]+1]&0x7F]-1] = a[j-1];     
              //          printf("a%d suffixes%d j-1:%d\n",temp[(unsigned int)str[suffixes[j-1]]&0x7F]-1,suffixes[j-1], j-1);
              temp[(unsigned int)str[a[j-1]+1]&0x7F] = temp[(unsigned int)str[a[j-1]+1]&0x7F] - 1;
            }
              else
            {
              //          printf("p%d ", j);
              b[right-left] = a[j-1];
            }    
            }
             for (j=left; j<=right; j++)        
           tempa[j] = b[j-left];
        }
          left += occ[i];
        }
      if (a[length]==filelength)
        {
          tempa[length] = a[length];
        }
      
      for (j=0; j<length; j++)  //no need to check for the last element in the array a coz there's no next element for it to compare with
        {
          //   printf("%d ", a[j]);      
          LCP = 0;
          if (GetReadBit(&str[tempa[j]]) && GetReadBit(&str[tempa[j]-1]))
        {
          if (str[tempa[j]] == str[tempa[j]-1])
            {} //handle situation 3. But situation 3 too complicated to handle
          else
            {           
              NormSit(length, &LCP,j, tempa);
            }
        }
        }     
             
    
    
      free(tempa);
      free(temp);
      free(b);  
      }

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    i tried to remove the free functions and my program still got segmentation fault! why is it so?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Because your error isn't with free, it's you trashing your memory some place else.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    Quote Originally Posted by quzah
    Because your error isn't with free, it's you trashing your memory some place else.

    Quzah.
    Can you elaborate more on trashing memory? And do I need to free array if it is created in a function? Or when the function call ended, the array will automatically free itself?

    I call this function multiple times and when I call it, dynamic array will be created in this function. Does this repeating creating of dynamic array whenever the function is called cause the segmentation fault??

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Reformatted
    Code:
    void countingsort2(unsigned int *occ, unsigned int *a, unsigned int length)
    {
        unsigned int i, left = 0, right = 0, j, LCP;
        unsigned int *temp, *b = NULL, *tempa = NULL;
        void *t;
    
        temp = calloc(128, sizeof(unsigned int));
        // temp = (unsigned int*) malloc (128*sizeof(unsigned int));
        // create an array which stores the char position in the string
        t = realloc(tempa, length * sizeof(unsigned int));
        if (t != NULL)
            tempa = t;
    
        for (i = 0; i < 128; i++) {
            if (occ[i] >= 1) {
                //      printf("occ[%c]%d\n",i,occ[i]);
                right = left + occ[i] - 1;  //right is the right boundary of the alphabet
                // create an array which stores the char position in the string
                t = realloc(b, occurrence[i] * sizeof(unsigned int));
                if (t != NULL)
                    b = t;
    
                for (j = 0; j < 128; j++)   // initialise temp array
                    temp[j] = 0;
    
                // counting the occurrences of the 3rd char belonging to a char
                for (j = left; j <= right; j++)
                {
                    if ((a[j] + 1) != filelength) {
                        temp[(unsigned int) str[a[j] + 1] & 0x7F] =
                            temp[(unsigned int) str[a[j] + 1] & 0x7F] + 1;
                    }
                }
    
                for (j = 1; j < 128; j++)
                    temp[j] = temp[j] + temp[j - 1];
    
                for (j = (right + 1); j >= (left + 1); j--) {
                    if ((a[j - 1] + 1) != filelength) {
                        b[temp[(unsigned int) str[a[j - 1] + 1] & 0x7F] - 1] =
                            a[j - 1];
                        //          printf("a%d suffixes%d j-1:%d\n",
                        //               temp[(unsigned int)str[suffixes[j-1]]&0x7F]-1,suffixes[j-1], j-1);
                        temp[(unsigned int) str[a[j - 1] + 1] & 0x7F] =
                            temp[(unsigned int) str[a[j - 1] + 1] & 0x7F] - 1;
                    } else {
                        //          printf("p%d ", j);
                        b[right - left] = a[j - 1];
                    }
                }
                for (j = left; j <= right; j++)
                    tempa[j] = b[j - left];
            }
            left += occ[i];
        }
        if (a[length] == filelength) {
            tempa[length] = a[length];
        }
    
        //no need to check for the last element in the array a coz there's no next 
        //element for it to compare with
        for (j = 0; j < length; j++)
        {
            //   printf("%d ", a[j]);
            LCP = 0;
            if (GetReadBit(&str[tempa[j]]) && GetReadBit(&str[tempa[j] - 1])) {
                if (str[tempa[j]] == str[tempa[j] - 1]) {
                }                   //handle situation 3. But situation 3 too complicated to handle
                else {
                    NormSit(length, &LCP, j, tempa);
                }
            }
        }
    
        free(tempa);
        free(temp);
        free(b);
    }
    Care of
    Code:
    indent --no-tabs --indent-level4 --k-and-r-style hello.c
    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. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  3. segmentation fault (calloc, free)
    By kentadams in forum C Programming
    Replies: 2
    Last Post: 09-07-2007, 08:50 AM
  4. strcat segmentation fault
    By captain-cat in forum C Programming
    Replies: 3
    Last Post: 07-20-2004, 10:29 AM
  5. segmentation fault
    By incognito54 in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:04 PM