Determining Undefined Behavior for Dereferencing Uninitialized Pointers in the C Standard
The question posed seeks to understand the specific section in the C standard that deems dereferencing an uninitialized pointer as undefined behavior. While previous references to 5.3.1/1 and 3.7.3.2/4 provide relevant information, they do not explicitly state the undefined behavior related to using uninitialized pointers.
The answer lies within Section 4.1, which addresses lvalue conversion to rvalue (emphasis added):
"An lvalue (3.10) of a non-function, non-array type T can be converted to an rvalue. If T is an incomplete type, a program that necessitates this conversion is ill-formed. If the object to which the lvalue refers is not an object of type T and is not an object of a type derived from T, or if the object is uninitialized, a program that necessitates this conversion has undefined behavior."
This section clearly states that attempting to use an lvalue (such as a pointer) that refers to an uninitialized object will result in undefined behavior. Therefore, the code sample provided:
int* ptr; *ptr = 0;
is indeed exhibiting undefined behavior because the pointer ptr is used without initialization.
For进一步深入了解 this topic, searching the C standard for "uninitialized" will reveal additional sections that touch upon the undefined behavior associated with uninitialized objects.
The above is the detailed content of Where in the C Standard is Dereferencing an Uninitialized Pointer Defined as Undefined Behavior?. For more information, please follow other related articles on the PHP Chinese website!