【问题标题】:Game of Life pointers生命游戏指针
【发布时间】:2018-03-13 12:43:52
【问题描述】:

我正在用 C 语言创建生命游戏,在程序接受用户输入后,我收到了这个分段错误(核心转储)错误。我最近开始学习 C 并且我对指针的理解是基本的。在网上查找并尝试不同的方法使其正确后,我无法找到解决它的方法。如果我不使用指针并保持简单,一切正常。我将不胜感激

   int main() {

    int maxR;               
    int maxC;               
    int generations;        
    int i=0;
    int j=0;
    int k=0;
    int n;                  //neighbour count
    char state;
    char **board;         //original boardfor comparison
    char **newBoard;        //boardto make changes to

    scanf("%d %d %d",&maxR,&maxC,&generations);             //take input

    board= (char**)malloc(maxR * sizeof(char*));         //allocating memory
    newBoard=(char**) malloc(maxR * sizeof(char*));        //allocating memory

    for(i=0; i<maxR; i++) {
        board[i] = malloc(maxC * sizeof (char));          //allocating memory
        newBoard[i] = malloc(maxC * sizeof (char));         //allocating memory
        for(j=0; j<maxC; j++) {
            scanf (" %c", &board[i][j]);                  //getting input
        }
    }

    for(i=0; i<=generations; i++ ) {                        
        for (j=0; j<maxR; j++) {                            
            for (k=0; k<maxC; k++) {                        
                state=board[j][k];                        
                n=countNeighbours(board,maxR,maxC,j,k);   

                if(state == '1') {                          //if the cell is alive
                    if(n==2 || n==3) newBoard[j][k] = '1';  //if the cell has 2 or 3 neighbours then it lives
                    else newBoard[j][k]='0';                //else the cell dies

                } else {                                     //else (if) the cell is dead
                    if(n==3) newBoard[j][k]='1';            //but has 3 neibours then the cell become alive
                    else newBoard[i][j]='0';                //else it dies
                }
            }
        }
        memcpy(board, newBoard,sizeof(board));          //copy the updated grid to the old one
    }

    printBoard(board,maxR,maxC);                          

    deallocate(board,maxR);                               //deallocatethe memory
    deallocate(copyGrid,maxR);                              //deallocatethe memory

【问题讨论】:

    标签: c pointers multidimensional-array


    【解决方案1】:

    这里有一个明显的问题。

    memcpy(oldGrid, copyGrid,sizeof(oldGrid));          //copy the updated grid to the old one
    

    由于oldGridchar** 指针,那么sizeof(oldGrid) 是指针的大小,取决于您的平台,它可能是4 或8 个字节。所以,你不是在复制网格,你只是在复制它的几个字节。

    如果要复制整个网格,则需要计算网格的大小(以字节为单位)。

    如果oldGrid 被声明为一个数组,而不是一个指针,那么sizeof(oldGrid) 将产生您所期望的整个网格大小。当涉及到sizeof() 时,数组的行为与指针不同。

    【讨论】:

    • 是的,简而言之-您需要放在那里的实例大小,而不是指针的大小,这就是您现在正在做的事情。很高兴你有 sizeof ,你会得到指针的大小,而不是网格的大小。
    • 感谢您的回复。如果我使用两个 for 循环将一个网格复制到另一个网格而不是 memcpy,则程序在用户输入后仍然崩溃
    • @James,可能在 main() 函数的第二个 for 循环中。 '否则 copyGrid[i][j]='.';'将是问题所在。你的意图是'else copyGrid[j][k]='.';' ?嗯?
    • @Kay 是的,你是对的,解决了问题。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    相关资源
    最近更新 更多