Is incrementing an uninitialized variable legal?

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

Can I be sure that after using the increment I always get the value "1"? Because I often hear that a variable can contain anything before initialization. So is it illegal? Or does an increment always start with "0" if the variable is not initialized?

-3
source share
5 answers

If no initializer is specified for the object, the object is initialized by default. When storing an object with automatic or dynamic storage time, the object has an undefined value, and if the object is not initialized, this object retains an undefined value until this value is replaced

The default initialization for integers is no-op, which means that no initialization is performed. This means that i in your example is undefined. Using an object as if it matters leads to undefined behavior:

If an undefined value is created by an estimate, the behavior is undefined [..]

+4
source

No, it will have undefined behavior.

i ++ does not start at 0, it just increments by one based on the previous value, in this case I have an undefined value.

see t his question for more details

+3
source

This is not necessarily undefined behavior, because it is very dependent on the language version - however, in C ++ 14, everything is much clearer. In any case, there is no guarantee that you will receive a value of 1.

  • int i; in this context, declares i with automatic storage time and default initialization. For int initialization by default leaves a variable with an undefined value.
  • In C, an undefined value is defined in the standard: i will have some value or representation of a trap.
  • The increment operation adds a value to the value of i , which was undefined. If it was a trap view, it could raise a signal. (On your regular PC hardware and C or C ++ compiler, there will be no traps for int .)
  • While the C ++ 14 project, C ++ left open, which was implied by undefined; it was used but not defined. As soon as one can assume that this meant the same as in the C standard, but frankly, there are no guarantees.
  • With C ++ 14 this was nailed, and with the exception of a limited number of operations with narrow characters, doing something with undefined values ​​leads to undefined behavior.

In short: if it is C, this is not undefined behavior, but no promises that it will be 1; if it was pre-C ++ 14 C ++, it was not correctly specified in the standard, but at least there is no promises that it will be equal to 1; and C ++ 14 states that this behavior is undefined.

Here's an interesting and informative discussion of what indefinite means in C ++ in stackoverflow.

+3
source

Only global variables are initialized to zero. This function is defined inside the function and it is not static, therefore it is local. This means that i contains random garbage after it is created.

0
source

There is no practical use in the real world. This will give you undefined behavior. Each time you get a different value.

0
source

All Articles