简体   繁体   中英

Decoding what these Valgrind debugger memory errors mean within my code

Fairly new student to c here. Just finished setting up a Linux subsytem so i can run Valgrind for debugging purposes. I'm working on an assignment that requires multiple arrays of strings, and organized storing of those values. 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. I believe this is a segmentation fault, and downloaded valgrind to pinpoint the problem. 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. Any advice on what my valgrind errors mean, or how i should go about dynamically allocating memory would be greatly appreciated:)

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.

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

==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. C isn't smart enough to figure out that it should declare the arrays after you give na value using scanf.

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. Or look into 'variable length arrays' and how you can make them work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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