0

trying to store the results from reading a file that match a criteria, in this case a stop being less than 1000m from a specified latitude and longtitude. I keep getting a segmentation error. Below is the code that is causing trouble and below below is the specific part of it...

    void check_stops(char *array[200][3], double home_lat, double home_lon)
{
  char *location_type;
  char *parent_station;
  char *stop_id;
  char *stop_code;
  char *stop_name;
  char *stop_description;
  double stop_lat;
  double stop_lon;
  char *zone_id;
  int distance;

  const char delimiters[] = ",";
  char* running;

  int count = 0;
  int col = 0;
  int row = 0;

  FILE *fp;
  fp = fopen("stops.txt", "r");

  if (fp != NULL)
  {
    char line[BUFSIZ];

    while (fgets(line, sizeof(line), fp) != NULL)
    {
      if(count > 1)
      {
        char stringcopy[sizeof(line)];
        strcpy(stringcopy, line);

        running = stringcopy;

        /*
        The code below assumes it knows where the lat and lon are in the CSV file.
        This method will not work with GTFS files from other public transport agencies
        since the order of the fields will be different.
        */

        separatestring(&running, delimiters);                         //removes location_type from stringcopy
        separatestring(&running, delimiters);                         //removes parent_station from stringcopy
        stop_id     = separatestring(&running, delimiters);           //gets stops_id
        separatestring(&running, delimiters);                         //removes stop_code from stringcopy
        stop_name   = separatestring(&running, delimiters);           //gets stop_name
        separatestring(&running, delimiters);                         //removes stop_description from stringcopy
        stop_lat    = atof(separatestring(&running, delimiters));     //gets stop_lat
        stop_lon    = atof(separatestring(&running, delimiters));     //gets stop_lon

        distance = haversine(stop_lat, stop_lon, home_lat, home_lon); //calculates distance between stop location and home location
        char p = distance;
        char * d = &p;

        if(distance <= 1000)
        {
          array[row][col] = stop_id;
          array[row+1][col] = stop_name;
          array[row+2][col] = d;

          col++;

        }
       }
        count++;**
      }
     }

      fclose(fp);
       }

The issues arise from this block of code, whenever I remove it, it works fine (omitting the other code that goes along with this) but stops working when I put it in...

        if(distance <= 1000)
    {
      array[row][col] = stop_id;
      array[row+1][col] = stop_name;
      array[row+2][col] = d;

      col++;

    }
   }
  count++;**
}

EDIT:

Fixed! Thanks guys

  • 1
    You're incrementing col. Think about what it does. Also think what `row+1`etc do and what you actually should be doing... – Sami Kuhmonen Sep 06 '15 at 14:33

1 Answers1

1

As a rule of thumb, segmentation fault usually points to wrong access to memory, as is the case, e.g., of buffer overflows. You end up corrupting the memory contents and it's no longer clear what will your program do -just crashing seems after all quite benign then. You should look for how you reference and assign values to your arrays.

In your specific case, you define a 2-dimensional string array as a 200x3 matrix. Also, the counter int col gets incremented for each line that has a distance < 1000m. Presumably the file you read in has way more than just 3 lines.

However, you assign values to the array as

array[row][col] = stop_id;

That seems doomed to write beyond the space reserved for array in memory, hence a segfault: For the fourth hit, this code is tantamount to array[row][3], but array has only 3 colums!

Are you sure, you don't want to reference it as

array[col][row]=stop_id;
array[col][row+1]=stop_name;
array[col][row+2]=d;

?

MASL
  • 699
  • 1
  • 7
  • 20
  • Aren't 2D arrays in C arranged as [Rows][Cols]? So Shouldn't it really save like below and save the values in a new column when it satisfies the condition of <1000m? I am afterall only storing 3 fields :\ (stop_id1) (stop_id2) .... (stop_name1) (stop_name2) (d1) (d2) – Timothy Tan Sep 07 '15 at 04:30
  • Without you providing more details about your error or say what else did you try it's hard to give a more detailed answer -for instance, you could check your code by having it printing the values as it reads & stores them. – MASL Sep 07 '15 at 10:01
  • As wrt 2d arrays in C, the way they are stored and the logic you program are two separate things. It's up to you to decide how you access an array, that is, how your data is structured and how you need/want to save it. – MASL Sep 07 '15 at 10:03
  • For more details on the internals of multi-dimensional arrays in C see http://stackoverflow.com/questions/2565039/how-are-multi-dimensional-arrays-formatted-in-memory and http://stackoverflow.com/questions/7784758/c-c-multidimensional-array-internals – MASL Sep 07 '15 at 10:11
  • Btw, you fixed the problem? If this answer was helpful it'd nice from you if you accept it. – MASL Sep 07 '15 at 10:14
  • No problem. We all were new once. Thanks. – MASL Sep 07 '15 at 13:24