1

I found a few topics that might have related, but only in C or C#, not C++. Since I'm so new to C++, I wasn't sure if those questions/answers applied. But I noticed that when I initialized a variable in my main method, if I tried to cout its value, it would return a memory location and not the variable's value.

The code I wrote (comments in the code expressing what I experienced):

#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

bool gameOver = true;
//int heights [5];
vector <int> heights;
int count; // This returns a number

int main()
{
    int height;
    int inch;
    int total;
    //int count;  this returns a memory location (I think?)
    double average;

    while (gameOver)
    {
        cout << "Enter height in inches, or 'end' to quit: ";
        cin >> height;
        heights.push_back(height);
        count++;
        cout << count; 
        if (count == 5)
        {
            gameOver = false;
        }
    }
    for (int i = 0; i < sizeof(heights); i++)
    {
        total += heights[i];
    }
    average = (total / count);

    return 0;
}

So, what's the difference between where it's initialized from?

TJMcButters
  • 43
  • 1
  • 5
  • 5
    Does this answer your question? [What happens to uninitialized variables? C++](https://stackoverflow.com/questions/40987901/what-happens-to-uninitialized-variables-c) – Mikel Rychliski Apr 13 '20 at 00:52
  • @MikelRychliski - It's helpful, but the variable has a value at the time that I was running cout on it. When I initialized it within the main method, it gave a value of 460123 or something like that. But when I moved it outside the main method, it returned what I expected it to return. – TJMcButters Apr 13 '20 at 00:55
  • 3
    @TJMcButters No, the variable did not have a value. When declared in `main`, it starts indeterminate, which gives undefined behavior when you try to use it (in the line `count++;`). – JaMiT Apr 13 '20 at 00:57
  • @TJMcButters: When declaring a built-in type (int, double, etc.) **inside a function**, you must provide the initial value (or update the value before you ever access it). Global variables are always value initialized (to zero for int). – Mikel Rychliski Apr 13 '20 at 00:59
  • @JaMiT Oh, gotcha. I can work with that. Self-learning C++ is difficult - not sure I would have picked up on that on my own. Thanks for the clarification! – TJMcButters Apr 13 '20 at 01:15
  • @MikelRychliski - So what's up with the value that I receive when I try to cout the variable? Is that actually the memory location? Or is it just some random number? Or is it the max value of type int? Thanks for the clarification, it's very helpful. – TJMcButters Apr 13 '20 at 01:16
  • @TJMcButters: The language spec give the compiler freedom to do anything when accessing the variable. In practice, all variables inside functions are allocated on one global stack, which gets reused as functions are called and returned. The number you are seeing is not (directly) an address, but instead some "left-over" value. The memory location of "count" was used to hold some other value in the past (pointer, float, int?) and you are seeing that. This can make things tricky because you can get lucky. You could "happen" to get zero for a long time, until you make some unrelated change. – Mikel Rychliski Apr 13 '20 at 01:28
  • @TJMcButters While most memory addresses are large integers, relatively few large integers are memory addresses. When you see a large integer output when you are expecting a small one, you should think "garbage value". If you instead think "memory addresses", you'll just confuse yourself. – JaMiT Apr 13 '20 at 01:52

0 Answers0