The print statements were added to see where the seg fault is. I'm having trouble with it, because the statements prints to show that i= 1999, which means it should quit the while loop after it is incremented and continue to the next print statement. I understand a seg fault has to do with accessing memory, but in this case there isn't any array or pointer that would cause a normal seg fault in between the "hi2" and "out of loop" print statements. Here is more code if it helps:
Code:
void sort(struct hostfreq hostCon[2001]){
FILE *host_freq;
host_freq= fopen("hostfreq.txt", "a");
FILE *host_name;
host_name= fopen("hostname.txt", "a");
//printf("in sort\n");
struct hostfreq temp[1];
//printf("defined temp\n");
int i, j = 0;
for(i; i<=1999; i++){
j=i+1;
for(j; j<=1999;j++){
if(hostCon[j].freq>hostCon[i].freq){
temp[0] = hostCon[j];
hostCon[j] = hostCon[i];
hostCon[i]=temp[0];
}
}
}
i = 0;
while(i<2000){
printf("Host name %s : %i \n i is %i\n", hostCon[i].hostName, hostCon[i].freq, i);
fprintf(host_freq, "%i\n", hostCon[i].freq);
printf("hi1\n");
fprintf(host_name, "%s", hostCon[i].hostName);
printf("hi2\n");
i++;
}
printf("out of for loop");
fclose(host_freq);
fclose(host_name);
int total = sum(hostCon);
printf("sort done");
CDF(hostCon, total);
}