Home > Backend Development > C++ > Where in the C Standard is Dereferencing an Uninitialized Pointer Defined as Undefined Behavior?

Where in the C Standard is Dereferencing an Uninitialized Pointer Defined as Undefined Behavior?

Mary-Kate Olsen
Release: 2024-12-16 20:59:11
Original
770 people have browsed it

Where in the C   Standard is Dereferencing an Uninitialized Pointer Defined as Undefined Behavior?

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;
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template