Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 376
Wjdavis5 Avatar asked Oct 12 '12 16:10

Wjdavis5


People also ask

What is the value of uninitialized variable?

INTRODUCTION: An uninitialized variable has an undefined value, often corresponding to the data that was already in the particular memory location that the variable is using.

How do you know if a variable is uninitialized?

Use the typeof operator to check if a variable is defined or initialized, e.g. if (typeof a !== 'undefined') {} . If the the typeof operator doesn't return a string of "undefined" , then the variable is defined.

How does C++ handle uninitialized variables?

Unlike some programming languages, C/C++ does not initialize most variables to a given value (such as zero) automatically. Thus when a variable is given a memory address to use to store data, the default value of that variable is whatever (garbage) value happens to already be in that memory address!

What is the value of uninitialized variable in Java?

Yeah in Java , uninitialized variables automatically initialized to zero but if the variable is an instance variable and a class variable because if we don't initialize the instance variable the system by default initialize them to their respective default values.


3 Answers

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.

like image 160
Andy Ross Avatar answered Sep 25 '22 13:09

Andy Ross


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.

like image 30
Alok Save Avatar answered Sep 24 '22 13:09

Alok Save


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

like image 38
DaveS Avatar answered Sep 24 '22 13:09

DaveS