核心转储错误 c

标签 c core dump

我的程序可以编译,但在输入 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 文件中,但我看不到它。

游戏.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

游戏.h:

#ifndef game_h
#define game_h

void Game(int NumberSystem,int MaxRepetitions,char *Player1,char *Player2);
void Getoptt();


#endif 

最佳答案

看起来您已经学会了足够的 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 字节.

内存损坏。

未定义的行为。

关于核心转储错误 c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26985723/

相关文章:

mpi - openMPI 中的处理器/套接字关联?

c - 在 C 中从本地时间(没有 Boost!)获取毫秒数

c - 试图使连续的 FIFO 数据流

php - 如何使用 PHPWord 在现有的 Word 文档上添加水印?

java - java串口异常

sql - 转储具有特定前缀的 psql 表

python - 有什么方法可以从树的转储文件中恢复 AST 树吗?

mysql - 将转储附加到现有的 MySQL 转储文件

C编程循环需要解释吗

c - 为什么netstat未列出我的套接字的开放端口?