clanguage-lawyeraddressoflvalue-to-rvalueregister-keyword

Using uninitialized variable without invoking undefined behavior


From 6.3.2.1 (emphasis mine)

If the lvalue designates an object of automatic storage duration that could have been declared with the register storage class (never had its address taken), and that object is uninitialized (not declared with an initializer and no assignment to it has been performed prior to use), the behavior is undefined.

That means, that if the automatic object could not be declared with the register storage class (have it's address taken):

int x; 

printf("just a dummy pointer print %p", &x);  //taking the address to break 6.3.2.1 UB condition

if (x == 2)
{
    print("x uninitialized value: %d", x);
} 

Than according to 6.3.2.1 there is no undefined behavior in if (x == 2) where I use the value of the uninitialized object. If that is true, and there is no UB here, than what is the defined behaviour? what should I expect in x according to the standard?


Solution

  • In this case, because x has had it's address taken, the behavior is not strictly undefined. The value of x at this point is indeterminate. This means the value is either a trap representation or unspecified.

    If x happens to contain a trap representation then the behavior is undefined, otherwise the value is unspecified which means that any valid value could be printed.

    Also, most systems you're likely to come across don't have any padding bits in integer types, meaning there are no trap representations on that implementation and the value will always be unspecified.

    The relevant passages from the C standard:

    Section 3.19:

    3.19.2

    1 indeterminate value either an unspecified value or a trap representation

    3.19.3

    1 unspecified value valid value of the relevant type where this International Standard imposes no requirements on which value is chosen in any instance

    2 NOTE An unspecified value cannot be a trap representation.

    3.19.4

    1 trap representation an object representation that need not represent a value of the object type

    Section 6.7.9p10:

    If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.