简体   繁体   English

解码这些 Valgrind 调试器 memory 错误在我的代码中的含义

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

Fairly new student to c here.相当新的学生到 c 在这里。 Just finished setting up a Linux subsytem so i can run Valgrind for debugging purposes.刚刚完成了 Linux 子系统的设置,因此我可以运行 Valgrind 进行调试。 I'm working on an assignment that requires multiple arrays of strings, and organized storing of those values.我正在处理一个需要多个 arrays 字符串的作业,并有组织地存储这些值。 I believe the logic of my code is sound, however when running it immediately exits the process leaving no time for me to even input the n number of times the program will run.我相信我的代码的逻辑是合理的,但是当它运行时立即退出进程,我什至没有时间输入程序将运行的 n 次。 I believe this is a segmentation fault, and downloaded valgrind to pinpoint the problem.我相信这是一个分段错误,并下载了 valgrind 来查明问题。 However i am having trouble understanding what these error messages mean.但是,我无法理解这些错误消息的含义。 It seems there is only one or two errors, but i could be wrong.似乎只有一两个错误,但我可能是错的。 I may need to use dynamic memory functions (malloc, calloc...) to make it work, but i am even more of a beginner when it comes to memory allocation and am not sure even where to start.我可能需要使用动态 memory 函数(malloc、calloc ...)来使其工作,但在 memory 分配方面我什至是初学者,甚至不知道从哪里开始。 Any advice on what my valgrind errors mean, or how i should go about dynamically allocating memory would be greatly appreciated:)关于我的 valgrind 错误意味着什么,或者我应该如何 go 关于动态分配 memory 的任何建议将不胜感激:)

Any additional information anyone needs, feel free to ask.任何人需要的任何其他信息,请随时询问。

Ive eliminated any error that showed up that i knew could be solved.我已经消除了出现的任何我知道可以解决的错误。 Now only these two remain.现在只剩下这两个了。 I believe it has something to do with a segmentation fault or not allocating memory correctly, but i am not sure.我相信这与分段错误或未正确分配 memory 有关,但我不确定。

Here is my current code.这是我当前的代码。 It may be a bit messy, or have bad whitespace.它可能有点混乱,或者有不好的空格。 I am also open to criticism on my coding style:)我也对我的编码风格持开放态度:)

    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;

} }

The error i am getting in valgrind is shown below我在 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];

You use the uninitialised value n to declare these arrays.您使用未初始化的值 n 来声明这些 arrays。 C isn't smart enough to figure out that it should declare the arrays after you give na value using scanf. C 不够聪明,无法确定它应该在使用 scanf 给出 na 值后声明 arrays 。

Since you want these array's to be dynamically allocated (using the value you get from scanf), I would suggest using malloc to allocate memory for them.由于您希望动态分配这些数组(使用从 scanf 获得的值),我建议使用 malloc 为它们分配 memory。 Or look into 'variable length arrays' and how you can make them work.或者查看“可变长度数组”以及如何使它们工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM