1

When does the compiler auto initialize the arrays to it's default datatype?

I know that in this example the arrays will be initialized, except the fact that i was expecting (reading here) \0 value for char instead in my console there were spaces (at least this is what i think because there is nothing).

#define MAX_LENGTH 25

// Array global
int vector[MAX_LENGTH];
char vector_char[MAX_LENGTH];

int main()
{
    //Print int array
    for(int i = 0;i < MAX_LENGTH; i++)
    {
        cout << vector[i] << " ";                        // All outputs 0
    }
    cout << endl;

    //Print char array and output text to see that there is 
    //Something printed
    cout << "start";
    for(int i = 0;i < MAX_LENGTH; i++)
    {
        cout << vector_char[i] << " ";                     //All outputs...
    }
    cout <<"end"<< endl;
    return 0;
}

Other two situations that I've encountered is when an array is internal to a function (where the values will be "garbage" or "impredictibile" E.g:

int main()
{
    int internal_vector[MAX_LENGTH];

    for(int i = 0;i < MAX_LENGTH; i++)
    {
        cout << internal_vector[i] << " ";
    }
    cout << endl;
    return 0;
}

and another one when the array is a member of a class (if you try anything you will have the surprise that array will contain garbage values:

class MyClass
{
private:
    int array[100];
...
}

So, when is an array automatically initialized to it's default datatype and when should i make my own function to initialize each element?

Cătălina Sîrbu
  • 1,253
  • 9
  • 30
  • Wow: "int vector[MAX_LENGTH]; char vector_char[MAX_LENGTH];" Please do not use plain C-Style arrays in C++. Never. Use ````std::vector```` or other STL container. You even name your C-Style arrays like this . . . – A M Nov 16 '19 at 15:43
  • What exactly led you to believe that `char c=0; std::cout << c` will produce "\0" as the output? This is not true. Your char vector is also zero-initialized, but the char 0 is simply not shown, so all you see are spaces. – Sam Varshavchik Nov 16 '19 at 15:44
  • @SamVarshavchik i though that the char default value it is `\0` therefore declaring it outside any function this would be the value – Cătălina Sîrbu Nov 16 '19 at 15:45
  • @ArminMontigny a link for me to better understand what are you talking about would help me – Cătălina Sîrbu Nov 16 '19 at 15:46
  • I was very clear in my comment. Read it again. If you inspect your `char` vector's value in a debugger, you will be surprised to learn that they are, indeed, `\0`s. What you're missing is that a `\0` character will output two characters, a backslash and a digit 0, to `std::cout`. Wherever you read that from, that source was incorrect. – Sam Varshavchik Nov 16 '19 at 15:48
  • If I remember correctly an array (or to be precise a C-style array) is default initialized exactly if it has static storage duration, i.e. if it is declared at namespace scope or with the identifier `static`. The character `\0` is the interpretation of the `char` with value `0`. `\0` is an escape sequence for this character, since it has no printable representation, it has no width. – n314159 Nov 16 '19 at 15:48
  • Regarding @ArminMontigny comment: The C++-way to use an array with compile-time size is [`std::array`](https://en.cppreference.com/w/cpp/container/array) which is basically a wrapper around a C-style array. It has the advantages, that it knows its own size, you can use `.at()` to index with bounds checking and it does not decay to a pointer. All of this with basically no overhead at runtime. [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) is the C++ way to use dynamic arrays, it can even change its size during runtime. – n314159 Nov 16 '19 at 15:55

1 Answers1

2

Global objects have static storage duration, they will be zero-initialized from the beginning,

For every named variable with static or thread-local storage duration that is not subject to constant initialization (since C++14), before any other initialization.

If T is a scalar type, the object's initial value is the integral constant zero explicitly converted to T.

If T is array type, each element is zero-initialized

That means all the elements of vector will be initialized to 0, all the elements of vector_char will be initialized to '\0'.

On the other hand, all the elements of internal_vector will be initialized to indeterminate values in default initialization.

if T is an array type, every element of the array is default-initialized;

otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.

Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-local objects get zero initialized)

Note that it's a little complex for array, whose storage duration depends on how the instance is declared.

Community
  • 1
  • 1
songyuanyao
  • 169,198
  • 16
  • 310
  • 405