簡體   English   中英

解碼這些 Valgrind 調試器 memory 錯誤在我的代碼中的含義

[英]Decoding what these Valgrind debugger memory errors mean within my code

相當新的學生到 c 在這里。 剛剛完成了 Linux 子系統的設置,因此我可以運行 Valgrind 進行調試。 我正在處理一個需要多個 arrays 字符串的作業,並有組織地存儲這些值。 我相信我的代碼的邏輯是合理的,但是當它運行時立即退出進程,我什至沒有時間輸入程序將運行的 n 次。 我相信這是一個分段錯誤,並下載了 valgrind 來查明問題。 但是,我無法理解這些錯誤消息的含義。 似乎只有一兩個錯誤,但我可能是錯的。 我可能需要使用動態 memory 函數(malloc、calloc ...)來使其工作,但在 memory 分配方面我什至是初學者,甚至不知道從哪里開始。 關於我的 valgrind 錯誤意味着什么,或者我應該如何 go 關於動態分配 memory 的任何建議將不勝感激:)

任何人需要的任何其他信息,請隨時詢問。

我已經消除了出現的任何我知道可以解決的錯誤。 現在只剩下這兩個了。 我相信這與分段錯誤或未正確分配 memory 有關,但我不確定。

這是我當前的代碼。 它可能有點混亂,或者有不好的空格。 我也對我的編碼風格持開放態度:)

    int main() {
int n, i, j = 0, k, m, p, flag, key, count;
char choiceUQ, choiceSS[100];
char nameTemp[100], printStu[n][100];
char stuName[n][100], stuSym[n][n][100];        

scanf("%d", &n); //Scan in n number of times u or q will run

for(i = 0; i < n; i++) { //initialize all symptoms to be null for later if statement.
    for(k = 0; k < n; k++) {
        strcpy (stuSym[k][i], "");
    }
}

for(i = 0; i < n; i++) {
    strcpy(nameTemp, "");
    scanf("%c", &choiceUQ);
    if(choiceUQ == 'u') { 
        flag = 0; //set flag to 0, will be changed if name is already in database.
        scanf("%s", nameTemp);
        for(k = 0; k < i; k++) { //for loop checks if name is already in database.
            if(nameTemp == stuName[k]) { 
                flag = 1; //sets flag if name is in database.
                for(m = 0; m < i; m++) { //checks for next available string array spot for symptoms.
                    if(stuSym[m][k] == "")
                       scanf("%s", stuSym[m][k]);
                }
            }
        }
        if(flag == 0) { //checks for set flag, if no flag is set, it is a new name, so symptom spot will always be 0.
           strcpy(stuName[i], nameTemp);
           scanf("%s", stuSym[0][i]);
        }
}
    if(choiceUQ == 'q') {
        scanf("%s", choiceSS); //checks for input student or symptom, and executes code related to it.
        if(choiceSS == "student") {
            scanf("%s", nameTemp);
            for(k = 0; k < i; k++) { //searches for student name in database
                if(nameTemp == stuName[k]) 
                    key = k;
            }
            for(m = 0; m < i; m++) {
                printf("%s\n", stuSym[m][key]); //prints all symptoms that student has reported
            }
        }
        if(choiceSS == "symptom") {
            count = 0; //initialize count of symptoms at 0
            scanf("%s", nameTemp);
            for(k = 0; k < i; k++) {
                for(m = 0; m < i; m++) {
                   if(nameTemp == stuSym[m][k]) { //nested for loops lead to if loop to check if each student has the given symptom
                      strcpy(printStu[count], stuName[k]);
                      count++;
                    }
                }
            }
            for(p = 0; p < count; p++) { //prints all students copied into printStu array
                printf("%s", printStu[p]);
            }
        }
    }      
}

return 0;

}

我在 valgrind 中遇到的錯誤如下所示

==4540== error calling PR_SET_PTRACER, vgdb might block

==4540== Use of uninitialised value of size 8
==4540==    at 0x108B9C: main (santos_pandemic2.c:12)


==4540== Use of uninitialised value of size 8
==4540==    at 0x4EB7EC0: __isoc99_scanf (isoc99_scanf.c:27)

^[[A

==4540== Conditional jump or move depends on uninitialised value(s)
==4540==    at 0x108C20: main (santos_pandemic2.c:14)

==4540== Conditional jump or move depends on uninitialised value(s)
==4540==    at 0x109115: main (santos_pandemic2.c:20)

==4540== HEAP SUMMARY:
==4540==     in use at exit: 0 bytes in 0 blocks
==4540==   total heap usage: 1 allocs, 1 frees, 4,096 bytes allocated

==4540== All heap blocks were freed -- no leaks are possible
char nameTemp[100], printStu[n][100];
char stuName[n][100], stuSym[n][n][100];

您使用未初始化的值 n 來聲明這些 arrays。 C 不夠聰明,無法確定它應該在使用 scanf 給出 na 值后聲明 arrays 。

由於您希望動態分配這些數組(使用從 scanf 獲得的值),我建議使用 malloc 為它們分配 memory。 或者查看“可變長度數組”以及如何使它們工作。

暫無
暫無

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

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