C runtime errors. desperately needing help.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dameon99
    New Member
    • Jun 2007
    • 57

    #1

    C runtime errors. desperately needing help.

    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]
  • Dameon99
    New Member
    • Jun 2007
    • 57

    #2
    it also points to this in <stdio.h>:

    "{ return __F->_flag & _IOEOF; }"

    I dont understand what this means, im hoping one of you does..

    Comment

    • RRick
      Recognized Expert Contributor
      • Feb 2007
      • 463

      #3
      The main cause of a seg violation is a NULL or "rogue" pointer. By rogue, I mean a pointer with random or unitialized values. You have a lot of pointers in your code and a lot of code and maybe someone will be lucky and see the problem. (I didn't see anything that was obvious).

      This is where a debugger is a great help. Compile your code with the debugger option turned on and let the program run in the debugger and when it crashes, the debugger will show where the offending line of code is. Alternatively, you can put in printfs until you bracket the problem.

      Comment

      • Dameon99
        New Member
        • Jun 2007
        • 57

        #4
        Originally posted by RRick
        The main cause of a seg violation is a NULL or "rogue" pointer. By rogue, I mean a pointer with random or unitialized values. You have a lot of pointers in your code and a lot of code and maybe someone will be lucky and see the problem. (I didn't see anything that was obvious).

        This is where a debugger is a great help. Compile your code with the debugger option turned on and let the program run in the debugger and when it crashes, the debugger will show where the offending line of code is. Alternatively, you can put in printfs until you bracket the problem.
        I am running a debugger but it int stopping at anything just coming up with an error.. I suppose I ould put printf s everywhere and try it that way but it is very time consuming.
        Can you tell me more about why NULL would cause a segmentation violation? we were told to use it.

        Comment

        • Dameon99
          New Member
          • Jun 2007
          • 57

          #5
          btw i think my compiler is compiling my code as C++. That may help some?

          Comment

          • RRick
            Recognized Expert Contributor
            • Feb 2007
            • 463

            #6
            I did notice one thing, your readMatrix is in need of a few revisions. First, the fileName variable is never set to a value. Fopen on a random string is not a good idea.

            You pass in a pointer value for fp, but you're not going to get a changed pointer value back. Any use of fp in main, will be with the initialized value of NULL. You need to pass in FILE *& or FILE ** for readMatrix to change the value.

            You need to check for a valid file pointer before calling feof because feof expects a valid file pointer. Usually, after fopen, you check for a valid file pointer simply by:
            Code:
            if ( !fp) printf( "Can't open file .......

            Comment

            • Dameon99
              New Member
              • Jun 2007
              • 57

              #7
              the program doesnt even prompt the user for the file name though.. Ill give it a try and c what I can do. Thanks heaps.

              Comment

              • Dameon99
                New Member
                • Jun 2007
                • 57

                #8
                Im getting these errors now on compile. It compiles with no problems in C++ but needs to be in C.. I honestly cant see anything wrong in any of these and is everything I have been taught in C.

                43 syntax error before '&' token
                45 syntax error before '&' token
                46 syntax error before '&' token
                96 syntax error before '&' token
                In function `readMatrix':
                99 `fp' undeclared (first use in this function)
                (Each undeclared identifier is reported only once
                for each function it appears in.)
                103 `end' undeclared (first use in this function)
                107 `rows' undeclared (first use in this function)
                108 `columns' undeclared (first use in this function)
                111 `matrixPointer' undeclared (first use in this function)
                113 'for' loop initial declaration used outside C99 mode
                115 'for' loop initial declaration used outside C99 mode
                In function `setMem':
                142 c 'for' loop initial declaration used outside C99 mode
                At top level:
                156 syntax error before '&' token
                In function `testRows':
                163 `rows' undeclared (first use in this function)
                167 `columns' undeclared (first use in this function)
                170 `matrixPointer' undeclared (first use in this function)
                175 `rowLocation' undeclared (first use in this function)
                At top level:
                186 syntax error before '&' token
                In function `testColumns':
                194 `rows' undeclared (first use in this function)
                198 `columns' undeclared (first use in this function)
                201 `matrixPointer' undeclared (first use in this function)
                In function `freeMem':
                250 'for' loop initial declaration used outside C99 mode


                I really cant work out any of them.. Im only just getting these errors now.. :'-(. very frustrating.

                heres my code:

                [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



                /*----------------------------------------------------------------------------*/


                FILE *readMatrix(FIL E *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;
                }



                /*----------------------------------------------------------------------------*/



                FILE *readMatrix(FIL E *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 fp;
                }


                /*----------------------------------------------------------------------------*/



                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

                • Dameon99
                  New Member
                  • Jun 2007
                  • 57

                  #9
                  dw, i have managed to fix all of these errors. Thankyou to all of you who assisted me with this.

                  Comment

                  Working...