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++?

7
  • 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. Commented May 31, 2013 at 2:24
  • 1
    "empty integers" is not a thing. At least not if you are talking about the standard integer types. Commented May 31, 2013 at 2:25
  • Sorry I mean like declaring an integer before and then set it through an array - is this not possible with C++? Commented May 31, 2013 at 2:26
  • 4
    Welcome to the most wonderful land of undefined behavior! Please enjoy your stay. Commented May 31, 2013 at 2:27
  • 1
    dusz, did you mean to overwrite empty_container[i] = i right before you print it? Commented May 31, 2013 at 2:29

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

I don't think it is what the OP asking about. Please look at updated question.
@soon Ah, did not notice the updated question. Will try to update the post now.
@taocp Sorry for being unclear - it is 04:45 am here. This was nonetheless useful information +1 :)
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.

4 Comments

*(empty_container[i]) = i; - parentheses are unnecessary here, [] has higher priority than dereferenece operator.
@soon It maybe is unnecessary for the compiler, but it isn't always that clear when humans are reading those kind of code.
Thank you for this, it does work. Now I have some more stuff to read up on :)
@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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.