Search code examples
cdebuggingabort

C Hangman program Debug Assist (Abort trap:6 error)


I'm creating the typical hangman program in C and the code compiles and runs flawlessly until I add 3 lines of code (25-29) that load a string into a struct after the user has selected the game mode they want.

When running the program, everything functions unless the user makes a selection that executes the "if" statement in lines 25-29 (there is a comment identifying this statement), then the terminal returns an "Abort trap: 6" error.

After research and attempting to debug my code, I am unable to find where I am writing to memory I haven't initialized already. Everything else in the code works fine so I am only looking for specific advice on this error:

char temp2[20]="", phrase[15]="cest la vie";
int guess=0, correct=0, banksize=0, wordsize=0, wordNum=0;
int n, select=0;
time_t t;
strcpy(hangman.guess,temp2);
hangman.incorrect=0;
hangman.wordcount=0;

//This section introduces the user to the game
printf("\nWelcome to Dakota's Amazing Hangman Program!!!(TM)\n");
printf("---------------------------------------------------\n");
printf("I will select a word or phrase at random. \n");
printf("Guess a letter wrong 10 times and you'll hang.\n");
printf("For a word, type and enter 0. For an expression, enter 1:");
scanf("%i",&select);

//This section requests a file and loads it into struct
if(select==0)
{
   InputFile();
   banksize=LoadWordbank();
   srand((unsigned)time(&t));
   wordNum=rand() % banksize;
}
if(select==1)  //The issue is with the addition of this if statement
{
   strcpy(hangman.word[0],phrase);
   wordNum=0;
} 

wordsize=strlen(hangman.word[wordNum]);
char temp3[wordsize];
for(n=0;n<wordsize;n++)
   temp3[n]='_';
strcpy(hangman.progress,temp3);

Here is my struct definition for reference, it is a global variable:

struct game
{
  char word[30][15];
  char progress[8];
  char guess[20];
  int incorrect;
  int wordcount;
};
struct game hangman;

Additionally, I am new to this forum so any productive advice on question formatting is welcome.


Solution

  • After working with some fellow students and determining that the issue was not relating to how the string was being loaded into the struct, we determined that it must therefore be an issue with a variable that is sensitive to the length of the word.

    After close examination, it looks as if I had extended the size of all of my variable to account for the addition of a word larger than 7 letters EXCEPT this variable in the struct:

    char progress[8];
    

    This caused as issue with line 33 of the code where I attempted to string copy a larger string into the hangman.progress variable.

    After extending this to a safe allocation of [15] I no longer receive any errors. The code now looks as follows:

    struct game
    {
      char word[30][15];
      char progress[15];
      char guess[20];
      char bodypart[10][15];
      int incorrect;
      int wordcount;
    };
    struct game hangman={"cest la vie"};
    

    ...

    //This section loads the arrays in the hangman struct
    char temp2[20]="", phrase[]="cest la vie";
    int guess=0, correct=0, banksize=0, wordsize=11, wordNum=0;
    int n, select=0;
    time_t t;
    strcpy(hangman.guess,temp2);
    hangman.incorrect=0;
    hangman.wordcount=0;
    LoadBody();
    
    //This section introduces the user to the game
    printf("\nWelcome to Dakota's Amazing Hangman Program!!!(TM)\n");
    printf("---------------------------------------------------\n");
    printf("I will select a word or phrase at random. \n");
    printf("Guess a letter wrong 10 times and you'll hang.\n");
    printf("For a word, type and enter 0. For an expression, enter 1:");
    scanf("%i",&select);
    
    //This section requests a file and loads it into struct
    if(select==0)
    {
       InputFile();
       banksize=LoadWordbank();
       srand((unsigned)time(&t));
       wordNum=rand() % banksize;
       wordsize=strlen(hangman.word[wordNum]);
    }
    
    char temp3[wordsize];
    for(n=0;n<wordsize;n++)
       temp3[n]='_';
    strcpy(hangman.progress,temp3);
    
    if(select==1)
       wordsize=wordsize-2;