Wjdavis5
Wjdavis5

Reputation: 4151

How are the values of uninitialized variables determined?

Given a program of :

int main()
{

    short myVariableName1;  // stores from -32768 to +32767
    short int myVariableName2;  // stores from -32768 to +32767
    signed short myVariableName3;  // stores from -32768 to +32767
    signed short int myVariableName4;  // stores from -32768 to +32767
    unsigned short myVariableName5;  // stores from 0 to +65535
    unsigned short int myVariableName6;  // stores from 0 to +65535

    int myVariableName7;  // stores from -32768 to +32767
    signed int myVariableName8;  // stores from -32768 to +32767
    unsigned int myVariableName9;  // stores from 0 to +65535

    long     myVariableName10;  // stores from -2147483648 to +2147483647
    long     int myVariableName11;  // stores from -2147483648 to +2147483647
    signed   long myVariableName12;  // stores from -2147483648 to +2147483647
    signed   long int myVariableName13;  // stores from -2147483648 to +2147483647
    unsigned long myVariableName14;  // stores from 0 to +4294967295
    unsigned long int myVariableName15;  // stores from 0 to +4294967295
    cout << "Hello World!" << endl;
    cout << myVariableName1 << endl;
    cout << myVariableName2 << endl;
    cout << myVariableName3 << endl;
    cout << myVariableName4 << endl;
    cout << myVariableName5 << endl;
    cout << myVariableName6 << endl;
    cout << myVariableName7 << endl;
    cout << myVariableName8 << endl;
    cout << myVariableName9 << endl;
    cout << myVariableName10 << endl;
    cout << myVariableName11 << endl;
    cout << myVariableName12 << endl;
    cout << myVariableName13 << endl;
    cout << myVariableName14 << endl;
    cout << myVariableName15 << endl;
    cin.get();

    return 0;
}

Printing out the unassigned variables will print whatever was stored in that memory location previously. What I've noticed is that across multiple consecutive executions the printed values are not changing - which tells me that the locations in memory are the same each time they execute.

I'm just curious as to how this is determined, why this is so.

Upvotes: 5

Views: 1314

Answers (5)

Ed Heal
Ed Heal

Reputation: 60037

Unlike humans, computers are deterministic.

Usually a good idea to use the compiler options to pick up when reading a variable that has not been given a value.

So the OS picks up the code and does exactly the same thing ever time. Hence the same result.

But if you start fiddling with the code the executable will be different. As you have not been specific you get a different result the next time around.

So in summary just use all the features of the compiler to help you spot this error and get into the habit of giving the variables values before using that variable.

Upvotes: 2

DaveS
DaveS

Reputation: 511

They're all stack based. Probably the startup code has already used those locations, so you're getting whatever it left in them.

Upvotes: 3

user1708860
user1708860

Reputation: 1763

The behaviour is not defined as "whatever was stored in that memory location previously", but it is rather not defined at all. There is no guarantee to what will happen.

My best guess is that your operation system is using virtual memory (as most modern OS do), thus giving you the same memory addresses every time.

Upvotes: 2

Alok Save
Alok Save

Reputation: 206636

They can be anything don't rely on them to be anything specific.
Technically, the values are Indeterminate.

Note that using an Indeterminate value results in Undefined Behavior.

Upvotes: 3

Andy Ross
Andy Ross

Reputation: 12051

Those variables live on the stack. The execution of your program looks to be deterministic, so every time you run it the same things happen. It's not choosing the same address necessarily (many runtimes these days make use of Address Space Randomization techniques to ensure that the stack addresses are not the same between runs), but the relative addresses on the stack contain the same data every time.

Upvotes: 3

Related Questions