我一直在 Windows 上开发这个非常简单的 C 游戏,我使用命令gcc matches.c -o ./matches来编译它。我已将代码导入到 Mac 上,并使用gcc和重新编译了它clang。使用这两种技术,程序有时会完全崩溃,关闭我的终端会话并输出它。
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
Run Code Online (Sandbox Code Playgroud)
该代码在 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;
}
Run Code Online (Sandbox Code Playgroud)
主要:
Run Code Online (Sandbox Code Playgroud)char *input; ... getline(&input, &n, stdin);
您在不初始化输入的情况下调用getline,如果(未定义)值不为 NULL getline将释放它,并且因为它不是已分配块的地址,所以您会遇到错误
例如你需要:
input = NULL;
getline(&input, &n, stdin);
move = atoi(input);
if (move > 3 || move < 1 ) {
move = 1;
}
free(input);
Run Code Online (Sandbox Code Playgroud)
您在设置中遇到类似的错误:
Run Code Online (Sandbox Code Playgroud)char *input; ... getline(&input, &n, stdin); ... getline(&input, &n, stdin);
您需要在第一次调用getline之前将输入设置为 NULL ,并且在最后一次调用之后需要释放,例如:
char *input;
...
input = NULL;
getline(&input, &n, stdin);
...
getline(&input, &n, stdin);
*numberOfMatchesA = atoi(input);
free(input);
Run Code Online (Sandbox Code Playgroud)
附加说明:
| 归档时间: |
|
| 查看次数: |
880 次 |
| 最近记录: |