Default initialization

Oct 14, 2019 at 12:29am
What is default initialization, when does it occur?
Would 'x' be considered as default initialized?

1
2
3
4
  int main(){
   int x;
    return 0;
}
Oct 14, 2019 at 1:24am
No.

Only class objects can default initialize, and that happens when you write a constructor taking no arguments to initialize the object, or if every member of that object is a class that default initializes.

1
2
3
4
5
6
7
8
9
10
11
12
struct does_not_default_initialize
{
  int x;          // does not default initialize
  std::string s;  // default initializes
};

struct default_initializes
{
  int x;
  std::string s;
  default_initializes(): x{0} { } // default ctor initializes x
};

Similarly, all global objects are zero-initialized, which is not quite the same thing. For integer types (char, int, pointers, etc) this is much the same, but for non-default-initialized class objects it is definitely not the same thing.

All local objects need initialization. So in your example, the x, a local object, is not initialized and contains random garbage.

You can (and should) explicitly initialize it:

1
2
3
4
int main()
{
  int x = 7;
}

Hope this helps.
Oct 14, 2019 at 2:36am
Thanks, but would the garbage value be considered the variable’s initial value? If not then what would it be considered as? Thanks.
Oct 14, 2019 at 3:17am
Would 'x' be considered as default initialized?
Well, yes. See the first sentence of the relevant reference page:
https://en.cppreference.com/w/cpp/language/default_initialization

In this case, the process of default-initialization does nothing, so x is default-initialized to an indeterminate value, which is garbage. Except in specific circumstances, a program that uses an indeterminate value has undefined behavior.

Your compiler cannot assume that the indeterminate value is benign. In fact, it may not represent an int at all. For this reason, you should think of x as "not-an-int", until you set it to something.

Note that C++'s initialization rules are very complicated. You don't need to know all the details to get serious work done.
Last edited on Oct 14, 2019 at 4:11am
Oct 14, 2019 at 4:06am
> What is default initialization?

a. For a POD class type or a built-in POD type like int, initialise to an indeterminate value (do nothing).
b. For a non-POD class type, call a default constructor which provides the initialised value.
c. For an array type, default initialise every element of the array.


> when does it occur?

a. A variable (with automatic, static, or thread-local storage duration) is declared with no initializer.
eg. void foo() { int x ; /*x is default initialised */ }

b. An object is created by a new-expression with no initializer
eg. void foo() { int* x = new int ; /*the object of type int is default initialised */ }

c. when a base class or a non-static data member of a class is not explicitly initialised in a constructor initializer list or via a default-member-initialiser.
eg.
1
2
3
struct A { int x ; std::string y ; };

void foo() { A a ; /*members x and y are default initialised */ }


Oct 14, 2019 at 3:22pm
Thanks for all the replies, I came across this:
“Primitive types remain uninitialized”
Is this true, I’ve also noticed some of the replies say yes and no, I’m confused.
Oct 14, 2019 at 4:08pm
I’ve also noticed some of the replies say yes and no,

it's "yes" or "no" depending on how precise you are with your language. There is an initial value, but you can't tell what it is. Formally this is described as "default-initialized to an indeterminate value", informally "uninitialized".

(if you want to be absolutely precise, this is a combination of the rules from http://eel.is/c++draft/dcl.init#7.3 and http://eel.is/c++draft/basic.indet )
Last edited on Oct 14, 2019 at 4:10pm
Oct 14, 2019 at 4:09pm
To default-initialize an object of type T means:

If T is a (possibly cv-qualified) class type, constructors are considered. ....

If T is an array type, each element is default-initialized.

Otherwise, no initialization is performed.

IS - https://eel.is/c++draft/dcl.init#7


Default initialisation of primitive types like int comes under the 'otherwise' clause:
ie. "no initialization is performed."
Oct 14, 2019 at 4:17pm
Global or static local POD types are initialised to 0,0.0,'\0',NULL (as appropriate to their type).

1
2
3
4
int global;  // will be 0
int main ( ) {
    static double local;  // will be 0.0
}
Oct 14, 2019 at 5:00pm
Default initialisation of primitive types like int comes under the 'otherwise' clause:
ie. "no initialization is performed."


What about global variables what clause would they come under?
Oct 14, 2019 at 5:22pm
Zero-initialisation: global variables have static storage duration.

static and thread-local variables that aren't constant-initialized are zero-initialized before any other initialization takes place. If the definition of a non-class non-local variable has no initializer, then default initialization does nothing, leaving the result of the earlier zero-initialization unmodified.
https://en.cppreference.com/w/cpp/language/zero_initialization
Oct 14, 2019 at 5:27pm
What about global variables what clause would they come under?

http://eel.is/c++draft/basic.stc#static-1 "all variables that ... are not local have static storage duration, then
http://eel.is/c++draft/basic.start#static-2.sentence-2 "a variable with static storage duration ... is zero-initialized", then
http://eel.is/c++draft/dcl.init#6.1 "To zero-initialize ... means ... the object is initialized to the value obtained by converting the integer literal 0 (zero) to T;"

(or, indeed, follow cppreference quoted above, it's simpler than the actual spec)
Last edited on Oct 14, 2019 at 5:30pm
Topic archived. No new replies allowed.