my program compiles without problems but when i try to run it pauses shortly and then crashes. When i set it to debug it came up with the following message:
"An Access Violation (Segmentation Fault) raised in your program."
Ill put my code below. can anyone see why it is doing this?
[code=c]
/* 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
/*----------------------------------------------------------------------------*/
void 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 &columnsLocatio n, int rows, int columns);
void outputResults(i nt 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++;
readMatrix(fp, matrixPointer, rows, columns, end);
if (end != TRUE)
{
int rowUnevens = testRows(matrix Pointer, rowLocation, rows, columns);
int columnUnevens = testColumns(mat rixPointer, columnLocation, rows, columns);
printf("%d) \n", matrixNumber);
outputResults(r owUnevens, columnUnevens, columnLocation, rowLocation);
}
freeMem(matrixP ointer, rows, columns);
}while(!feof(fp ));
return 0;
}
/*----------------------------------------------------------------------------*/
void 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'.
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(matrixPo inter, rows, columns);
for(int index1 = INIT; index1 < rows; index1++)// Loops through rows.
{
for (int 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", &matrixPoint er[index1][index2]);
}
}
}
else
{
end = TRUE;
}
return;
}
/*----------------------------------------------------------------------------*/
int **setMem(int **matrixPointer , int rows, int columns)
{
// Allocate memory for each row.
*matrixPointer = (int *)calloc(rows, sizeof(int *));
// Allocate memory for each columns within each row.
for (int index1 = INIT; index1 < rows; index1++)
{
*matrixPointer[index1] = (int)calloc(col umns, 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 &columnsLocatio n, 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(i nt 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)
{
for (int index1 = INIT; index1 < rows; index1++)// Loops through rows.
{
// Frees memory of each array in the matrix and sets to NULL.
free(matrixPoin ter[index1]);
matrixPointer[index1] = NULL;
}
// Frees memory of the matrix.
free(matrixPoin ter);
matrixPointer = NULL;
return;
}
/*----------------------------------------------------------------------------*/
[/code]
"An Access Violation (Segmentation Fault) raised in your program."
Ill put my code below. can anyone see why it is doing this?
[code=c]
/* 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
/*----------------------------------------------------------------------------*/
void 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 &columnsLocatio n, int rows, int columns);
void outputResults(i nt 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++;
readMatrix(fp, matrixPointer, rows, columns, end);
if (end != TRUE)
{
int rowUnevens = testRows(matrix Pointer, rowLocation, rows, columns);
int columnUnevens = testColumns(mat rixPointer, columnLocation, rows, columns);
printf("%d) \n", matrixNumber);
outputResults(r owUnevens, columnUnevens, columnLocation, rowLocation);
}
freeMem(matrixP ointer, rows, columns);
}while(!feof(fp ));
return 0;
}
/*----------------------------------------------------------------------------*/
void 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'.
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(matrixPo inter, rows, columns);
for(int index1 = INIT; index1 < rows; index1++)// Loops through rows.
{
for (int 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", &matrixPoint er[index1][index2]);
}
}
}
else
{
end = TRUE;
}
return;
}
/*----------------------------------------------------------------------------*/
int **setMem(int **matrixPointer , int rows, int columns)
{
// Allocate memory for each row.
*matrixPointer = (int *)calloc(rows, sizeof(int *));
// Allocate memory for each columns within each row.
for (int index1 = INIT; index1 < rows; index1++)
{
*matrixPointer[index1] = (int)calloc(col umns, 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 &columnsLocatio n, 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(i nt 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)
{
for (int index1 = INIT; index1 < rows; index1++)// Loops through rows.
{
// Frees memory of each array in the matrix and sets to NULL.
free(matrixPoin ter[index1]);
matrixPointer[index1] = NULL;
}
// Frees memory of the matrix.
free(matrixPoin ter);
matrixPointer = NULL;
return;
}
/*----------------------------------------------------------------------------*/
[/code]
Comment