0

So the function "thinkOfANumber" gives the variable "x" the value 108. Then, we go in to the function "mindReading" that has the variable "secrets" which isn't given any value. But somehow it get the same value as "x". My guess is that it has something to do with the stack and memory. Could someone explain it for me?

The code:

void thinkOfANumber(){
    int x = 108;
    printf( "This function thinks of a secret number (%d)\n", x);
}

void mindReading(){
    int secret;
    printf( "This function believes that the other functions secret is: %d!\n", secret); //Prints 108
}

void main(){
    thinkOfANumber();
    mindReading();
    return 0;
}

davzter
  • 1
  • 1
  • 5
    [Undefined behaviour](https://en.cppreference.com/w/cpp/language/ub) works in mysterious ways, and using an uninitialized variable is UB.. – user4581301 May 28 '20 at 22:37
  • 1
    Side note: `return 0;` looks odd in a `void main()` function. I recommend changing the return type because `main` must return `int`. The C++ Standard says so. – user4581301 May 28 '20 at 22:39
  • Try compiling this code with optimisation enabled. That is enough to break it, see: https://wandbox.org/permlink/abDKQVpFjjMxbnnK – Paul Sanders May 28 '20 at 22:39
  • It's definitely undefined behavior. But it happens to work because `secret` very likely shares the same stack address as `x`. But compile with `-O3` optimizations and it all goes out the window. – selbie May 28 '20 at 22:41
  • I once had something like this where the behaviour changed by adding or removing comments from the source code. Undefined behaviour can do ANYTHING, including time travel (see https://stackoverflow.com/questions/24527401/undefined-behavior-causing-time-travel ) – Jerry Jeremiah May 28 '20 at 22:42
  • Your `thinkOfANumber` doesn't return a value nor does it have any parameters to change. This function doesn't communicate with the calling function. – Thomas Matthews May 28 '20 at 22:47
  • Your `mindReading` function declares a variable, but doesn't initialize it. The uninitialized value is printed. The value could be anything because you did not initialize it. – Thomas Matthews May 28 '20 at 22:48

3 Answers3

5

Reading from a default initialized int is undefined behavior, since it has an indeterminate value.

When reading from secret, the compiler might be reading the value from a register that just happened to be holding the value of x.

In any case, this is not behavior that you can rely upon, even on the same machine, with the same compiler, with the same compilation flags, etc.

cigien
  • 57,834
  • 11
  • 73
  • 112
2

Your variable isn't initialised and will give undefined behaviour. This may include but not limited to -

  • Your program produces different results every time it is run.

  • Your program consistently produces the same result.

  • Your program works on some compilers but not others.


In your case, the uninitialised variable is probably pointing to the memory location storing the address of the initialised variable x. But atleast according to me, this wouldn't be the case everytime and would run in an undefined manner on different compilers or different machines.

Remember to Always initialise your variables

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
0

C/C++ local variables are given memory when it is needed. Here the secret variable doesn't have any memory assigned. So it is actually following the address of x here. Look at this code here,

#include<stdio.h>

void a(){
    int x = 40;
    int y=30;
    printf( "This function thinks of a secret number (%d)\n", x);
}

void b(){
    int xx;
    int z;
    printf( "This function believes that the other functions secret is: %d !\n", xx);
    printf("%d\n",z);
}

int main(){
    a();
    b();
    return 0;
}

Here the secret is getting value of x and z is getting value of y. If you change the value of secret in the function b then this will assign a new memory for the variable secret.