簡體   English   中英

無法運行此 C 代碼:沒有錯誤或警告,但程序崩潰了

[英]Can't run this C code: no errors or warnings but the program just crashes

我沒有抱怨代碼結果,但是,當我運行代碼時,IDE 立即停止工作。

我嘗試了很多 IDE 和編輯器,但仍然沒有做任何事情,也沒有錯誤或警告。

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

#define NUM_STRING 4
#define MAX_STRING_SIZE 40

char* getWord()
{
    static char words[NUM_STRING][MAX_STRING_SIZE] = {
        "game",
        "hello",
        "program",
        "pointer"
    };
    int randomIndex = rand() % 4;
    char* selectedWord = words[randomIndex];
    return selectedWord;
}

char GuessLetter()
{
    char letter;
    int value=1;

    while(value)
    {
        printf("\nmake a guess: ");
        if(scanf("%c", &letter) != 1)
        {
            printf("Please re-enter a letter");
        }
        else
        {
            value = 0;
        }
    }
return letter;
}

int checkchar(char* PW)
{
    int i, len = strlen(PW);
    for(i=0; i < len; i++)
    {
        if(PW[i]=='_')
        {
            return 1;
        }
    }
return 0;
}
char* printafter( char *currentWord, int len, char letter, char *PW)
{
    int i;

    for(i=0; i<len; i++)
    {
        if(letter == currentWord[i])
        {
            PW[i] = letter;
        }
        printf("%c ", PW[i]);
    }
return PW;
}

void startGame()
{
    char* currentWord = getWord();
    char *PartialWord=NULL;
    int i, length = strlen(currentWord);

    printf("%s\n", currentWord);
    for(i=0; i<length; i++)
    {
        PartialWord[i] = '_';
        printf("_ ");
    }

    if(!(PartialWord = (char*)malloc(length)))
       {
           printf("Sorry there was an allocation error! ");
           exit(1);
       }

    while(checkchar(PartialWord))
    {
        PartialWord = printafter(currentWord, length, GuessLetter(), PartialWord);
    }
}
int main() {
    srand(time(NULL));
    startGame();
    return 0;
}

您代碼中的主要問題是您試圖在分配數據之前將數據分配給PartialWord數組! 這會導致未定義的行為,並且幾乎任何事情都可能發生 - 包括數據的部分輸出,然后是程序崩潰。

只需將malloc代碼移動到startGame函數中的for循環之前(如下所示)......並記住在完成后free()內存:

void startGame()
{
    char* currentWord = getWord();
    char* PartialWord = NULL;
    int i, length = strlen(currentWord);

    // You MUST have this allocation BEFORE you assign the '_' characters in the for loop!
    if (!(PartialWord = malloc(length))) { // Also, yoi don't need to (and shouldn't) cast the malloc return
        printf("Sorry there was an allocation error! ");
        exit(1);
    }

    printf("%s\n", currentWord);
    for (i = 0; i < length; i++) {
        PartialWord[i] = '_'; // In your code, PartialWord is NULL when you do this!
        printf("_ ");
    }

    while (checkchar(PartialWord)) {
        PartialWord = printafter(currentWord, length, GuessLetter(), PartialWord);
    }

    free(PartialWord); // Remember to free the memory when you're done with it!
}

關於是否對malloc的結果進行malloc ,請看這里: Do I cast the result of malloc? .

隨時要求任何進一步的澄清和/或解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM