-1
int main() {
 int i;
 i++; // 1
}

Can I be sure that I always get the value "1" after using increment? Because I often hear that a variable can contain anything before initializing. So it's illegal? Or increment always starts from "0" if the variable is a non-initialized?

ratojakuf
  • 708
  • 1
  • 11
  • 21

5 Answers5

7

If no initializer is specified for an object, the object is default-initialized. When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value, and if no initialization is performed for the object, that object retains an indeterminate value until that value is replaced

Default-initialization for integers is a no-op, meaning no initialization is performed. That means i in your example has an indeterminate value. Using the object as if it had a value results in undefined behavior:

If an indeterminate value is produced by an evaluation, the behavior is undefined [..]

David G
  • 94,763
  • 41
  • 167
  • 253
5

It is not necessarily undefined behaviour, because it depends very much upon the version of the language — things are much clearer in C++14, however. Either way though, there are no guarantees you will get the value 1.

  • int i; in this context declares i with automatic storage duration, and with default initialization. For int, default initialization leaves the variable with indeterminate value.
  • In C, indeterminate value is defined in the standard: i will have some value, or a trap representation.
  • The increment operation adds one to the value of i, which was indeterminate. If it happened to be a trap representation, this could raise a signal. (On your typical PC hardware and C or C++ compiler, there won't be any trap representations for int.)
  • Until the C++14 draft, C++ left open what was meant by indeterminate; it was used, but not defined. Once might assume that it meant the same thing as in the C standard, but frankly there are no guarantees here.
  • With C++14, this has been nailed down, and except for a limited number of operations on narrow characters, doing anything with indeterminate values leads to undefined behaviour.

In short: if it were C, it's not undefined behaviour, but no promises that it will be 1; if it were pre-C++14 C++, it's not properly specified in the standard, but at the very least, there are no promises that it will be 1; and in C++14 it is specified that this is undefined behaviour.

There's an interesting and informative discussion of what indeterminate means in C++ on stackoverflow.

Community
  • 1
  • 1
halfflat
  • 1,584
  • 8
  • 11
4

no it will have undefined behaviour.

i++ doesn't start from 0, it simply increment by one based on the previous value, in this case i has indeterminate value.

see this question for more detail

Community
  • 1
  • 1
swang
  • 5,157
  • 5
  • 33
  • 55
  • Thanks for the link! I was unaware of the could-have-been-register caveat for undefined behaviour in C. – halfflat Feb 17 '15 at 00:05
1

Only global variables are zero initialized. This one is defined inside function and it is not static so it is local. It means i contains random garbage after it is created.

Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
1

No practical use in the real world. This will give you undefined behaviour. Each time you will get a different value.

Arun Kumar
  • 151
  • 3
  • 10