Confusing Output for Values Array

I'm really confused by the output I'm getting for these two bits of code in Visual Studio. Please Note that the output I receive in visual Studio is completely different from that I receive in the online compiler cpp.sh.

In the first bit of code, one element of the output in Visual Studio is always the equivalent of the value I set equal to "const int SIZE".
The Output I receive for the below is:
100
100
100
100
100
3
-size
address
address
Notice the 3 which equals values[5].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
using namespace std;

int main()
{
	const int SIZE = 3;  // Constant for the array size
	int values[SIZE];    // An array of 3 integers
	int count;           // Loop counter variable

	// Attempt to store five numbers in the three-element array.
	cout << "Store 5 numbers in a 3 element array!\n";
	for (count = 0; count < 5; count++)
		values[count] = 100;

	// If the program is still running, display the numbers.
	cout << "If you see this message, it means the program\n";
	cout << "has not crashed! Here are the numbers:\n";
	for (count = 0; count < 9; count++)
		cout << values[count] << endl;
	
	cin.get();
	return 0;
}




In the following I isolate values[5] by changing "cout << values[count] << endl;" to "cout << values[5] << endl;" I get 3 all the way through, but I don't understand why? I haven't specifically set values[5] to equal anything so why is it always pumping out the value of my const int size in Visual Studio specifically? Secondly, if this can actually be answered, why are different compilers giving me completely different output values? [/b]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
using namespace std;

int main()
{
	const int SIZE = 3;  // Constant for the array size
	int values[SIZE];    // An array of 3 integers
	int count;           // Loop counter variable

	// Attempt to store five numbers in the three-element array.
	cout << "Store 5 numbers in a 3 element array!\n";
	for (count = 0; count < 5; count++)
		values[count] = 100;

	// If the program is still running, display the numbers.
	cout << "If you see this message, it means the program\n";
	cout << "has not crashed! Here are the numbers:\n";
	for (count = 0; count < 9; count++)
		cout << values[5] << endl;
	
	cin.get();
	return 0;
}





Thanks for the help guys





Last edited on
I haven't specifically set values[5] to equal anything
There is a train station across the road. Why? I didn' build it.

Everything in your program, be it data or code, has to be stored somewhere in memory. An you can access it if you know where is it stored. When you breaking language rules and accessing random memory, you get values stored there (assuming something bas as segmentation fault or UB-driven optimisations will not happen).

why is it always pumping out the value of my const int size in Visual Studio specifically?
Because this compiler, with these preferences, happens to place SIZE variable there (and I suppose that you are compiling without optimisations. Enable /Ox and it should disappear).

why are different compilers giving me completely different output values?
Because they are compiling differently, arranging values in different places and emitting different code.
Last edited on
Ah. Wow. I wasn't aware that there were optimization options available in compilers. What I was half-expecting was that the specific integer output would be "0" as opposed to the equivalent of the const int, but now that I realize that this is essentially a random value being output due to unconfigured options, I'll do more research.

Thansk so much MiiNiPaa. I appreciaete it.
Last edited on
Topic archived. No new replies allowed.