2
int main()
{    
    int a;
    cout << a;
    return 0;
}

I am wondering why the value 0 is being output. I thought if a variable is uninitialized, it would output a garbage value.

However, I also remember hearing that the default value of an integer is 0 so I am a bit confused.

Thanks

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
csguy
  • 1,354
  • 2
  • 17
  • 37
  • 8
    `0` is just as good a garbage value as any other value. You have _undefined behaviour_. That means there is no guarantee on what your program will do. – paddy Dec 06 '16 at 04:36
  • @paddy well getting 0 every time out of all the countless possible values is like winning powerball every time you play, don't you think a little fishy? :) – zar Dec 06 '16 at 05:00
  • @zar: Not really. Some numbers are more common than others in computer memory, and 0 is the most common of all. – Benjamin Lindley Dec 06 '16 at 05:06
  • I think that's a debug load playing silly buggers and hiding potential mistakes in the name of helping out. – user4581301 Dec 06 '16 at 05:06
  • @zar No, not fishy at all. There are any number of reasons why that memory could be zero on that particular OS. The compiler deciding to specifically initialize a chunk of the stack to zero is one possible reason. – paddy Dec 06 '16 at 05:08
  • 3
    Don't assume that _undefined behavior_ means that the results are _random_ – jdigital Dec 06 '16 at 05:19

3 Answers3

6

The default behavior of an uninitialized function scope (i.e., local) integer in C++ is for it to be indeterminate, which is fine; however if that value is used before it is defined it introduces undefined behavior, and anything could happen - demons could fly out of your nose.

This page on cppreference provides examples of default integer behavior.

On the other hand, all non-local, thread-local variables, not just integers, are zero initialized. But this case wasn't included in your original example.

(Side note: It is generally considered good practice to simply initialize variables anyway and avoid potential hazards altogether... Especially in the form of global variables. )

There are exceptions to best practice using global variables in rare special cases, such as some embedded systems; which initialize values based off of sensor readings on startup, or during their initial loop iteration... And need to retain a value after the scope of their loop ends.

Community
  • 1
  • 1
NonCreature0714
  • 5,744
  • 10
  • 30
  • 52
  • does this mean that when 0 is being output, that is still an undefined behavior. Also what are default values and when do you see them in play? – csguy Dec 06 '16 at 04:58
  • 1
    Yes, 0 as output is still undefined behavior, even if it is desired behavior. And there are no such things as default values. – NonCreature0714 Dec 06 '16 at 05:00
  • With your help (ty) and after reading around a bit more, i am concluding that only global variables are set to a default value when uninitialized, but local variables will contain an arbitrary value when uninitialized. Does this sound right? – csguy Dec 06 '16 at 05:06
  • @ec043 That isn't *quite* correct, but close, [zero initialization](http://en.cppreference.com/w/cpp/language/zero_initialization) on all non-local (i.e., "global"), thread local variables does take place. – NonCreature0714 Dec 06 '16 at 05:26
  • @ec043 thanks kindly for accepting my answer, hope this has helped you! – NonCreature0714 Dec 06 '16 at 05:40
2

I think you are not convinced with the answers/comments given, may be you can try the below code:

#include <iostream>
using namespace std;

int main(){

 int a,b,c,d,e,f,g,h,i,j;

 cout<<a<<endl;
 cout<<b<<endl;
 cout<<c<<endl;
 cout<<d<<endl;
 cout<<e<<endl;
 cout<<f<<endl;
 cout<<g<<endl;
 cout<<h<<endl;
 cout<<i<<endl;
 cout<<j<<endl;

 return 0;
}
D Untouchable
  • 332
  • 4
  • 9
  • that was helpful ty – csguy Dec 06 '16 at 05:14
  • interestingly enough, i tried with all bool values and all of them were 0. Does this mean that there are default values for some data types? I realize 0 is false and integer has many more possible values – csguy Dec 06 '16 at 05:16
  • 3
    Don't assume that _undefined behavior_ means that the results are _random_. A program that relies on undefined behavior can produce consistent results. In fact, this answer is misleading, as it implies that running the program will output different values for the uninitialized variables. It might. It might not. – jdigital Dec 06 '16 at 05:31
  • specifically what happens to uninitialized bool, char, int, and double? – csguy Dec 06 '16 at 05:35
  • True, it might, it might not. But in practical purposes, undefined behaviour here would relate to indeterminate. So with the example, there is a higher probability to get results other 0. it is not an answer but only an example to show that uninitialised variable is not 0 always. – D Untouchable Dec 06 '16 at 05:37
  • If the behavior were random, then you could talk about probability. But the behavior may not be random, For example, a compiler could choose to generate code that always sets uninitialized values to a specific value, say, for debugging purposes. You are making the assumption that variables will pick up indetermine values in memory. That's not what _undefined behavior_ means. – jdigital Dec 06 '16 at 05:44
  • 1
    And with the boolean question, may be you need to try more than what we tried for integer. Generally 1 integer is 4 bytes, and 1 boolean is 1 byte, if it relates to the same memory location then one 0 integer value relates to four 0 boolean values. – D Untouchable Dec 06 '16 at 05:44
  • I am not saying random, but here "Undefined Behaviour" does not mean you are going to burn your CPU, but it more relates to value is indeterminate, you can not determine the value. – D Untouchable Dec 06 '16 at 05:48
0

Well the reason being, a variable gets garbage value( a value unknown/senseless to program) is when someone runs a program, it gets loaded in some part of RAM. Now it all depends what values were previously set to certain location, may be some other program was there previously. It just happen the your program has loaded into a that location where it happens to be 0 value in RAM and that's what you are getting in return.

It quite possible that if restart your system and try running the same program then you might get garbage value.

Above statements are valid for variables which doesn't get initialized by the compiler.

MGpro
  • 33
  • 4
  • The indeterminate value of a variable is not guaranteed to be equal to the value that the memory had before. In fact, using indeterminate values at all causes undefined behavior in most cases. – walnut Apr 10 '20 at 15:08