0

I have a function that should read from a file, create data structure and return. the function works, just before the return line everything looks good, and the structure looks okay. but then, the function fail with-

"Run-Time Check Failure #2 - Stack around the variable 'output' was corrupted."

The file contains info about a power stations and cities.(output, location, name and etc.) Some of the rows are cities that and some are power stations, which differ with the last integer in the row (or lack thereof). if its exists (lets call him X), this line is a power station and the next X lines are the cities connected to it.

this func. should create pointer to station pointers (station **) with all the cities connected to each station.

station** read_from_file(FILE *file , station **power_grid){

    int output  , cities_connected ,i, counter = 0 ,j =0;
    double x , y;
    char name[256] = {0};
    station *st;


    while (fscanf(file, "%*c%[^\"]%*c%lf%lf%lf%d\n", name, &output, &x, &y, &cities_connected) != EOF){
        counter++;
        for( i = 0; i < cities_connected; i++){
            fscanf(file , "%*c%[^\"]%*c%lf%lf%lf\n" , name , &output , &x ,&y);
        }
    }

    power_grid = (station **)malloc(sizeof(station *)* counter);

    rewind(file);

    while (fscanf(file, "%*c%[^\"]%*c%lf%lf%lf%d\n", name, &output, &x, &y, &cities_connected) != EOF)
    {
        st = (station *)malloc(sizeof(station));
        st->capacity = output;
        st->cities_list = NULL;
        st->num_of_cities = cities_connected;
        st->name = (char *)malloc(strlen(name));
        strcpy(st->name , name);
        st->location[0] = x;
        st->location[1] = y;

        st->cities_list = (city **)malloc(sizeof(city *)*cities_connected);

        for( i = 0; i < cities_connected; i++){
            fscanf(file , "%*c%[^\"]%*c%lf%lf%lf\n" , name , &output , &x ,&y);
            st->cities_list[i] = (city *)malloc(sizeof(city));
            st->cities_list[i]->consumption = output;
            st->cities_list[i]->location[0] = x;
            st->cities_list[i]->location[1] = y;
            st->cities_list[i]->name = (char *)malloc(strlen(name)+1);
            strcpy(st->cities_list[i]->name , name);
        }
        power_grid[j] = st;
        j++;
    }

    fclose(file);
    return;

}

station and city structs-

typedef struct city {
    char * name;
    double location[2];
    double consumption; 
}city;

typedef struct station {
    char * name;
    double location[2];
    city ** cities_list;
    int num_of_cities;
    double capacity;    
}station; 

Tested file - here

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Liad Sagi
  • 63
  • 9

1 Answers1

0

Your types do not match here:

fscanf(file, "%*c%[^\"]%*c%lf%lf%lf%d\n", name, &output, ...

since output is declared as an int, while you use %lf. Change it to %d.

Moreover, when you allocate memory for sr->name, you do not allocate enough space for the name itself, and the null terminator.

As a result, change this:

st->name = (char *)malloc(strlen(name));

to this:

st->name = malloc(strlen(name) + 1);

Notice that I didn't cast what malloc() returned, and you shouldn't either (Do I cast the result of malloc?).

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
gsamaras
  • 71,951
  • 46
  • 188
  • 305