-1

I'm doing a homework for learning graphs. For that, I'm taking a csv file and I keep that information in a Sensor structure.

#define sensor_amount 70
typedef struct Sensor {
    int id;
    float x,y,z;
}Sensor; 

For getting this struct from file I'm using the following function:

Sensor* get_sensors(char* file_name){
    FILE* file = fopen(file_name,"r");
    //Skip first line
    char* c;
    fscanf(file,"%[^\n]",c);

    int id = 0;
    float x,y,z;

    Sensor* sensor_arr = malloc(sizeof(Sensor));
    fscanf(file,"%i,%f,%f,%f",&id,&x,&y,&z);
    sensor_arr[0].id = id;
    sensor_arr[0].x = x;
    sensor_arr[0].y = y;
    sensor_arr[0].z = z;

    int counter = 1;

    while(!feof(file)){
        fscanf(file,"%i,%f,%f,%f\n",&id,&x,&y,&z);
        ++counter;
        sensor_arr = realloc(sensor_arr,counter*sizeof(Sensor));
        sensor_arr[counter-1].id = id;
        sensor_arr[counter-1].x = x;
        sensor_arr[counter-1].y = y;
        sensor_arr[counter-1].z = z; 
    }
    fclose(file);
    return sensor_arr;
}

I'm calculating distances between each sensor with following code:

float** get_distances(Sensor* s){
    float** a = malloc(sensor_amount*sizeof(float*));

    for(int i = 0; i < sensor_amount;i++)
        a[i] = malloc(sensor_amount*sizeof(float));

    for(int i = 0; i < sensor_amount;i++){
        for(int j = 0; j < sensor_amount; j++){
            float dis = distance(s[i].x,s[i].y,s[j].x,s[j].y);
            a[i][j] = dis;
        }
    }
    return a;
}

Finally in my main i print those values like this:

int i,j;
int main(){
    char file_name[] = "sensor_locations.csv";
    Sensor* sensors; 
    sensors = get_sensors(file_name);
    float**ar=get_distances(sensors);
    for(i=0;i < 70; ++i)
        for(j=0;j<70;++j){
            printf("(%i,%i)->%f\n",i,j,ar[i][j]);
    }
    return 0;
}

In main, if I move the declarations of i and j to the for loops, it throws a segmentation fault. But why?

  • Please show a [mcve] and not code snippets that must be stiched together. Also learn how to use your debugger. – Jabberwocky Nov 12 '19 at 11:39
  • 1
    `char* c; fscanf(file,"%[^\n]",c);` is invalid, you are using unintialized pointer `c` to store the first line of input. You should add error checking to your code. Each `malloc` can fail, each `fscanf` can fail, a `fopen` can fail - you just ignore errors. – KamilCuk Nov 12 '19 at 11:41
  • Another probable cause: your indexes might be out of range, add some code that checks that. – Jabberwocky Nov 12 '19 at 11:42
  • The hard-wired occurrences of 70 are red flags. How does what know the size of anything? It isn’t obvious how anything works. But the symptoms (the crash) point to undefined behaviour. – Jonathan Leffler Nov 12 '19 at 11:43
  • please describe, what the `distance` function does in this statement? `float dis = distance(s[i].x,s[i].y,s[j].x,s[j].y);` – VJAYSLN Nov 12 '19 at 12:13

1 Answers1

0

This is designed for array out of bounds bugs:

int counter = 1;
...
++counter;
...
[counter-1]

Instead do

for(int i=0; more_data; i++)
{
  sensor_arr[i] = ...;
  sensor_arr = realloc(sensor_arr,(i+2)*sizeof(Sensor));
}

Please note Why is “while ( !feof (file) )” always wrong?.

And your use of realloc is wrong, use a tmp pointer for storing the result and check it against NULL before assigning it back to the sensor_arr pointer.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • It appears that the problem really was my usage of feof(file). I writed a function for count lines in the file and in that function I used the return value of fgets. Thank you very much. – Berkay Ülke Nov 13 '19 at 08:17