I have a code
distance = (double**)malloc(city_count*sizeof(double*));
for(i=0; i<city_count; i++)
{
distance[i] = (double*)malloc(city_count*sizeof(double));
}
for(i=0; i<city_count; i++)
{
for(j=0; j<city_count; j++)
{
distance[i][j] = 1; // fscanf(fp, "%d", &tmp); distance[i][j] = tmp; EDITED
}
}
Then I debug it in Visual Studio it works just fine. But on a real cluster it always filled with zeros. Can anybody help me?
Problem with assignment to matrix, not in reading files.
Update
With your simplified problem statement (thanks for that) it pretty much leaves checking the pointer values returned by malloc to make sure they’re not NULL (which would be very surprising unless city_count is really large.)
Old Update
Doh! I just noticed. %d reads an int which doesn’t match the type of &tmp. Should be:
fscanfisn’t typesafe so this type of mismatch usually causes a problem.Post Facto Edit
And make sure you’re not making the same mistake passing "%d" to
printf. 😉End Update
Make sure
city_countisn’t0.Check
fscanf‘s return value (and the value of tmp). Are you sure this file read is succeeding? From the docs at cplusplus.com