Connecting Tech Pros Worldwide Forums | Help | Site Map

"Access Violation with segmentation errors"!

Member
 
Join Date: Jun 2007
Posts: 57
#1: Oct 7 '07
Hi,
I have just spent many hours debugging my code and now when I run it I get the following error:

"An access violation (Segmentation fault) raised in your program"

I have researched on this and found that it is usually the result of faulty pointers or memory that is not allocated. However I have allocated all of my memory corectly (I feel) and my pointers look to be correct.. Is there another reason my code could be causing this error? My code is below:

Expand|Select|Wrap|Line Numbers
  1. /**-----------------------------------------------------------------------------
  2.  *                                                                              
  3.  * Title: Matrix Check
  4.  *
  5.  * File: MatrixCheck.c
  6.  *
  7.  * Author: Andrew Bullen (30562147)
  8.  *
  9.  * Date: 1st October 2007
  10.  *
  11.  * Program Function: Reads in data on a boolean matrix (/s) and outputs
  12.  *         whether the file has parity, is corrupt or the coordinates of the bit
  13.  *         we can change to give it parity.
  14.  *
  15.  -----------------------------------------------------------------------------*/
  16.  
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <malloc.h>
  22.  
  23.  
  24.  
  25. /*----------------------------------------------------------------------------*/
  26.  
  27. // Sets values in place of booleans.
  28. #define TRUE 1
  29. #define FALSE 0
  30.  
  31. // Numbers of uneven rows / columns in a matrix for the properties.
  32. #define PARITY 1
  33. #define CHANGE 2
  34. #define CORRUPT 3
  35. #define INIT 0
  36. #define FILENAMEL 100
  37.  
  38.  
  39.  
  40. /*----------------------------------------------------------------------------*/
  41.  
  42.  
  43. FILE *readMatrix(FILE *fp, int **matrixPointer, int rows, int columns, int end);
  44. int **setMem(int **matrixPointer, int rows, int columns);
  45. int testRows(int **matrixPointer, int rowLocation, int rows, int columns);
  46. int testColumns(int **matrixPointer, int columnsLocation, int rows, int columns);
  47. void outputResults(int rowUnevens, int columnUnevens, int columnLocation, int rowLocation);
  48. void freeMem(int **matrixPointer, int rows, int columns);
  49.  
  50.  
  51. /*----------------------------------------------------------------------------*/
  52.  
  53.  
  54. int main(int argc, char **argv)
  55. {
  56.     int matrixNumber = INIT;
  57.     int rows = INIT;
  58.     int columns = INIT;
  59.     int rowLocation = INIT;
  60.     int columnLocation = INIT;
  61.  
  62.     int end = FALSE;
  63.  
  64.     FILE *fp = NULL;
  65.  
  66.     int **matrixPointer = NULL;
  67.  
  68.     do
  69.     {
  70.            matrixNumber++;
  71.  
  72.            fp = readMatrix(fp, matrixPointer, rows, columns, end);
  73.  
  74.            if (end != TRUE)
  75.            {
  76.                 int rowUnevens = testRows(matrixPointer, rowLocation, rows, columns);
  77.                 int columnUnevens = testColumns(matrixPointer, columnLocation, rows, columns);
  78.  
  79.                 printf("%d) \n", matrixNumber);
  80.                 outputResults(rowUnevens, columnUnevens, columnLocation, rowLocation);
  81.  
  82.            }           
  83.            freeMem(matrixPointer, rows, columns);
  84.  
  85.     }while(!feof(fp));
  86.  
  87.     return 0;   
  88. }
  89.  
  90.  
  91.  
  92. /*----------------------------------------------------------------------------*/
  93.  
  94.  
  95.  
  96. FILE *readMatrix(FILE *fp, int **matrixPointer, int rows, int columns, int end)
  97. {
  98.      char fileName[FILENAMEL]; // Character array for file-name string.   
  99.      fp = fopen(fileName, "r"); // Opens file with given name in 'read-only'.
  100.      int index1 = INIT;
  101.      int index2 = INIT;
  102.  
  103.      if(!feof(fp))
  104.      {
  105.             end = FALSE;      
  106.  
  107.             /* Reads numbers from file for values of 'rows' and 'columns' on the first 
  108.                line of input. */
  109.                fscanf(fp, "%d", &rows);
  110.                fscanf(fp, "%d", &columns);
  111.  
  112.                 // Allocates the memory for the matrixPointer array.
  113.                 matrixPointer = setMem(matrixPointer, rows, columns);
  114.  
  115.                 for(index1 = INIT; index1 < rows; index1++)// Loops through rows.
  116.                 {
  117.                      for (index2 = INIT; index2 < columns; index2++) // Loops through columns.
  118.                      {
  119.                          /* Inputs value at that point of file and stores in appropriate
  120.                             point in the matrix array according to current index. */
  121.                         fscanf(fp, "%d", &matrixPointer[index1][index2]);
  122.                       }
  123.                 }
  124.      }
  125.      else
  126.      {
  127.          end = TRUE;
  128.      }
  129.  
  130.  return fp;   
  131. }
  132.  
  133.  
  134. /*----------------------------------------------------------------------------*/
  135.  
  136.  
  137.  
  138. int **setMem(int **matrixPointer, int rows, int columns)
  139. {
  140.     int index1 = INIT;
  141.  
  142.     // Allocate memory for each row.
  143.     *matrixPointer = (int *)calloc(rows, sizeof(int *));
  144.  
  145.     // Allocate memory for each columns within each row. 
  146.     for (index1 = INIT; index1 < rows; index1++)
  147.     {
  148.         *matrixPointer[index1] = (int)calloc(columns, sizeof(int));
  149.     }
  150.  
  151.     return matrixPointer;
  152. }
  153.  
  154.  
  155.  
  156. /*----------------------------------------------------------------------------*/
  157.  
  158.  
  159.  
  160. int testRows(int **matrixPointer, int rowLocation, int rows, int columns)
  161. {
  162.     int counterRows = INIT;
  163.     int unevens = INIT;
  164.     int index1 = INIT;
  165.     int index2 = INIT;
  166.  
  167.     for(index1 = INIT; index1 < rows; index1++)// Loops through rows.
  168.      {
  169.           counterRows = INIT; 
  170.  
  171.           for (index2 = INIT; index2 < columns; index2++) // Loops through columns.
  172.           {
  173.               //Adds values of pointer as a counter of 1s. 
  174.               counterRows += matrixPointer[index1][index2];
  175.           }
  176.           if ((counterRows % 2) != 0)// If counter is not evenly divisible by 2.
  177.           {
  178.               unevens++;  // Number of uneven rows count increases.   
  179.               rowLocation = index2; // Coordinate of changeBit is altered.        
  180.           }        
  181.      }
  182.  
  183.     return unevens;
  184. }
  185.  
  186.  
  187. /*----------------------------------------------------------------------------*/
  188.  
  189.  
  190. int testColumns(int **matrixPointer, int columnsLocation, int rows, int columns)
  191. {
  192.     int counterRows = INIT;
  193.     int unevens = INIT;
  194.     int index1 = INIT;
  195.     int index2 = INIT;
  196.     int columnLocation = INIT;
  197.  
  198.     for(index1 = INIT; index1 < rows; index1++)// Loops through rows.
  199.      {
  200.           counterRows = INIT; 
  201.  
  202.           for (index2 = INIT; index2 < columns; index2++) // Loops through columns.
  203.           {
  204.               //Adds values of pointer as a counter of 1s. 
  205.               counterRows += matrixPointer[index2][index1];
  206.           }
  207.           if ((counterRows % 2) != 0)// If counter is not evenly divisible by 2.
  208.           {
  209.               unevens++;  // Number of uneven rows count increases.
  210.               columnLocation = index2; // Coordinate of changeBit is altered.           
  211.           }        
  212.      }
  213.  
  214.     return unevens;
  215. }
  216.  
  217.  
  218.  
  219. /*----------------------------------------------------------------------------*/
  220.  
  221.  
  222.  
  223. void outputResults(int rowUnevens, int columnUnevens, int columnLocation, int rowLocation)
  224. {
  225.      // If there are no rows/columns with uneven numbers.
  226.      if((rowUnevens == PARITY) && (columnUnevens == PARITY))
  227.      {
  228.           printf("Matrix has parity property. \n\n");          
  229.      }        
  230.      else
  231.      {
  232.           /* If 1 row and column is uneven then it uses the coordinate for 
  233.              change-bit*/
  234.           if((rowUnevens == CHANGE) && (columnUnevens == CHANGE))
  235.           {
  236.                // Prints bit-change location to user from recorded values.
  237.                printf("Change bit (%d , %d). \n\n", rowLocation, columnLocation);       
  238.           }
  239.           else
  240.           {
  241.                printf("Matrix is corrupt. \n\n");
  242.           }
  243.      }             
  244.  
  245.      return;                 
  246. }
  247.  
  248.  
  249. /*----------------------------------------------------------------------------*/
  250.  
  251.  
  252. void freeMem(int **matrixPointer, int rows, int columns)
  253. {
  254.      int index1 = INIT;
  255.  
  256.      for (index1 = INIT; index1 < rows; index1++)// Loops through rows.
  257.      {
  258.          // Frees memory of each array in the matrix and sets to NULL.
  259.          free(matrixPointer[index1]);
  260.          matrixPointer[index1] = NULL; 
  261.      }
  262.      // Frees memory of the matrix.
  263.      free(matrixPointer);
  264.      matrixPointer = NULL;
  265.  
  266.      return;    
  267. }
  268.  
  269.  
  270. /*----------------------------------------------------------------------------*/
  271.  
  272.  
  273.  
when I debug it stops at the line:

if(!feof(fp))

if thats any help :-).

I will keep looking over it to try and find the problem but i definately NEED help pretty bad.

sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#2: Oct 9 '07

re: "Access Violation with segmentation errors"!


Quote:

Originally Posted by Dameon99

when I debug it stops at the line:

if(!feof(fp))

Did you check to make sure the open was successful before you tried to see if it was an EOF?
Reply