2

Pedantically speaking, is x initialized in the following code or not?

int main()
{
    int x;
}

There are some paragraphs about it in 8.5 Initializers [dcl.init] (for C++11) but not backed by any examples.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625

3 Answers3

5

It is formally default-initialized, which means for ints, that no initialization is performed.

[dcl.init]/12 (N3797)

If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value

[dcl.init]/7

To default-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type, the default constructor for T is called [...];

  • if T is an array type, each element is default-initialized;

  • otherwise, no initialization is performed.

dyp
  • 38,334
  • 13
  • 112
  • 177
  • 3
    So, it is initialized, but that action consists of "no initialization is performed". Either that, or it is default-initialized, but default-initialized is not a kind of initialized in this case. Or, the term "initialized" has no formal meaning in the standard, while "default-initialized" does as does "initializaion is performed". Gah. – Yakk - Adam Nevraumont Sep 23 '14 at 15:50
  • @Yakk Exactly. Reminds me of complete types that cannot be completed. – dyp Sep 23 '14 at 15:53
  • @Yakk: It stays indeterminate is thus factually correct and the standard conformant wording for its state. So, using it if it's auto-storage-class is UB according to 4.1 lvalue-to-rvalue-conversion [conv.lval]. – Deduplicator Sep 23 '14 at 15:53
  • @Deduplicator thankfully [in C++14](http://stackoverflow.com/q/23415661/1708801) we don't need to rely on the l-to-r conversion to know this undefined behavior. – Shafik Yaghmour Sep 23 '14 at 16:05
3

No, it isn't. According to standard, x is default-initialized ([dcl.init]/6):

To default-initialize an object of type T means:

— if T is a (possibly cv-qualified) class type [...]

— if T is an array type [...]

otherwise, no initialization was performed.

x is therefore uninitialized since no initialization is performed.
Hence the object has indeterminate value ([dcl.init]/11):

If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value.

Moreover, if we were to access it's stored, indeterminate value - in other words, perform an lvalue-to-rvalue conversion on it - we would induce undefined behavior ([conv.lval]/1]):

If the object to which the glvalue refers is [..], or if the object is uninitialized, a program that necessitates this conversion has undefined behavior.

Community
  • 1
  • 1
Columbo
  • 60,038
  • 8
  • 155
  • 203
2

The way I understand it is that the place in memory for the variable x is reserved, but not set to a value (un-initialized). Because it is un-initialized, any old values there will be considered as 'garbage' int.