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);
}