2

Question 1: My friend asked me a question, and I wanted to verify that my understanding was correct. If I declare an array of a certain size, but do not specify the value of each element, then print it out, I get all 0s. But that is the compiler that is making them 0s correct? All I can expect from declaring an array of X size, is that it will allocate a contiguous block of that size correct? I should not count on them always being zero, and should manually initialize them myself.

Question 2: I have read on a message board that newer c++ lets you declare an array that has a user defined size. That does not make sense to me, I've always thought it needed to be known at compile time. What's the deal here? Is this syntactic sugar? Whats going on under the hood?

#include <iostream>

using namespace std;

void question2()
{
    int userInput;
    cin >> userInput;

    int anotherArray[userInput];
}

int main()
{
    int array[5];

    for (int i = 0; i < 5; i++) {
        cout << array[i] << endl;
    }
    cout << endl;

    question2();
}

I would expect jibberish output from main and an error saying something like, "userInput not const" but I am getting 0 0 0 0 0 and user input, and Process exited after 2.857 seconds with return value 0

Swordfish
  • 12,971
  • 3
  • 21
  • 43

2 Answers2

6

If I declare an array of a certain size, but do not specify the value of each element, then print it out, I get all 0s. But that is the compiler that is making them 0s correct?

Correct.

All I can expect from declaring an array of X size, is that it will allocate a contiguous block of that size correct?

Correct.

I should not count on them always being zero, and should manually initialize them myself.

Correct.

You're probably seeing the compiler do extra work as a favour because it's a debug build. In general the values are unspecified and reading them has undefined behaviour (and you can expect to get any old rubbish as a result; that's not just academic).


I have read on a message board that newer c++ lets you declare an array that has a user defined size. That does not make sense to me,

Good reason to ignore message boards.

I've always thought it needed to be known at compile time.

You have always been right.

What's the deal here? Is this syntactic sugar?

C has "variable length arrays". GCC permits them as an extension in C++; ignore it.

Whats going on under the hood?

More complicated stack magic.


I would expect jibberish output from main and an error saying something like, "userInput not const" but I am getting 0 0 0 0 0 and user input, and Process exited after 2.857 seconds with return value 0

Zero is a valid gibberish result, and VLAs are permitted by your compiler (as discussed above); if you go into "proper" C++ mode (say, by passing -Werror=vla to GCC) it'll be more strict. I recommend that.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
3

I should not count on them always being zero, and should manually initialize them myself.

For automatic variables (not just arrays), yes. Static variables — notably those defined at file scope — are set to zero by the compiler at compile time.

To initialize an array to zeros, you can take advantage of the fact that partially initialized anythings have non-initialized members set to zero. So you can initialize it with no members, and the "others" set to zero for free:

int array[5] = {};
James K. Lowden
  • 7,574
  • 1
  • 16
  • 31