Home > database >  Variable loses its value C
Variable loses its value C

Time:08-13

I'm working on this project where a user has to guess a word (wordToGuess) and he has a number of attempts. The problem is that the variable "wordToGuess" loses its value when the code arrives in the point marked ("HERE LOSES ITS VALUE). I don't know how to solve this problem, I've tried in many ways. Thank u for your help! (checkExistence is a function that checks if the word is present in the dictionary)

void  newGame(node* head){
    char wordToGuess[10];
    char attempt[10];
    int numberOfAttempts = 0;

   if (scanf("%s" , wordToGuess) != 1){
        printf("error1");
    }
    getchar();

    if (scanf("%d", &numberOfAttempts) != 1){
        printf("error2");
    }
    getchar();

    while(numberOfAttempts > 0){
        if (scanf("%s", attempt) != EOF){
            if (attempt[0] != ' '){
                if (checkExistence(head, attempt) == false){
                    printf("not_exists\n");
                }else{
                    if (strcmp(wordToGuess, attempt) == 0){
                        printf("ok\n");
                        return;
                    }else{
                        //code
                        numberOfAttempts--;
                    }
                }
            }else{
                if (attempt[0] == ' ' && attempt[1] == 's'){
                    //HERE LOSES ITS VALUE
                }else if (attempt[0] == ' ' && attempt[1] == 'i'){
                    //other code
                }
            }
        }else{
            printf("ko");
            return;
        }
    }
    return;
}

Here a test case:

 2rj9R (wordToGuess)
 18 (numerAttemps)
 DP3wc (attempt)
 7PGPU (attempt)
 2rz9R (attempt)
  print_list (from this point I lose the value of wordToGuess)
 2rj9R (attempt)

CodePudding user response:

As the others have point, you're probably causing a buffer overflow in your attempt buffer which overwrites your wordToGuess buffer since your attempt and wordToGuess buffer is stored like this in your memory:

<attempt buffer> | <word To Guess>

You have two possible fixes for this (as the comments have said...):

  1. A little fix would be to set a limit of how many characters should be read from stdin to scanf like this:
scanf("%9s" , wordToGuess) // you need 9, because your buffer can store up to 
                           // 10 bytes but don't forget that `scanf` is also
                           // addinng `\0` for you!

and don't forget to flush the rest of the user input if you want that the user should be only able to insert at most 9 characters!

  1. Increase the buffer size of your attempt (and wordToGuess) buffer but also add those read-limits for scanf which is described in the first point.

CodePudding user response:

At the indicated point of the code where wordToGuess appears to lose its value, it is a dead variable. If you're looking at optimized code in a debugger, you may find that the variable doesn't exist there any more.

At a given point in a program, a dead variable is one which is never used past that point. All control flows out of that point reach the termination of that code, without ever using the variable again. Simple example:

{
  int x = 3;
  // x is live here: there is a next reference
  printf("%d\n", x);
  // x is now dead: it is not referenced after the above use
  printf("foo\n");
}

In the generated code, the compiler may arrange to re-use the resources tied to a dead variable as soon as it is dead: give its register and memory location to something else.

In the debugger, if we put a breakpoint on the printf("foo\n") and try to examine x, we might get a strange result.

To have the best chance of seeing the expected result (that x still exists, and is retaining its most recent value), we have to compile with optimizations disabled.

  • Related