空闲指针即使被分配也不起作用

时间:2018-11-04 17:50:23

标签: c string pointers memory free

我的程序中有两个指针,释放它们是行不通的,我没有收到任何错误,只是来自Visual Studio的错误声音。 如果我不释放它们,该程序运行会很好(但是我知道我必须这样做)。 这是我的代码,所有使用这两个指针的代码,希望可以对我有所帮助。

void gameProcess(char *word, int len) 
{
    int i, letterExist, life = 7, temp;
    char *pGuess, *bank, c;
    if (!(bank = (char*)malloc(N)))
        printf("Error!! Allocated memory failure!");
    bank[N] = '\0';
    if (!(pGuess = (char*)malloc(len)))
        printf("Error!! Allocated memory failure!");
    pGuess[len] = '\0';
    guessVisual(pGuess);
    while (life > 0 && len > 0)
    {
        letterExist = 0;
        printf("\n\nEnter your guess please:\n");
        c = guessInterface(word, pGuess, bank);
        letterExist = checkIfExist(word, c, len);
        temp = replaceIfExist(word, c, pGuess);
        if (letterExist > 0)
            len = rightGuess(pGuess, len, temp);
        else
            life = wrongGuess(life);
        if (len == 0)
            win();
        if (life == 0)
            lose();
    }
    free(pGuess);//***************
    free(bank);//***************
}

谢谢! 如果需要其他信息,请告诉我。

0 个答案:

没有答案