我一直在 Windows 上开发这个非常简单的 C 游戏,我使用命令 gcc matches.c -o ./matches
来编译它。
我已将代码导入到 Mac 上,并使用 gcc
和 clang
重新编译它。
使用这两种技术,程序有时会完全崩溃,关闭我的终端 session 并输出此内容。
matches(54122,0x1137715c0) malloc: *** error for object 0x7ffee9e8ba40: pointer being realloc'd was not allocated
matches(54122,0x1137715c0) malloc: *** set a breakpoint in malloc_error_break to debug
Abort trap: 6
Broadcast Message from _appleevents@(myname).local
(no tty) at 20:33 CET...
matches(54122,0x1137715c0) malloc: *** error for object 0x7ffee9e8ba40: pointer
being realloc'd was not allocated
该代码在 Windows 上完全没有错误。
我认为这与 xcode 或类似的东西有关。 有谁知道如何解决这个问题吗?
顺便说一句,这是代码。程序在 getline()
上的设置函数中崩溃
#include <stdio.h>
#include <stdlib.h>
void show_matches(int n);
void setup(int *numberOfPlayersA, int *gamemodeA, int *numberOfMatchesA);
void changePlayersTurn(int *currentPlayerA, int numberOfPlayers);
int random_move();
int main(void) {
char *input;
int currentPlayer = 1;
size_t n = 0;
int numberOfPlayers = 0;
int gamemode = 0;
int numberOfMatches = 0;
int move = 0;
setup(&numberOfPlayers, &gamemode, &numberOfMatches);
show_matches(numberOfMatches);
while (numberOfMatches >= 1) {
printf("\033[1;31m");
printf("\n\nPlayer %d> ", currentPlayer);
printf("\033[0m");
if (gamemode == 2 || currentPlayer == 1) {
getline(&input, &n, stdin);
move = atoi(input);
if (move > 3 || move < 1 ) {
move = 1;
}
} else {
int randomMove = random_move();
move = randomMove;
printf("%d", randomMove);
}
numberOfMatches -= move;
show_matches(numberOfMatches);
if (numberOfMatches >= 1) {
changePlayersTurn(¤tPlayer, numberOfPlayers);
}
}
printf("\n\nPlayer %d lost\n\n", currentPlayer);
return 0;
}
void setup(int *numberOfPlayersA, int *gamemodeA, int *numberOfMatchesA) {
char *input;
size_t n = 0;
printf("--The matches--\n\n");
printf("Do you plan on playing against:\n\t1. The computer\n\t2. Other persons\n\n(1 / 2) > ");
getline(&input, &n, stdin);
printf("1");
*gamemodeA = atoi(input);
printf("2");
if (*gamemodeA == 2) {
printf("\n\nPlease enter the number of players: ");
getline(&input, &n, stdin);
*numberOfPlayersA = atoi(input);
}
printf("Enter the number of matches: ");
getline(&input, &n, stdin);
*numberOfMatchesA = atoi(input);
*numberOfPlayersA = 2;
printf("4");
}
void changePlayersTurn(int *currentPlayerA, int numberOfPlayers) {
if (*currentPlayerA == numberOfPlayers) {
*currentPlayerA = 1;
} else {
*currentPlayerA += 1;
}
}
void show_matches(int n) {
for (int i = 0; i < n; i++) {
printf("|");
}
}
int random_move() {
int num = (rand() % (3 - 1 + 1)) + 1;
return num;
}
最佳答案
在主要中:
char *input; ... getline(&input, &n, stdin);
您调用getline而不初始化输入,如果(未定义)值不为NULL,getline将释放它,因为它不是已分配 block 的地址,您有错误
您需要例如:
input = NULL;
getline(&input, &n, stdin);
move = atoi(input);
if (move > 3 || move < 1 ) {
move = 1;
}
free(input);
您在设置中遇到类似的错误:
char *input; ... getline(&input, &n, stdin); ... getline(&input, &n, stdin);
例如,在第一次调用getline之前,您需要将input设置为NULL,并且在最后一次调用之后需要free :
char *input;
...
input = NULL;
getline(&input, &n, stdin);
...
getline(&input, &n, stdin);
*numberOfMatchesA = atoi(input);
free(input);
补充说明:
- 您错过了检查getline的结果
- 使用atoi不安全,如果用户输入错误,你会得到0,最好使用strtod或sscanf
- 在main中,当输入值不在1和3之间时,最好重做输入,而不是强制为1
关于c - 被realloc'd的指针未分配,在malloc_error_break中设置断点进行调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55129558/