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