2

I am not really sure how or why this works:

#include <stdio.h>
#include<stdlib.h>
#include <limits.h>
#include <iostream>
using namespace std;

int main() {

 int min = INT_MAX, min_index;

 cout << min <<"\n";
 cout << INT_MAX << "\n";
 cout << min_index <<"\n";


 system("pause");
 return 0;
}

There is actual output for min, INT_MAX, and min_index I know that INT_MAX is part of the limits.h and so min and INT_MAX are the same but min_index isn't given a value why does it display a value.

Alexander
  • 59,041
  • 12
  • 98
  • 151
HGI
  • 83
  • 8
  • 4
    `min_index` is an uninitialized variable, containing random garbage. Accessing its value exhibits undefined behavior. "Seems to work" is one possible manifestation of undefined behavior. – Igor Tandetnik Feb 17 '17 at 05:30
  • 1
    @HGI Instead of `<< "\n"`, you should use `endl` in most cases. http://stackoverflow.com/a/213977/3141234 – Alexander Feb 17 '17 at 05:38
  • @Alexander I'd say the opposite. Most times you don't need to flush the buffer and pay the associated cost. – juanchopanza Feb 17 '17 at 06:53
  • 1
    @jaunchopanza Given that OP is at a level where he doesn't yet understand garbage values, I dont think they're at a point to be concerned about the micro cost of extra flushing. It's much more important that their debugging print statements work properly, even if the program crashes before messages are flushed. – Alexander Feb 17 '17 at 07:00
  • Another example why using the highest compiler warning should be a must! – Peter VARGA Feb 17 '17 at 07:06

4 Answers4

4

C++ isn't a memory-safe language. There's no guarantee that a variable has been a assigned a value before it's used.

What you're seeing as the printed value of min_index, is the value in the memory of min_index that just so happened to be left from the last time the memory was set previously.

Reading from such an initialized variable is "undefined behaviour", in that each invocation of this program will likely lead to a different, unexpected result (depending on what was last using the memory that eventually got allocated for min_index).

Alexander
  • 59,041
  • 12
  • 98
  • 151
2

In a language like C and C++, when you define a variable, like:

int x;

a small block of memory is allocated to the variable. However, we have only declared the variable, and not initialized it, which means that the block of memory that has been allocated to the variable still contains some value that has been left over from previous programs and operations. That value is called a garbage value. This may lead to erroneous results in programs.

To avoid this, declare and initialize variables like this:

int x = 0;

Source: What do you mean by a garbage value in a variable?

See also: How does an uninitiliazed variable get a garbage value?

Community
  • 1
  • 1
Jomoos
  • 12,823
  • 10
  • 55
  • 92
0

Because $min_index is not initialized to a specified value but it is assigned to a memory location. If you print min_index without initializing it first, the values on that memory location will be interpreted as data of the type of min_index.

Therefore it is always recommended to initialize a variable, because you will never know, what the value of that memory location is.

Aeonos
  • 365
  • 1
  • 13
0

you cant initialize min= int_max. the value is showing due to garbage value

user49931
  • 7
  • 1
  • 2