C语言中的生命分割错误

时间:2013-09-14 11:37:04

标签: c pointers segmentation-fault multidimensional-array conways-game-of-life

我是C,Linux等的新手我的代码编译并运行但是一旦我输入我的第一个用户输入,我就会出现分段错误。如果有人可以指出我的代码有什么问题会对我们有帮助,我认为它是在'calculate()'或'main()'中,因为我试图在两者中使用'malloc()'分配内存那些地方。

#include <stdio.h>
#include <stdlib.h>

#define LIFE_YES 'X'
#define LIFE_NO 'O'

int HEIGHT, WIDTH;
typedef int **TableType;

void printTable(TableType table) {
    int height, width;

        for (height = 0; height < HEIGHT; height++) {
            for (width = 0; width < WIDTH; width++) {
                    if (table[height][width] == LIFE_YES) {
                        printf("X");
                    } 
            else {
                    printf("-");
                    }
            }
            printf("\n");
        }
        printf("\n");
}

void clearTable(TableType table) {
    int height, width;

        for (height = 0; height < HEIGHT; height++) {
            for (width = 0; width < WIDTH; width++) {
                    table[height][width] = LIFE_NO;
            }
        }
}

void askUser(TableType tableA) {
    int i;
        int n;
        int height, width;

        printf("Enter the amount of initial organisms: ");
        scanf("%d", &n);

        for (i = 0; i < n; i++) {
            printf("Enter dimensions (x y) where organism %d will live: ", i + 1);
            scanf("%d %d", &height, &width);

            tableA[height][width] = LIFE_YES;
        }

        printTable(tableA);
        printf("Generation 0");
}

int getNeighborValue(TableType table, int row, int col) {
        if (row < 0 || row >= HEIGHT || col < 0 || col >= WIDTH || table[row][col] != LIFE_YES ) {
            return 0;
        } 
    else {
            return 1;
        }
}

int getNeighborCount(TableType table, int row, int col) {
        int neighbor = 0;

        neighbor += getNeighborValue(table, row - 1, col - 1);
        neighbor += getNeighborValue(table, row - 1, col);
        neighbor += getNeighborValue(table, row - 1, col + 1);
        neighbor += getNeighborValue(table, row, col - 1);
        neighbor += getNeighborValue(table, row, col + 1);
        neighbor += getNeighborValue(table, row + 1, col - 1);
        neighbor += getNeighborValue(table, row + 1, col);
        neighbor += getNeighborValue(table, row + 1, col + 1);

        return neighbor;
}

void calculate(TableType tableA) {
        TableType tableB;
        int neighbor, height, width, i;
    tableB= malloc(HEIGHT * sizeof(int*));

    for (i = 0; i < HEIGHT; i++) {
            tableB[i] = malloc(WIDTH * sizeof(int));
    }

        for (height = 0; height < HEIGHT; height++) {
            for (width = 0; width < WIDTH; width++) {
                    neighbor = getNeighborCount(tableA, height, width);
                    if (neighbor==3) {
                        tableB[height][width] = LIFE_YES;
                    } 
            else if (neighbor == 2 && tableA[height][width] == LIFE_YES) {
                        tableB[height][width] = LIFE_YES;
                    } 
            else {
                        tableB[height][width] = LIFE_NO;
                    }
            }
        }

        for (height = 0; height < HEIGHT; height++) {
            for (width = 0; width < WIDTH; width++) {
                    tableA[height][width] = tableB[height][width];
            }
    }
    free(tableB);
}

/* test data
void loadTestData(TableType table) {
        table[3][4] = LIFE_YES;
        table[3][5] = LIFE_YES;
        table[3][6] = LIFE_YES;

        table[10][4] = LIFE_YES;
        table[10][5] = LIFE_YES;
        table[10][6] = LIFE_YES;
        table[11][6] = LIFE_YES;
        table[12][5] = LIFE_YES;
}
*/

int main(void) {
    int i;
        char end;
        int generation = 0;

    printf("Enter the amount of rows and columns you want in the grid: ");
    scanf("%i %i\n", &HEIGHT, &WIDTH);

        TableType table = malloc(HEIGHT * sizeof(int*));

    for (i = 0; i < HEIGHT; i++) {
            table[i] = malloc(WIDTH * sizeof(int));
    }

        clearTable(table);
        askUser(table);
        /*loadTestData(table);*/
        printTable(table);

        while (end != 'q') {
            calculate(table);
            printTable(table);
            printf("Generation %d\n", ++generation);
            printf("Press q to quit or 1 to continue: ");
            scanf(" %c", &end);
        }

        return 0;
}

2 个答案:

答案 0 :(得分:0)

主要

scanf("%i %i\n", &HEIGHT, &WIDTH);  ==>    scanf("%i %i", &HEIGHT, &WIDTH);
             ^

因为那个\ n你需要再输入一个输入。禁止它

askUser

中的细分错误
tableA[height][width] = LIFE_YES;

Program received signal SIGSEGV, Segmentation fault.
0x00000000004007c1 in askUser (tableA=0x603010) at seg3.c:49
49                  tableA[height][width] = LIFE_YES;
(gdb) bt
#0  0x00000000004007c1 in askUser (tableA=0x603010) at seg3.c:49
#1  0x0000000000400bf3 in main () at seg3.c:142
(gdb)  

在这里你没有分配内存但是你正在访问。首先分配内存

答案 1 :(得分:0)

因为:

是分段错误
for (i = 0; i < n; i++) 
{
   printf("Enter dimensions (x y) where organism %d will live: ", i + 1);
   scanf("%d %d", &height, &width);
    tableA[height][width] = LIFE_YES;  <--- Here 
}

如果用户指定的维度超出了您最初在main中动态分配的“HEIGHT”和“WEIGHT”维度,那该怎么办。

当用户输入时,您需要检查坐标是非负的且小于上述尺寸,如果它不在绑定范围内,只需抛出错误消息并退出。

第二件事:
此外,您在{main}中的end变量未初始化,可能您在end = getchar()条件下错过了此if
如果未初始化,则可能导致seg-fault。

第三件事:
真的需要\n中的scanf吗?如果存在,则将第3个输入作为下一个scanf的输入,并且不会提示您输入该scanf。
即这里目前第3个输入将进入no。生物体没有被提示。