使用realloc()时的SIGABRT

使用realloc()时的SIGABRT,c,dynamic-memory-allocation,realloc,C,Dynamic Memory Allocation,Realloc,我正在尝试制作一个C版本的游戏蛇作为我的家庭作业。我已经为游戏实现了所有必要的机制,但是我在动态内存分配方面遇到了问题。我的“蛇”被存储在一个数组中,作为身体碎片及其位置的列表。如果蛇吃了水果,我会尝试改变蛇的长度,并在数组中添加另一个身体碎片。因为我事先不知道蛇的最终大小,所以我尝试使用realloc来延长数组。下面是我的代码。注意:在导致SIGABRT之前,它可以工作14次迭代 #include <stdio.h> #include <stdlib.h> vo

我正在尝试制作一个C版本的游戏蛇作为我的家庭作业。我已经为游戏实现了所有必要的机制,但是我在动态内存分配方面遇到了问题。我的“蛇”被存储在一个数组中,作为身体碎片及其位置的列表。如果蛇吃了水果,我会尝试改变蛇的长度,并在数组中添加另一个身体碎片。因为我事先不知道蛇的最终大小,所以我尝试使用realloc来延长数组。下面是我的代码。注意:在导致SIGABRT之前,它可以工作14次迭代

    #include <stdio.h>
#include <stdlib.h>
void moveSnake(int *snakeLength,int direction, int **tableSnake, int extend);
int checkItems(int **tableSnake, int **items, int itemsSize);
int selfdestruct(int **tableSnake);

/* TEST INPUT DATA: 
9
-5 4 2
-3 4 1
-2 4 1
0 4 2
1 3 3
-2 2 1
0 2 1
-7 1 1
-5 1 3
15
*/

int main() {
    int xHead = 0;
    int yHead = 0;
    int snakeLength = 1;
    int direction = 1;

    // creates the snake table


    int **tableSnake;
    tableSnake = (int **) calloc(1,1 * sizeof (int *));

    for (int i = 0; i < 1; i++) {
        tableSnake[i] = (int *) calloc(3,3 * sizeof (int));
    }

    tableSnake[0][0] = 0;
    tableSnake[0][1] = 0;
    tableSnake[0][2] = 1;


    // reads all the variables
    int noItems;
    scanf("%d", &noItems);

    int **items;
    items = (int **) malloc(noItems * sizeof (int *));

    for (int i = 0; i < noItems; i++) {
        items[i] = (int *) malloc(3 * sizeof (int));
    }

    for (int i = 0; i < noItems; i++) {
        scanf("%d", &items[i][0]);
        scanf("%d", &items[i][1]);
        scanf("%d", &items[i][2]);
    }


    int noSteps;
    scanf("%d", &noSteps);

    // start of run

    /*
     *  legend:
     *  direction: 1 - up, 2 - down, 3 - left, 4 - right
     *  typesOfItems: 5 - fruit, 6 - turn left, 7 - turn right 
     */

    int itemHead = 0;
    int extend = 0;

    for (int i = 0; i < noSteps; i++) {
        moveSnake(&snakeLength,direction,tableSnake,extend);
        extend = 0;
        itemHead = checkItems(tableSnake, items, noItems);

        if(itemHead == -1){
            if(selfdestruct(tableSnake) == 1){
                snakeLength = 0;
                break;
            };
        }
        if (itemHead == 1) {
            extend = 1;
        } else if (itemHead == 2) {
            if(direction == 1){
                direction = 3;
            }else if(direction == 2){
                direction = 4;
            }else if(direction = 3){
                direction = 2;
            }else{
                direction = 1;
            }
        } else if (itemHead == 3) {
            if(direction == 1){
                direction = 4;
            }else if(direction == 2){
                direction = 3;
            }else if(direction = 3){
                direction = 1;
            }else{
                direction = 2;
            }
        } 
    }
    printf("%d %d %d", snakeLength, tableSnake[0][0], tableSnake[0][1]);

    return (EXIT_SUCCESS);
}

