(fscanf(file, "%lf", &num) > 0) and segmentation fault in C
I'm modifying a piece of the source code of bind, specifically the random
order section of the rdataset.c file, which is below:
for (i = 0; i < count; i++) {
dns_rdata_t rdata;
isc_uint32_t val;
isc_random_get(&val);
choice = i + (val % (count - i));
rdata = shuffled[i];
shuffled[i] = shuffled[choice];
shuffled[choice] = rdata;
if (order != NULL)
sorted[i].key = (*order)(&shuffled[i], order_arg);
else
sorted[i].key = 0; /* Unused */
sorted[i].rdata = &shuffled[i];
}
I change the line with choice and let that variable be taken from a
function like this
choice=weightCal();
and the code of function is
unsigned int weightCal() {
FILE *file = fopen("weight.txt", "r");
double integers[10],prob[10];
unsigned int i=0,j=0,k=0;
double sum=0,subSum=0,num;
unsigned int result=0;
while(fscanf(file, "%lf", &num) > 0) {
integers[i] =num;
sum+=num;
i++;
}
rewind(file);
while(fscanf(file, "%lf", &num) > 0){
subSum=subSum+num;
prob[j]= subSum / sum;
j++;
}
srand(time(NULL));
double r = rand() / (double)RAND_MAX;
for(k=0;k<sizeof(prob)/sizeof(double);k++) {
if (r < prob[k]) {
result=k;
break;
}
}
fclose(file);
return result;
}
then I recompile bind. The compilation works but when I use the command :
dig www.example.com. @127.0.0.1
it returns the error "Segmentation fault (core dumped)." I tried to debug
it and the debugger told me that the error is in the line
while(fscanf(file, "%lf", &num) > 0)
How can I fix this error?
No comments:
Post a Comment