The only thing that can be said for certain is that the corruption is occurring in the heap. If you would like useful feedback, post your code instead of hiding it behind comments. And, please, use code tags in your post to preserve the indentation.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int random_no(int);
void calc_file_row_col(FILE *,int *,int *);
int main()
{
int i,j,z;
int temp=0;
int row,column;
FILE *fp;
char x,**a;
for(z=0;z<2;++z){
fp=fopen("poly.dat","r");
calc_file_row_col(fp,&row,&column); //Function which calculates
// the no of rows and column of a file
a=calloc(row,sizeof(char *)); // Declare a 2D array of size
// row by column
for(i=0;i<row;i++)
a=calloc(column,sizeof(char));
for(i=0;i<row;i++) // Read the elements of file fp into
{ // array a
for(j=0;j<column+1;j++)
{
x=getc(fp);
if(x=='\n')
{}
else
{
*(*(a+i)+j)=x;
}
}}
fclose(fp);
fseek(fp,0,SEEK_SET);
temp=random_no(row); // Generates a random no. between 0 and row
printf("temp=%d\n",temp);
fp=fopen("poly.dat","w");
for(i=0;i<temp;i++) //Write elements from 0 to temp and then temp to row in file fp;
{
for(j=0;j<column;j++)
fprintf(fp,"%c",a[j]);
fprintf(fp,"\n");
}
for(i=temp+1;i<row;i++)
{
for(j=0;j<column;j++)
fprintf(fp,"%c",a[j]);
fprintf(fp,"\n");
}
fclose(fp);
for(i=0;i<row;i++)
free(a);
free(a);
a=NULL;
}
return 0;
}
int random_no(int range)
{
int LOW=0;
int r;
time_t seconds;
time(&seconds);
srand((unsigned int)seconds);
r = rand() % (range - LOW + 1) + LOW;
return r%range;
}
void calc_file_row_col(FILE *poly,int *row,int *column)
{
int m=0;
int n=0;
char x;
fseek(poly,0,SEEK_SET);
while((x=getc(poly))!=EOF)
{
if(x=='\n'){
m++;
n=0;
}
else{
n++;
*column=n;
}
}
*row=m;
printf("row=%d column=%d\n",*row,*column);
fseek(poly,0,SEEK_SET);
}
For production code, definitely, add the error handling code. For unimportant, personal stuff, I'm sure we all cut corners from time to time.
In this case, unless I'm mistaken, the first calloc of the second loop iteration never returns. When it calls malloc, malloc aborts after detecting corruption caused by fseek writing to memory freed by fclose.
It is still better to promote good coding practices IMO...be it dev or prod code among newbie developers otherwise old timers like me get stuck with a lot of cleanup...
I tried the code without using fseek but same thing happens.....as I am still in a nascent stage of learning, I am unable to ascertain the usefulness of strace....any help would be great.
Hmmm. With your code I was able to reproduce the malloc() corruption error; removing the fseek() call with the pointer to memory free()'d by fclose() fixed the issue.