gpt4 book ai didi

c - 使用 realloc() 时的 SIGABRT

转载 作者:行者123 更新时间:2023-11-30 19:35:11 25 4
gpt4 key购买 nike

我正在尝试为我的家庭作业制作贪吃蛇游戏的 C 版本。我已经实现了游戏中所有必要的机制,但我在动态内存分配方面遇到了问题。我的“蛇”作为 body 部位及其位置的列表存储在数组中。如果蛇吃掉了水果,我会尝试改变蛇的长度并将另一个 body 部分添加到数组中。由于我事先不知道蛇的最终大小,因此我尝试使用 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;
}

最佳答案

如果我把这两行放在一起,一个问题应该是显而易见的?

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

...

tableSnake = realloc(tableSnake, *snakeLength * sizeof(int));

然后您不会为新的 tableSnake 元素分配内存,而是将其指向局部变量...

tableSnake[*snakeLength-1] = tempRep;

...当您退出该函数时,它将自动释放。

关于c - 使用 realloc() 时的 SIGABRT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43002271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com