我的程序编译但输入fnumber变量后崩溃。
错误说:核心转储。我不知道为什么。我已经研究了30分钟的代码,但我没有看到解决方案。
请帮帮我。
这是我的主要文件:
#ifndef input_h
#define input_h
#include "game.h"
#include <unistd.h>
#include <stdio.h>
#include <getopt.h>
#include <assert.h>
#include <stdlib.h>
#define NDEBUG
#define N 56
#define Max_size 256
void Game(int NumberSystem,int MaxRepetitions,char *Player1,char *Player2)
{
int counter,k,i,temp;
char fnumber,number;
int status = 0;
counter = k = 1;
char *array1 = malloc(2*MaxRepetitions*NumberSystem*sizeof(char));
int *array2 = malloc(2*NumberSystem*sizeof(int));
char CharArray[N] = {'0','1','2','3', '4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
for(i=0; i<2*NumberSystem*sizeof(int),i++;)
array2[i] = 0;
printf("Let's start the game! %s begins.\n",Player1);
while(status == 0 || fnumber > CharArray[NumberSystem-1] || fnumber <=CharArray[0])
{
while((temp=getchar()) != EOF && temp != '\n');
scanf(" %c", &fnumber);
for(i=0; i<NumberSystem; i++)
{
if(fnumber == CharArray[i])
{
status = 1;
break;
}
}
if(status == 0 || fnumber > CharArray[NumberSystem-1] || fnumber <=CharArray[0] )
printf("First number must be a positive decimal number between %d and %d in chosen system\n",1,NumberSystem-1,NumberSystem);
}
array1[0] = fnumber;
array2[fnumber]++;
while( array2[number] < MaxRepetitions)
{
status = 0;
while(status == 0 || ( (array1[k-1] != CharArray[NumberSystem-1]) && (array1[k-1] >= number) ) )
{
while((temp=getchar()) != EOF && temp != '\n');
scanf(" %c", &number);
for(i=0; i<NumberSystem; i++)
{
if(number == CharArray[i])
{
status = 1;
break;
}
}
if(status == 1 && ( (array1[k-1] == CharArray[NumberSystem-1]) || array1[k-1] < number ) && number <= CharArray[NumberSystem-1] )
{
break;
}
else
printf("You must enter a number greater than %c and smaller or equal to %d in chosen system\n",array1[k-1],NumberSystem-1);
}
array2[number]++;
array1[k] = number;
k++;
counter++;
}
if(counter%2 == 0)
{
printf("%s entered %c for the %dth time and exceeded the possible limit. %s won!",Player2,array1[k-1],MaxRepetitions,Player1);
}
else
{
printf("%s entered %c for the %dth time and exceeded the possible limit. %s won!",Player1,array1[k-1],MaxRepetitions,Player2);
}
free (array1);
free (array2);
}
#endif
这些是其他文件。我认为问题必须在game.c文件中,但我看不到它。
game.c:
#ifndef input_h
#define input_h
#include "game.h"
#include <unistd.h>
#include <stdio.h>
#include <getopt.h>
#include <assert.h>
#include <stdlib.h>
#define NDEBUG
#define N 56
#define Max_size 256
void Game(int NumberSystem,int MaxRepetitions,char *Player1,char *Player2)
{
int counter,k,i,temp;
char fnumber,number;
int status = 0;
counter = k = 1;
char *array1 = malloc(MaxRepetitions*NumberSystem*sizeof(char));
int *array2 = malloc(NumberSystem*sizeof(int));
char CharArray[N] = {'0','1','2','3', '4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
for(i=0; i<NumberSystem*sizeof(int),i++;)
array2[i] = 0;
printf("Let's start the game! %s begins.\n",Player1);
while(status == 0 || fnumber > CharArray[NumberSystem-1] || fnumber <=CharArray[0])
{
while((temp=getchar()) != EOF && temp != '\n');
scanf(" %c", &fnumber);
for(i=0; i<NumberSystem; i++)
{
if(fnumber == CharArray[i])
{
status = 1;
break;
}
}
if(status == 0 || fnumber > CharArray[NumberSystem-1] || fnumber <=CharArray[0] )
printf("First number must be a positive decimal number between %d and %d in chosen system\n",1,NumberSystem-1,NumberSystem);
}
array1[0] = fnumber;
array2[fnumber]++;
while( array2[number] < MaxRepetitions)
{
status = 0;
while(status == 0 || ( (array1[k-1] != CharArray[NumberSystem-1]) && (array1[k-1] >= number) ) )
{
while((temp=getchar()) != EOF && temp != '\n');
scanf(" %c", &number);
for(i=0; i<NumberSystem; i++)
{
if(number == CharArray[i])
{
status = 1;
break;
}
}
if(status == 1 && ( (array1[k-1] == CharArray[NumberSystem-1]) || array1[k-1] < number ) && number <= CharArray[NumberSystem-1] )
{
break;
}
else
printf("You must enter a number greater than %c and smaller or equal to %d in chosen system\n",array1[k-1],NumberSystem-1);
}
array2[number]++;
array1[k] = number;
k++;
counter++;
}
if(counter%2 == 0)
{
printf("%s entered %c for the %dth time and exceeded the possible limit. %s won!",Player2,array1[k-1],MaxRepetitions,Player1);
}
else
{
printf("%s entered %c for the %dth time and exceeded the possible limit. %s won!",Player1,array1[k-1],MaxRepetitions,Player2);
}
free (array1);
free (array2);
}
#endif
game.h:
#ifndef game_h
#define game_h
void Game(int NumberSystem,int MaxRepetitions,char *Player1,char *Player2);
void Getoptt();
#endif
答案 0 :(得分:1)
看起来你已经学会了足够的C来编写一个中型程序。下一步是学习如何使用调试器。任何调试器都应该能够指出您的问题:
int *array2 = malloc(2*NumberSystem*sizeof(int));
/* .... */
for(i=0; i<2*NumberSystem*sizeof(int),i++;)
array2[i] = 0;
让我们说NumberSystem
是4,而在您的硬件上,sizeof(int)
是4.合理的假设。然后,您将最终同步2 * 4 * 4或32字节。
不幸的是,你的for循环也将迭代32次,而不是8次,并最终清除32 * sizeof(int)
或128字节的内存,即使你只分配了32。
内存损坏。
未定义的行为。