所以我有一個特定的編程任務,我正在努力,我相信需要使用我不熟悉的malloc()
函數。我的理解是,如果您使用malloc()
,則必須在完成使用時釋放內存,否則最終會出現奇怪的行爲。我想知道這是爲什麼我每隔一段時間都會遇到總線10運行時錯誤?Malloc和總線10運行時錯誤幫助C
這個任務是寫一個石頭剪刀紙遊戲,這就是我想出的。
#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()
malloced內存。我顯然不能在返回後釋放它,但我需要返回值。
我會將它複製到char
然後釋放分配的原始內存?
再次感謝
'cpuChoice = 「foo」 的複製串;',內存泄漏。使用strcpy或類似的東西爲你的男子空間。並且你在getComputerChoice()中追蹤你的字符串,所以你在第一個結果中獲得** win **。如果電腦贏了,你正在做strcat字符串,所以seg故障。 – moeCake
你可以擴展更多關於strcat字符串文字的東西嗎?我認爲這是該計劃的主要問題,因爲我拿出來了,而且我再也沒有遇到巴士錯誤。 – user2948847
[strcat](http://www.cplusplus.com/reference/cstring/strcat/)將修改** destination **的內容,因此您不能將字符串字面量作爲第一個參數傳遞。 Malloced space可以,但是你搞砸了getComputerChoice()。 – moeCake