C Hangman 程序 Debug Assist (Abort trap:6 error)
C Hangman program Debug Assist (Abort trap:6 error)
我正在用 C 创建典型的 hangman 程序,代码编译和运行完美无缺,直到我添加 3 行代码 (25-29),在用户选择了游戏模式后将字符串加载到结构中想要。
当 运行 程序时,一切正常,除非用户选择执行第 25-29 行中的 "if" 语句(有注释标识此语句),然后终端 returns 一个 "Abort trap: 6" 错误。
经过研究并尝试调试我的代码后,我无法找到我正在写入尚未初始化的内存的位置。代码中的其他所有内容都工作正常,所以我只是在寻找有关此错误的具体建议:
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);
这里是我的结构体定义,供参考,它是一个全局变量:
struct game
{
char word[30][15];
char progress[8];
char guess[20];
int incorrect;
int wordcount;
};
struct game hangman;
另外,我是这个论坛的新手,所以欢迎任何有关问题格式的有效建议。
在与一些同学合作并确定问题与字符串如何加载到结构中无关之后,我们确定这一定是对字符串长度敏感的变量的问题单词。
仔细检查后,似乎我已经扩展了所有变量的大小以解决结构中除了这个变量之外的大于 7 个字母的单词的添加:
char progress[8];
这导致代码的第 33 行出现问题,在该行中我尝试将更大的字符串字符串复制到 hangman.progress 变量中。
将此扩展到 [15] 的安全分配后,我不再收到任何错误。代码现在如下所示:
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;
我正在用 C 创建典型的 hangman 程序,代码编译和运行完美无缺,直到我添加 3 行代码 (25-29),在用户选择了游戏模式后将字符串加载到结构中想要。
当 运行 程序时,一切正常,除非用户选择执行第 25-29 行中的 "if" 语句(有注释标识此语句),然后终端 returns 一个 "Abort trap: 6" 错误。
经过研究并尝试调试我的代码后,我无法找到我正在写入尚未初始化的内存的位置。代码中的其他所有内容都工作正常,所以我只是在寻找有关此错误的具体建议:
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);
这里是我的结构体定义,供参考,它是一个全局变量:
struct game
{
char word[30][15];
char progress[8];
char guess[20];
int incorrect;
int wordcount;
};
struct game hangman;
另外,我是这个论坛的新手,所以欢迎任何有关问题格式的有效建议。
在与一些同学合作并确定问题与字符串如何加载到结构中无关之后,我们确定这一定是对字符串长度敏感的变量的问题单词。
仔细检查后,似乎我已经扩展了所有变量的大小以解决结构中除了这个变量之外的大于 7 个字母的单词的添加:
char progress[8];
这导致代码的第 33 行出现问题,在该行中我尝试将更大的字符串字符串复制到 hangman.progress 变量中。
将此扩展到 [15] 的安全分配后,我不再收到任何错误。代码现在如下所示:
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;