Thread: Error In Code - read access violation

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    27

    Error In Code - read access violation

    Hello Everyone,

    I hope you are all doing well. I am creating a program to detect and correct errors in hamming code. The issue I am having is the program is crashing.

    Debugging it I found the error that's causing the crash is on line 76 and is throwing the error message "Exception Thrown: read access violation".

    Something tells me its one of those little mistakes I just cant seem to find, i'm hoping someone with some fresh eyes can take a look at my below code and give me some advice or assistance.

    Thanks in advance for your help, i appreciate it.

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    
    
    
        /** Initializing the global variables */
        int MaxLength;
        int length;
        int parity;
        // Initialize the hamming string with a random or NULL memory address
        char *HammingString=NULL;
    
    
        /** Function to enter the values */
        void EnterParameters(int *length, int *parity)
        {
            printf("Enter the maximum length: ");
            /** %d reads an integer to be stored in an int. This integer can be signed */
            scanf(" %d", length);
            printf("Enter the parity (0=even, 1=odd): ");
            /** %d reads an integer to be stored in an int. This integer can be signed */
            scanf(" %d", parity);
            printf("\n");
        }
    
    
        void CheckHamming(char *HammingString, int parity)
        {
            // Initializing the local variables i, j, k, start, length, ParityNumber
            int i, j, k, start, length, ParityNumber;
            printf("Enter the Hamming code: ");
            scanf("%s", HammingString);
    
    
            int ErrorBit = 0;                        // Initialize the error bit
            length = strlen(HammingString);          // The strlen computes the length of a string up to, but not including the terminating null character
            length--;
            if (length > MaxLength)
            {
                printf("\n** Invalid Entry - Exceeds Maximum Code Length of %d\n\n", MaxLength);
                return;
            }
            ParityNumber = ceil(log(length)/log(2)); // The ceil function returns the smallest integer that is greater than or equal to 'x'.
    
    
            for(i = 0; i < ParityNumber; i++)
            {
                // pow returns x raised to the power y. In this case, 2 raised to the power i.
                start = pow(2, i);
                int ParityCheck = parity;
    
    
                for(j = start; j < length; j=j+(2*start))
                {
                    for(k = j; (k < ((2*j) - 1)) && (k < length); k++)
                    {
                        ParityCheck ^= (HammingString[length - k] - '0');
                    } // End the k for-loop
                } // End the j for-loop
    
    
                    ErrorBit = ErrorBit + (ParityCheck * start);
                } // End the i for-loop
    
    
            if(ErrorBit == 0)
            {
                printf("No error \n");
            }
            else
            {
                printf("There is an error in bit: %d\n", ErrorBit);
                if((HammingString[length - ErrorBit]) == '0')
                {
                    (HammingString[length - ErrorBit]) = '1';
                }
                else
                {
                    (HammingString[length - ErrorBit]) = '0';
                }
    
    
                printf("The corrected Hamming code is: %s \n", HammingString);
            }
        } // End CheckHamming
    
    
        int main()
        {
    
    
            int parity;
            char choice = NULL;
                printf("Error detection/correction: \n");
                printf("----------------------------\n");
                printf("a) Enter Parameters \n");
                printf("b) Check Hamming Code \n");
                printf("c) Exit \n");
                printf("\nEnter selection: ");
                scanf(" %c", &choice);
    
    
                while (choice != 'c')
                {
                    if (choice == 'a')
                    {
                        EnterParameters(&MaxLength, &parity);
                        HammingString = (char*) malloc (MaxLength * sizeof(char));
                        main();
                    }
                    else if (choice == 'b')
                    {
                        CheckHamming(HammingString, &parity);
                        main();
                    }
                    else
                    {
                        printf("Valid options are a, b, or c. Quitting program. \n");
                        getchar();
                        exit(0);
                    }   
                }//end while
                exit(0);
        }//end main
    Last edited by rb737; 04-09-2018 at 11:54 PM.

  2. #2
    Registered User
    Join Date
    Mar 2016
    Posts
    27
    I Fixed it!
    I ended up rewriting my code from scratch as what I had was far too complex for its purpose.
    I found that using a switch statement in main oddly made the whole process much easier.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LoadLibrary returns error code 998 - Access violation
    By VirtualAce in forum Windows Programming
    Replies: 1
    Last Post: 04-11-2012, 01:20 PM
  2. Access violation error?
    By rousse09 in forum C++ Programming
    Replies: 2
    Last Post: 12-21-2011, 09:42 AM
  3. Prolog code causing Access Violation error in VS2008
    By HJoshy in forum General AI Programming
    Replies: 13
    Last Post: 04-21-2010, 01:27 PM
  4. Access Violation error
    By thedoofus in forum C Programming
    Replies: 2
    Last Post: 04-30-2005, 11:33 AM
  5. Access Violation Error
    By Scott in forum C++ Programming
    Replies: 6
    Last Post: 07-24-2003, 05:00 AM

Tags for this Thread