void moveSnake(int *snakeLength,int direction, int **tableSnake, int extend){
    int tempX = tableSnake[0][0];
    int tempY = tableSnake[0][1];
    int tempDirection = tableSnake[0][2];

    int tempXTail = tableSnake[*snakeLength -1][0];
    int tempYTail = tableSnake[*snakeLength -1][1];
    int tempDirectionTail = tableSnake[*snakeLength -1][2];

    int tempRep[3] = {tempXTail,tempYTail,tempDirectionTail};
    if(direction == 1){
        tempY++;
    }else if(direction == 2){
        tempY--;
    }else if(direction == 3){
        tempX--;
    }else if(direction == 4){
        tempX++;
    }

    int *temp;
    temp = malloc(3 * sizeof(int));
    for(int i = 0; i < *snakeLength; i++){
        temp = tableSnake[i];
        tableSnake[i][0] = tempX;
        tableSnake[i][1] = tempY;
        tableSnake[i][2] = tempDirection;
        tempX = temp[0];
        tempY = temp[1];
        tempDirection = temp[2];
    }
    if(extend == 1){
        // this is where the error occurs
        *snakeLength = *snakeLength +1;
        tableSnake = realloc(tableSnake, *snakeLength * sizeof(int));
        tableSnake[*snakeLength-1] = tempRep;
    }

}

int checkItems(int **tableSnake, int **items, int itemsSize){
    int *item;
    item = malloc(3 * sizeof(int));
    int itemX;
    int itemY;
    int headX = tableSnake[0][0];
    int headY = tableSnake[0][1];

    for(int i = 0; i < itemsSize; i++){
        item = items[i];
        itemX = item[0];
        itemY = item[1];
        if(itemX == headX && itemY == headY){
            return item[2];
        }
    }
    return -1;
}

int selfdestruct(int **tableSnake){
    int tempX = tableSnake[0][0];
    int tempY = tableSnake[0][1];
    int lengthTable = sizeof(tableSnake)/sizeof(tableSnake[0]);

    for(int i = 1; i < lengthTable; i++){
        if(tempX == tableSnake[i][0]){
            if(tempY == tableSnake[i][1]){
                return 1;
            }
        }
    }
    return 0;
}
#包括
#包括
void moveSnake(int*snakeLength,int direction,int**tableSnake,int extend);
int checkItems(int**tableSnake,int**items,int itemsize);
int自毁(int**tableSnake);
/*测试输入数据:
9
-5 4 2
-3 4 1
-2 4 1
0 4 2
1 3 3
-2 2 1
0 2 1
-7 1 1
-5 1 3
15
*/
int main(){
int xHead=0;
int-yHead=0;
int蛇长=1;
int方向=1;
//创建snake表
int**tableSnake;
tableSnake=(int**)calloc(1,1*sizeof(int*);
对于(int i=0;i<1;i++){
tableSnake[i]=(int*)calloc(3,3*sizeof(int));
}
tableSnake[0][0]=0;
表名[0][1]=0;
表名[0][2]=1;
//读取所有变量
国际核项目;
scanf(“%d”和“noItems”);
国际**项目;
项目=(int**)malloc(noItems*sizeof(int*);
for(int i=0;i
如果我把这两行放在一起,一个问题应该是显而易见的

tableSnake = (int **) calloc(1,1 * sizeof (int *));

然后,您没有为新的tableSnake元素分配内存,而是将它指向一个局部变量

tableSnake[*snakeLength-1] = tempRep;
…当您退出该函数时将自动释放。

以下效果:

tableSnake = realloc(tableSnake, *snakeLength * sizeof(int)); 
…不会在moveSnake()函数外部传播。一种修复方法是作为结果返回“tableSnake”

    int ** moveSnake(int *snakeLength,int direction, int **tableSnake, int extend){
      ...
      return tablesnake;
    );

...

tableSnake=moveSnake(&snakeLength,direction,tableSnake,extend);

如果您更清楚地命名变量,例如参数为“localSnake”,那么这个问题可能不会出现。尽量不要给多个var赋予相同的名称:)

您有一些内存泄漏:
item=malloc(3*sizeof(int))其他问题:
temp=malloc(3*sizeof(int));对于(inti=0;i<*snakeLength;i++){temp=tableSnake[i];tableSnake[i][0]=tempX;tableSnake[i][1]=tempY;tableSnake[i][2]=tempDirection;tempX=temp[0];tempY=temp[1];tempDirection=temp[2];}    int ** moveSnake(int *snakeLength,int direction, int **tableSnake, int extend){
      ...
      return tablesnake;
    );

...

tableSnake=moveSnake(&snakeLength,direction,tableSnake,extend);