Thread: Segmentation fault reading integers into an array

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    17

    Segmentation fault reading integers into an array

    Well I was just about to give up on this as I was having difficulty debugging it but S and s are not the same ;-). The title basically states what I'm trying to do. The issue is in the first while loop where I am trying to fill the array. I'm going to keep attempting to figure out what's wrong but would love some assistance. Thanks.



    Code:
    #include "prog5.h"
    
    int main(int argc, char *argv[]){
    
      int size, i, matrix_length, prims_length, row, column, edge;
    
      int *matrix, *prims;
    
      FILE *infile;
    
      infile = fopen("prog5.in", "r");
    
      fscanf(infile, "%d", &size);
    
      matrix = (int *)malloc(size*size*sizeof(int));    /*Create an array of
                                                        integers large enough to
                                                        hold the matrix*/
    
      prims = (int *)calloc(size*3,sizeof(int));        /*Create an array of
                                                        integers large enough to
                                                        hold Prim's 3 column chart
                                                        and sets initial to 0*/
    
      matrix_length = sizeof(matrix)/sizeof(int);       /*gets the length of the
                                                                              matrix array*/
    
      prims_length = sizeof(prims)/sizeof(int);         /*gets the length of the
                                                                              matrix array*/
    
      while(!feof(infile)){                             /*while End of File is not
                                                                  true*/
    
          for(i=0; i<matrix_length; i++){
    
            fscanf(infile, "%d", matrix[i]);            /*fills the matrix array*/
    
          }/*for(i=0; i<length; i++){...*/
    
      }/*while(!feof(infile)){...*/
    
      fclose(infile);
    
      row = column = 0;
    
      for(i=0; i < size; i++){
    
          prims[size*row+0] = 1;   /*set current row to visited*/
    
          edge = 1;
    
          for(column = 1; column < size; column++){
    
                if(matrix[size*row+column] != 0 &&
                                   matrix[size*row+column] < prims[size*column+1]){
                     prims[size*column+1] = matrix[size*row+column];
                     prims[size*column+2] = row;
    
                                 /*sets cost and previous column in the prims array
                                 for the current column in the matrix if the cost
                                 is lower*/
    
                }/*if(matrix[size*row+column] != 0) && ...*/
    
                if(matrix[size*row+column] < edge && matrix[size*row+column] != 0){
    
                     edge = matrix[size*row+column];
                     row = column;
    
                                 /*compares edges in the current row setting the
                                 next row to the column with the lowest edge greater
                                 than 0*/
    
                }/*if(matrix[size*row+column] < edge ...*/
    
          }/*for(column = 1; column < size; column++){...*/
    
          printprims(&prims_length, &prims);
    
      }/*while(i=0; i<size; i++){...*/
    
    
      free(matrix);
    
      free(prims);
    
      return 0;
    
    }/*int main(int argc, char *argv[]){...*/

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Despite the name, matrix_length is not the length of your matrix (it is, in fact, 1). Nor is prims_length a length of anything either.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    17
    yeah I figured that out just before looking at this again. they should be size*size.

    And I think fscanf(infile, "%d", matrix[i]); should be fscanf(infile, "%d", &matrix[i]);

    is that right?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bolivartech View Post
    yeah I figured that out just before looking at this again. they should be size*size.

    And I think fscanf(infile, "%d", matrix[i]); should be fscanf(infile, "%d", &matrix[i]);

    is that right?
    Indeed.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    17
    Ok this is going well. It appears to be running like it should but it's not printing what I expect it too. Here is both the main and the print functions. I'm going to look into my pointers, I'm fairly sure it has something to do with that. That maybe I'm printing a location rather than what's there..... Thanks!

    Code:
    #include "prog5.h"
    
    int main(int argc, char *argv[]){
    
      int size, i, matrix_length, prims_length, row, column, edge;
    
      int *matrix, *prims;
    
      FILE *infile;
    
      infile = fopen("prog5.in", "r");
    
      fscanf(infile, "%d", &size);
    
      matrix = (int *)malloc(size*size*sizeof(int));    /*Create an array of
                                                        integers large enough to
                                                        hold the matrix*/
    
      prims = (int *)calloc(size*3,sizeof(int));        /*Create an array of
                                                        integers large enough to
                                                        hold Prim's 3 column chart
                                                        and sets initial to 0*/
    
      matrix_length = size*size;       /*gets the length of the
                                                                              matrix array*/
    
      prims_length = size*3;         /*gets the length of the
                                                                              matrix array*/
    
      while(!feof(infile)){                             /*while End of File is not
                                                                  true*/
    
          for(i=0; i<matrix_length; i++){
    
            fscanf(infile, "%d", &matrix[i]);            /*fills the matrix array*/
    
          }/*for(i=0; i<length; i++){...*/
    
      }/*while(!feof(infile)){...*/
    
      fclose(infile);
    
      row = column = 0;
    
      for(i=0; i < size; i++){
    
          prims[size*row+0] = 1;   /*set current row to visited*/
    
          edge = 1;
    
          for(column = 1; column < size; column++){
    
                if(matrix[size*row+column] != 0 &&
                                   matrix[size*row+column] < prims[size*column+1]){
                     prims[size*column+1] = matrix[size*row+column];
                     prims[size*column+2] = row;
    
                                 /*sets cost and previous column in the prims array
                                 for the current column in the matrix if the cost
                                 is lower*/
    
                }/*if(matrix[size*row+column] != 0) && ...*/
    
                if(matrix[size*row+column] < edge && matrix[size*row+column] != 0){
    
                     edge = matrix[size*row+column];
                     row = column;
    
                                 /*compares edges in the current row setting the
                                 next row to the column with the lowest edge greater
                                 than 0*/
    
                }/*if(matrix[size*row+column] < edge ...*/
    
          }/*for(column = 1; column < size; column++){...*/
    
          printprims(&prims_length, &prims);
    
      }/*while(i=0; i<size; i++){...*/
    
    
      free(matrix);
    
      free(prims);
    
      return 0;
    
    }/*int main(int argc, char *argv[]){...*/
    Code:
    #include "prog5.h"
    
    void printprims(int *prims_length, int *prims[]){
    
         int j;
    
         for(j=0; j<*prims_length; j=j+3){
    
              printf("%d %d %d\n", prims[j],prims[j+1],prims[j+2]);
    
         }/*for(j=0; j<prim_length; j=j+3){...*/
    
         printf("\n");
    
    }/*void printprims(int *prims_length, int *prims[]){...*/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert array of characters in file to integers
    By millsy5 in forum C Programming
    Replies: 4
    Last Post: 10-02-2009, 11:41 AM
  2. reading bitmap into array
    By kallistine in forum C Programming
    Replies: 35
    Last Post: 07-26-2009, 03:44 PM
  3. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  4. Replies: 3
    Last Post: 07-17-2008, 08:45 AM
  5. Annoying Segmentation Fault
    By Zildjian in forum C++ Programming
    Replies: 7
    Last Post: 10-08-2004, 02:07 PM