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
- /**-----------------------------------------------------------------------------
- *
- * Title: Matrix Check
- *
- * File: MatrixCheck.c
- *
- * Author: Andrew Bullen (30562147)
- *
- * Date: 1st October 2007
- *
- * Program Function: Reads in data on a boolean matrix (/s) and outputs
- * whether the file has parity, is corrupt or the coordinates of the bit
- * we can change to give it parity.
- *
- -----------------------------------------------------------------------------*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- /*----------------------------------------------------------------------------*/
- // Sets values in place of booleans.
- #define TRUE 1
- #define FALSE 0
- // Numbers of uneven rows / columns in a matrix for the properties.
- #define PARITY 1
- #define CHANGE 2
- #define CORRUPT 3
- #define INIT 0
- #define FILENAMEL 100
- /*----------------------------------------------------------------------------*/
- FILE *readMatrix(FILE *fp, int **matrixPointer, int rows, int columns, int end);
- int **setMem(int **matrixPointer, int rows, int columns);
- int testRows(int **matrixPointer, int rowLocation, int rows, int columns);
- int testColumns(int **matrixPointer, int columnsLocation, int rows, int columns);
- void outputResults(int rowUnevens, int columnUnevens, int columnLocation, int rowLocation);
- void freeMem(int **matrixPointer, int rows, int columns);
- /*----------------------------------------------------------------------------*/
- int main(int argc, char **argv)
- {
- int matrixNumber = INIT;
- int rows = INIT;
- int columns = INIT;
- int rowLocation = INIT;
- int columnLocation = INIT;
- int end = FALSE;
- FILE *fp = NULL;
- int **matrixPointer = NULL;
- do
- {
- matrixNumber++;
- fp = readMatrix(fp, matrixPointer, rows, columns, end);
- if (end != TRUE)
- {
- int rowUnevens = testRows(matrixPointer, rowLocation, rows, columns);
- int columnUnevens = testColumns(matrixPointer, columnLocation, rows, columns);
- printf("%d) \n", matrixNumber);
- outputResults(rowUnevens, columnUnevens, columnLocation, rowLocation);
- }
- freeMem(matrixPointer, rows, columns);
- }while(!feof(fp));
- return 0;
- }
- /*----------------------------------------------------------------------------*/
- FILE *readMatrix(FILE *fp, int **matrixPointer, int rows, int columns, int end)
- {
- char fileName[FILENAMEL]; // Character array for file-name string.
- fp = fopen(fileName, "r"); // Opens file with given name in 'read-only'.
- int index1 = INIT;
- int index2 = INIT;
- if(!feof(fp))
- {
- end = FALSE;
- /* Reads numbers from file for values of 'rows' and 'columns' on the first
- line of input. */
- fscanf(fp, "%d", &rows);
- fscanf(fp, "%d", &columns);
- // Allocates the memory for the matrixPointer array.
- matrixPointer = setMem(matrixPointer, rows, columns);
- for(index1 = INIT; index1 < rows; index1++)// Loops through rows.
- {
- for (index2 = INIT; index2 < columns; index2++) // Loops through columns.
- {
- /* Inputs value at that point of file and stores in appropriate
- point in the matrix array according to current index. */
- fscanf(fp, "%d", &matrixPointer[index1][index2]);
- }
- }
- }
- else
- {
- end = TRUE;
- }
- return fp;
- }
- /*----------------------------------------------------------------------------*/
- int **setMem(int **matrixPointer, int rows, int columns)
- {
- int index1 = INIT;
- // Allocate memory for each row.
- *matrixPointer = (int *)calloc(rows, sizeof(int *));
- // Allocate memory for each columns within each row.
- for (index1 = INIT; index1 < rows; index1++)
- {
- *matrixPointer[index1] = (int)calloc(columns, sizeof(int));
- }
- return matrixPointer;
- }
- /*----------------------------------------------------------------------------*/
- int testRows(int **matrixPointer, int rowLocation, int rows, int columns)
- {
- int counterRows = INIT;
- int unevens = INIT;
- int index1 = INIT;
- int index2 = INIT;
- for(index1 = INIT; index1 < rows; index1++)// Loops through rows.
- {
- counterRows = INIT;
- for (index2 = INIT; index2 < columns; index2++) // Loops through columns.
- {
- //Adds values of pointer as a counter of 1s.
- counterRows += matrixPointer[index1][index2];
- }
- if ((counterRows % 2) != 0)// If counter is not evenly divisible by 2.
- {
- unevens++; // Number of uneven rows count increases.
- rowLocation = index2; // Coordinate of changeBit is altered.
- }
- }
- return unevens;
- }
- /*----------------------------------------------------------------------------*/
- int testColumns(int **matrixPointer, int columnsLocation, int rows, int columns)
- {
- int counterRows = INIT;
- int unevens = INIT;
- int index1 = INIT;
- int index2 = INIT;
- int columnLocation = INIT;
- for(index1 = INIT; index1 < rows; index1++)// Loops through rows.
- {
- counterRows = INIT;
- for (index2 = INIT; index2 < columns; index2++) // Loops through columns.
- {
- //Adds values of pointer as a counter of 1s.
- counterRows += matrixPointer[index2][index1];
- }
- if ((counterRows % 2) != 0)// If counter is not evenly divisible by 2.
- {
- unevens++; // Number of uneven rows count increases.
- columnLocation = index2; // Coordinate of changeBit is altered.
- }
- }
- return unevens;
- }
- /*----------------------------------------------------------------------------*/
- void outputResults(int rowUnevens, int columnUnevens, int columnLocation, int rowLocation)
- {
- // If there are no rows/columns with uneven numbers.
- if((rowUnevens == PARITY) && (columnUnevens == PARITY))
- {
- printf("Matrix has parity property. \n\n");
- }
- else
- {
- /* If 1 row and column is uneven then it uses the coordinate for
- change-bit*/
- if((rowUnevens == CHANGE) && (columnUnevens == CHANGE))
- {
- // Prints bit-change location to user from recorded values.
- printf("Change bit (%d , %d). \n\n", rowLocation, columnLocation);
- }
- else
- {
- printf("Matrix is corrupt. \n\n");
- }
- }
- return;
- }
- /*----------------------------------------------------------------------------*/
- void freeMem(int **matrixPointer, int rows, int columns)
- {
- int index1 = INIT;
- for (index1 = INIT; index1 < rows; index1++)// Loops through rows.
- {
- // Frees memory of each array in the matrix and sets to NULL.
- free(matrixPointer[index1]);
- matrixPointer[index1] = NULL;
- }
- // Frees memory of the matrix.
- free(matrixPointer);
- matrixPointer = NULL;
- return;
- }
- /*----------------------------------------------------------------------------*/
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.