我正在修改一段bind的源代码,特别是rdataset.c文件的随机顺序部分,如下:
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];
}
我用选择更改了行,并让该变量取自这样的函数
choice=weightCal();
功能代码是
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;
}
然后我重新编译绑定。编译有效,但是当我使用命令时:
dig www.example.com. @127.0.0.1
它返回错误“分段错误(核心转储)”。我试图调试它,调试器告诉我错误在行
while(fscanf(file, "%lf", &num) > 0)
我该如何解决这个错误?