0

I was writing a simple program to test how the scope of variables works, but I'm obtaining unexpected results, so I was hoping you could give me an hand to understand them. I compiled this code

#include<iostream>
using namespace std;

void myFunction1()
    {
    int e;
    cout << e << endl;  
    e++;
    cout << e << endl<<endl;
    }

int main()
    {
    cout << "MAIN" << endl;
    int a,b,c,d;
    cout << "a= " << a << endl;
    cout << "b= " << b << endl;
    cout << "c= " << c << endl;
    cout << "d= " << d << endl<<endl;
    cout << "MY_FUNC" << endl;
    myFunction1();
    myFunction1();
    myFunction1();
    }

and obtained this output

MAIN
a= -1617852976
b= 32767
c= 0
d= 0

MY_FUNC
32675
32676

32676
32677

32677
32678

So, there are two things I really don't understand

1) In the main() function I'm creating 4 int variables (a,b,c,d) WITHOUT initializing them, so I expect them to assume a different value each time I run the code. Strange thing is, the first variable (a) is always different, while the others always assume the same values (b=32767, c=d=0)

2) The function output is even stranger to me. Again, I'm creating a variable e without initializing it, so the first time it assumes a random value (in the example, e=32675).....then, I increase it by one, so that it prints 32675 and 32676, and that sounds right.

But how come the second time I call the function, e keeps the previous value (32676)? I thought e was created each time I call myFunction1() and deleted at the end of the function, so that e assumed a different random value each time (since I don't initialize it). Why is the value of e stored even if the variable goes out of scope?

Carlo
  • 1,321
  • 12
  • 37
  • 1
    No result is unexpected when you have undefined behaviour. – chris Mar 01 '15 at 19:47
  • 1
    It's just undefined behavior to use uninitialized variables. – πάντα ῥεῖ Mar 01 '15 at 19:48
  • Reading uninitialized variables is undefined behavior, *anything* can happen including explosions and demons flying out of your nose. (except if that variable is a char, then it's merely unspecified and you don't get the demons/explosions). – tux3 Mar 01 '15 at 19:50
  • @tux3, Not `char`, only `unsigned char` (more properly, an unsigned narrow character type). – chris Mar 01 '15 at 19:56
  • @chris right, my bad. – tux3 Mar 01 '15 at 19:56
  • @dyp, Last I heard of it was [this](http://stackoverflow.com/questions/23415661/has-c-standard-changed-with-respect-to-the-use-of-indeterminate-values-and-und) :( – chris Mar 01 '15 at 19:57
  • @chris Ah, sorry, it's not in [conv.lval] but [dcl.init] (now?) – dyp Mar 01 '15 at 19:58
  • @dyp the resolution seems to say it still holds, with an extra condition "ACCEPTED: Further restrict loads of uninitialized unsigned char such that the value can only be stored, and the result of storing it is to make the destination contain an indeterminate value. " – tux3 Mar 01 '15 at 19:58

4 Answers4

2

Uninitialized primitive values are simply not defined. They can have any value.

D.Shawley
  • 58,213
  • 10
  • 98
  • 113
2

It is an undefined behavior. That's why it doesn't make any sense to analyze the behavior of this program.

kraskevich
  • 18,368
  • 4
  • 33
  • 45
2

In the main() function I'm creating 4 int variables (a,b,c,d) WITHOUT initializing them, so I expect them to assume a different value each time I run the code

This assumption is flawed. They may have a different value each time you run the code, but they may not. Anything could happen. The point of UB is that you should drop all your assumptions.

But how come the second time I call the function, e keeps the previous value (32676)? I thought e was created each time I call myFunction1() and deleted at the end of the function, so that e assumed a different random value each time (since I don't initialize it)

It does. If you replace "random" for the more correct "arbitrary", then the results you're seeing fit that pattern just fine.

It's just pure luck, and comes down to the state you're leaving unclaimed memory in at each stage of your program's execution.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

A good way to help you understand this is to explain in terms of memory allocation.

When you run a program, a certain amount of memory that is not used is assigned to your variable.

Computers are lazy, the best way to delete a data is to forget where it is stored. When you assign a chunk of memory to a variable, you are telling the computer to remember where that certain data belongs to.

If it so happens that it was used prior to you assigning the memory to the variable, it will simply read (let's say 4 bytes for a common machine) and get the data from that location.

Hope that this helps =)

Jay Wai Tan
  • 169
  • 7