C Language Undefined behavior Reading an uninitialized object that is not backed by memory

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

C11

Reading an object will cause undefined behavior, if the object is1:

  • uninitialized
  • defined with automatic storage duration
  • it's address is never taken

The variable a in the below example satisfies all those conditions:

void Function( void )
{
    int a;
    int b = a;
} 

1 (Quoted from: ISO:IEC 9899:201X 6.3.2.1 Lvalues, arrays, and function designators 2)
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.



Got any C Language Question?