gpt4 book ai didi

c - Malloc 和总线 10 运行时错误帮助 C

转载 作者:行者123 更新时间:2023-11-30 20:40:44 24 4
gpt4 key购买 nike

因此,我正在处理一项特定的编程作业,我认为该作业需要使用我不熟悉的 malloc() 函数。我的理解是,如果您使用 malloc() ,则必须在使用完内存后释放内存,否则最终可能会出现奇怪的行为。我想知道这是否就是我每隔一段时间就会收到总线 10 运行时错误的原因?

作业是编写一个石头剪刀布游戏,这就是我的想法。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

char* getUserChoice()
{
/* Prompt the user "Enter rock, paper, or scissors: " and return
the string they enter */
printf("Enter rock, paper, or scissors: ");
char * uChoice = malloc(sizeof(char) * 128);
scanf("%s", uChoice);
return uChoice;
}

char* getComputerChoice()
{
srand (time(NULL));
/* get a pseudo-random integer between 0 and 2 (inclusive) */
int randChoice = rand() % 3;
char * cpuChoice = malloc(sizeof(char) * 128);
/* If randChoice is 0, return "rock", otherwise if randChoice is 1,
return "paper", and if randChoice is 2, return "scissors". */
if (randChoice == 0)
cpuChoice = "rock";
else if (randChoice == 1)
cpuChoice = "paper";
else
cpuChoice = "scissors";
return cpuChoice;
}

char* compare(char* choice1, char* choice2)
{
/* Implement the logic of the game here. If choice1 and choice2
are equal, the result should be "This game is a tie."
Make sure to use strcmp for string comparison.*/

char * cmpChoice = malloc(sizeof(char) * 128);
int comparedValue = strcmp(choice1,choice2);
if (comparedValue == 0)
cmpChoice = "This game is a tie.";
else
{
if ((strcmp(choice1, "rock") == 0 && strcmp(choice2, "paper") == 0) ||
(strcmp(choice1,"paper") == 0 && strcmp(choice2, "scissors") == 0) ||
(strcmp(choice1, "scissors") == 0 && strcmp(choice2, "rock") == 0))
cmpChoice = strcat(choice2, " wins");
else
strcat(choice1, " wins);
}
return cmpChoice;
}

int main(int argc, char** argv)
{
char *userChoice, *computerChoice, *outcome;

userChoice = getUserChoice();
computerChoice = getComputerChoice();
outcome = compare(userChoice, computerChoice);

printf("You picked %s.\n", userChoice);
printf("Computer picked %s\n", computerChoice);
printf("%s\n", outcome);
return 0;
}

我描述的奇怪行为有时输出会像这样

Enter rock, paper, or scissors: paper
You picked paper wins.. // why is it saying "You picked paper wins.."
Computer picked rock
paper wins.

其他时候无需重新编译即可

Enter rock, paper, or scissors: scissors
Bus error: 10 // possibly due to not calling free()?

如果有人可以帮助我了解如何在返回指针之前释放分配的内存,那就太好了。显然,仅使用字符串会更容易,但要求使用 char * 类型。

感谢您提供的所有帮助或见解。

使用更新的代码进行编辑

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

char* getUserChoice()
{
/* Prompt the user "Enter rock, paper, or scissors: " and return
the string they enter */
printf("Enter rock, paper, or scissors: ");
char * uChoice = malloc(sizeof(char) * 128);
scanf("%s", uChoice);
return uChoice;
}

char* getComputerChoice()
{
srand (time(NULL));
/* get a pseudo-random integer between 0 and 2 (inclusive) */
int randChoice = rand() % 3;
char * cpuChoice = malloc(sizeof(char) * 128);
/* If randChoice is 0, return "rock", otherwise if randChoice is 1,
return "paper", and if randChoice is 2, return "scissors". */
if (randChoice == 0)
strcpy(cpuChoice, "rock");
else if (randChoice == 1)
strcpy(cpuChoice, "paper");
else
strcpy(cpuChoice, "scissors");
return cpuChoice;
}

char* compare(char* choice1, char* choice2)
{
/* Implement the logic of the game here. If choice1 and choice2
are equal, the result should be "This game is a tie."
Make sure to use strcmp for string comparison.*/

char * cmpChoice = malloc(sizeof(char) * 128);
int comparedValue = strcmp(choice1,choice2);
if (comparedValue == 0)
strcpy(cmpChoice, "This game is a tie.");
else
{
if ((strcmp(choice1, "rock") == 0 && strcmp(choice2, "paper") == 0) ||
(strcmp(choice1,"paper") == 0 && strcmp(choice2, "scissors") == 0) ||
(strcmp(choice1, "scissors") == 0 && strcmp(choice2, "rock") == 0))
{
strcat(cmpChoice, choice2);
strcat(cmpChoice, " wins");
}
else
{
strcat(cmpChoice, choice1);
strcat(cmpChoice, " wins.");
}
}
return cmpChoice;
}

int main(int argc, char** argv)
{
char *userChoice, *computerChoice, *outcome;

userChoice = getUserChoice();
computerChoice = getComputerChoice();
outcome = compare(userChoice, computerChoice);

printf("You picked %s.\n", userChoice);
printf("Computer picked %s\n", computerChoice);
printf("%s\n", outcome);
return 0;
}

所以,非常感谢大家的评论。我已经修复了代码上的一些地方,并且它似乎可以编译和运行,没有错误。现在,我试图弄清楚何时free() 分配的内存。我显然无法在返回后释放它,但我需要返回该值。

我是否可以将其复制到 char,然后释放分配的原始内存?

再次感谢

最佳答案

您不需要使用malloc。只需将一个 char 指针传递给该方法,而不是返回一个。

void getUserChoice(char * choice)
{
/* Prompt the user "Enter rock, paper, or scissors: " and return
the string they enter */
printf("Enter rock, paper, or scissors: ");
scanf("%s", choice);
}

然后在 main 中像这样使用它...

char uChoice[128];
getUserChoice(uChoice);

关于c - Malloc 和总线 10 运行时错误帮助 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20964541/

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