2

Consider the following, you have two sets of arrays - one of which has some set of integers stored with values, you can put these in an array and cout the value in the array with ease. However when you have an array with empty integers within it - it does not seem to be able to "store" the values to the integer value - why?

Here is an example:

int empty1;
int empty2;
int contain1 = 100;
int contain2 = 200;
int container [2] = { contain1, contain2 };
int empty_container [2] = { empty1, empty2 };
int i = 0;
while ( i != 2 ) {
    empty_container[i] = i;
    cout << endl << "empty_container[i]: " << empty_container[i] << endl
    << "empty1: " << empty1 << endl << "empty2: " << empty2 << endl;
    cout << "i: " << i << endl;
    cout << "container[i]: " << container[i] << endl;
    cout << "contain1: " << contain1 << endl;
    cout << "contain2: " << contain2 << endl;
    i++;
}

Output:

empty_container[i]: 0
empty1: 4197341
empty2: 0
i: 0
container[i]: 100
contain1: 100
contain2: 200

empty_container[i]: 1
empty1: 4197341
empty2: 0
i: 1
container[i]: 200
contain1: 100
contain2: 200

Notice how empty1 all of a sudden has the value "4197341" while empty2 is 0, what is going on here?

UPD(from comments):

I mean like declaring an integer before and then set it through an array - is this not possible with C++?

awesoon
  • 32,469
  • 11
  • 74
  • 99
dusz
  • 913
  • 1
  • 9
  • 15
  • Empty 1 is what we call garbage. The integer was not initialized, so your IDE (i.e. Visual Studio) assigned a random (i.e. garbage) value as a placeholder. – MrPickle5 May 31 '13 at 02:24
  • 1
    *"empty integers"* is not a thing. At least not if you are talking about the standard integer types. – Benjamin Lindley May 31 '13 at 02:25
  • Sorry I mean like declaring an integer before and then set it through an array - is this not possible with C++? – dusz May 31 '13 at 02:26
  • 4
    Welcome to the most wonderful land of ***undefined behavior***! Please enjoy your stay. – Mark Garcia May 31 '13 at 02:27
  • 1
    dusz, did you mean to overwrite `empty_container[i] = i` right before you print it? – Robert Martin May 31 '13 at 02:29
  • Read this on uninitialized variables: http://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value – Shafik Yaghmour May 31 '13 at 02:32
  • Yes, it was a test to see if I could set the value of i to empty_container[0] and 1, which here was supposed to be `empty2` and `empty2` – dusz May 31 '13 at 02:32

2 Answers2

3

Since you have never initialized or assign values to empty1 and empty2. empty1 and empty2 do not have global or static storage, they will not be initialized implicitly. Both of them contain garbage values.

You have undefined behavior. You can see that this compiler gives you different results on values of empty1 and empty2: Local variable not initialized Demo

Always remember to initialize variables before using it.

EDIT:

I mean like declaring an integer before and then set it through an array - is this not possible with C++?

IMHO, you cannot do that. You can initialize array elements with given initialized variables. However, what you did:

 int empty_container [2] = { empty1, empty2 };

will not assign values to empty1 and empty2 with values in that array since the array itself is not initialized.

However, you may try the following:

empty_container[2] = {10}; //initialize array at first
empty1 = empty_container[0];
empty2 = empty_container[1]; //this will give you empty1 10 and empty2 0

See another live demo here: Array and Variable initialization demo

taocp
  • 23,276
  • 10
  • 49
  • 62
2

In here

...
int i = 0;
while ( i != 2 ) {
    empty_container[i] = i;  // <- HERE
    cout << endl << "empty_container[i]: " << empty_container[i] << endl
...

you must have assumed that doing empty_container[i] = i would also affect empty1 or empty2. It seems that your thinking it wrong.

In C++, doing int empty_container [2] = { empty1, empty2 }; copies the values of empty1 and empty2 into the respective elements of empty_container. This means that even if they have the same values, they do not refer to the same memory location.

In order to also modify empty1 and empty2 when you modify empty_container, you must modify empty_container and change it to an array of pointers, or some similar methods to refer to some object.

The code:

#include <iostream>

using namespace std;


int main() {
    int empty1;
    int empty2;
    int contain1 = 100;
    int contain2 = 200;
    int container [2] = { contain1, contain2 };
    int* empty_container [2] = { &empty1, &empty2 };
    // ^ Notice this
    int i = 0;
    while ( i != 2 ) {
        *(empty_container[i]) = i;
    //  ^ Also this
        cout << endl << "empty_container[i]: " << empty_container[i] << endl
        << "empty1: " << empty1 << endl << "empty2: " << empty2 << endl;
        cout << "i: " << i << endl;
        cout << "container[i]: " << container[i] << endl;
        cout << "contain1: " << contain1 << endl;
        cout << "contain2: " << contain2 << endl;
        i++;
    }
}

Notice that empty1 becomes 0 in the first iteration, and empty2 becomes 1 in the second iteration.

Mark Garcia
  • 17,424
  • 4
  • 58
  • 94
  • `*(empty_container[i]) = i;` - parentheses are unnecessary here, `[]` has higher priority than dereferenece operator. – awesoon May 31 '13 at 02:50
  • 1
    @soon It maybe is unnecessary for the compiler, but it isn't always that clear when humans are reading those kind of code. – Mark Garcia May 31 '13 at 02:52
  • Thank you for this, it does work. Now I have some more stuff to read up on :) – dusz May 31 '13 at 02:54
  • @dusz *"Now I have some more stuff to read up on"*. Very good! Coupling that with experience is all it takes to be a good programmer. Read then apply! – Mark Garcia May 31 '13 at 03:00