Why do uninitialized objects of built-in type defined inside a function body have undefined value?
Asked Answered
N

3

10

Question:
Why do uninitialized objects of built-in type defined inside a function body have undefined value, while objects of built-in type defined outside of any function are initialized to 0 or ''?

Take this example:

#include <iostream>
using std::cout; using std::endl;

int ia[10];                   /* ia has global scope */

int main() 
{
    int ia2[10];              /* ia2 has block scope */

    for (const auto& i : ia)
        cout << i << " ";     /* Result: 0 0 0 0 0 0 0 0 0 0 */

    cout << endl;
    for (const auto& i : ia2)
        cout << i << " ";     /* Result: 1972896424 2686716 1972303058 8
                                 1972310414 1972310370 1076588592 0 0 0 */
    return 0;
}
Ningsia answered 5/11, 2014 at 13:24 Comment(2)
Consider the overhead for initialising the globals once vs. initialising the locals each time a function is called.Ulphiah
This is obviously not a duplicate (at least not of the linked question). OP doesn't ask if global memory is initialized but rather why, and also why local variables are not, both of which aren't answered in the linked question.Romy
V
18

Because one of general rules of C++ is that you don't pay for what you don't use.

Initializing global objects is relatively cheap because it happens only once at program startup. Initializing local variables would add overhead to every function call, which not everybody would like. So it was decided to make initialization of locals optional, the same way as in C language.

BTW If you want to initialize your array inside a function, you can write:

int ia2[10] = {0};

or in C++11:

int ia2[10]{};
Varia answered 5/11, 2014 at 13:37 Comment(1)
You might want to mention the stack (a common implementation of automatic storage), and how when retreating you can leave it in a random state, and when you grow it with the above rule you don't have to clear it.Duhamel
S
2

Well the answer to your question can be found here http://www.cplusplus.com/doc/tutorial/arrays/

Basically, if the variable is defined outside of the scope it is default zero initialized.

Static arrays, and those declared directly in a namespace (outside any function), are always initialized. If no explicit initializer is specified, all the elements are default-initialized (with zeroes, for fundamental types).

This is compared to variables defined inside of the scope which are not 0 defined.

By default, regular arrays of local scope (for example, those declared within a function) are left uninitialized. This means that none of its elements are set to any particular value; their contents are undetermined at the point the array is declared.

The above link actually explains it very well and I am in no way affiliated with them.

Sienna answered 5/11, 2014 at 13:49 Comment(1)
My apologies for the misunderstanding in what you were looking for. Further to the point outside of a function, in the global scope, C++ gives it a static storage duration. Inside of a function, or within a local scope, it has a automatic storage duration. en.cppreference.com/w/cpp/language/storage_durationSienna
N
2

Variable's defined outside the function are in global scope. Global variables are stored in the BSS part of the data segment of the executable. The are initialized to zero by default at program startup directly and done only once.

Variables defined in the function on the other hand are stored on the stack, initializing it zero every time will be a relatively expensive operation.

Look at this link for Memory Layout of a program: Memory Layout 1

Look at this link as well for good explanation: Memory Layout 2
Quoting from the above link

Global, external, and static variable are by default initialized to zero.

Niagara answered 30/11, 2014 at 4:31 Comment(1)
Not entirely true. Initialized variables with static duration (e.g. int somedata = 42;) will be in the .data segment, and are initialized at compile/link time, since the .data segment is simply mapped into memory.Ahner

© 2022 - 2024 — McMap. All rights reserved.