Thursday, 12 September 2013

Segmenatation fault in fread

Segmenatation fault in fread

I encounter segmenatation fault in fread while I am reading 1500 or more
ethernet data packets. Here "test2" is the binary file whose file size is
22.6MB. 1132 is the number of useful data points in each packet and 142
points carries header information hence it is skipped.
here is the main program:
void main()
{
int count;
FILE *fp;
long file_size;
unsigned char rawdata[1132];
fp=fopen("test2","rb");
if(fp==-1)
{
printf("unsucessful");
exit(0);
}
long int before=ftell(fp);
fseek(fp,0,SEEK_END);
file_size=ftell(fp);
rewind(fp);
long int after=ftell(fp);
//skip first 142 bytes(header information)since its not required
fseek(fp,142,SEEK_SET);
long int s=ftell(fp);
int length_of_fft=4096;
int buffer_width=128;
int buffer_depth=1024;
int k,aa,payloadindex=0,l=0,j,a;
int no_of_data_pts_to_be_read=1132;
int no_of_ethernet_pkts_to_be_read=1500;
int q=no_of_ethernet_pkts_to_be_read*buffer_depth;
unsigned char payload[q];
unsigned int payloadint[q];
int no_of_data_pks_read=0;
int reading_for_first_time=1;
unsigned char
data_from_file[no_of_ethernet_pkts_to_be_read][buffer_depth];
int addr_offset_in_inarray=0;
int udp_counter_values[no_of_ethernet_pkts_to_be_read];
unsigned int rawdataint[1132];
long int size;
count=0;
for(a=0; a<no_of_ethernet_pkts_to_be_read; a++)
{
int p=fread(rawdata,1 ,sizeof(rawdata), fp);
count=p;
//----------- to check if all data points have been read, i,e the
pointer must be at a position wich is a multiple of 1132 which
is[(1274-142=1132)*(a+1)],( since 142 bytes were skipped in the
beginning )
printf("\n\n %d\t Start=%x\t\t Stop=%x\t Count=%d\t
Address=%x",no_of_data_pks_read, rawdata[0],
rawdata[sizeof(rawdata)-1],count,
ftello(fp));
if(count==no_of_data_pts_to_be_read)
{
printf("\nNumber of data points read in packet %d (of %d) is
%d ",no_of_data_pks_read, no_of_ethernet_pkts_to_be_read,
count);
reading_for_first_time=0;
//--------------converting char array rawdata into int array
and then call udp
for(i=0;i<1132;i++)
rawdataint[i]=rawdata[i]-'\0';
udp_counter_values[a]=check_UDPpacketCount(&addr_offset_in_inarray,
&rawdataint,10,no_of_data_pks_read,1132);
// printf("\n--------udp:: %d ",udp_counter_values[a]);
//-----------------create new mat and compute payload and put
the contents of array rawwdata into the respective row of the
new matrix
int k,t,w,time=0;
for(k=0,l=addr_offset_in_inarray;l<sizeof(rawdata),k<1024;k++,l++)
{
data_from_file[no_of_data_pks_read][k]=rawdata[l];
// printf("\n
datafile:%d",data_from_file[no_of_data_pks_read][k]);
}
for(t=0;t<1024;t++)
{
payload[payloadindex++]=data_from_file[no_of_data_pks_read][t];
}
no_of_data_pks_read++;
}
else
{
count=0;
printf("\n not equal, exiting ");
exit(0);
}
}
//------convert payload to int array and send to data extraction function
for(i=0;i<sizeof(payload);i++)
{
payloadint[i]=payload[i]-'\0';
}
printf(" sizepayload: %d", sizeof(payload));
size=sizeof(payload);
data_extraction(size,
payloadint,buffer_depth,buffer_width,length_of_fft);
printf("\n s:%d",file_size);
printf("\n ft:%x",ftell(fp));
printf("\n****----end----****");
fclose(fp);
}

No comments:

Post a Comment