Home > Backend Development > C++ > Is `int x = x;` Undefined Behavior in C Due to Lvalue-to-Rvalue Conversion?

Is `int x = x;` Undefined Behavior in C Due to Lvalue-to-Rvalue Conversion?

Barbara Streisand
Release: 2024-12-28 21:31:17
Original
179 people have browsed it

Is `int x = x;` Undefined Behavior in C   Due to Lvalue-to-Rvalue Conversion?

Initialization and lvalue-to-rvalue conversion

The provided code snippet raises a commonly debated issue in C . Initializing a variable with itself, as in int x = x;, involves an lvalue-to-rvalue conversion. The question arises whether this conversion leads to undefined behavior (UB) considering that the right-hand 'x' is an uninitialized value, and lvalue-to-rvalue conversion on uninitialized values is generally prohibited.

Evidence of Expected lvalue-to-rvalue Conversion

While the C 11 Standard lacks an explicit specification regarding value categories expected by language constructs, circumstantial evidence suggests that rvalues are intended to be the default expectation.

  • Built-in operators consistently require a prvalue operand and explicitly mention lvalue-to-rvalue conversion when necessary.
  • The initialization of references implies that lvalue-to-rvalue conversion is expected for object initialization.

Application to Initializer Conversion

By analogy to the behavior of built-in operators, it is reasonable to assume that copy-initialization also expects a prvalue as an initializer. This assumption is further supported by the following:

  • The Standard emphasizes using standard conversions to convert the initializer expression to the destination type.
  • Performing lvalue-to-rvalue conversion enables the correct initialization of variables with lvalue initializers.
  • The lack of user-defined conversions suggests that value category conversions are intended to be handled by the Standard.

Implications for the Code Snippet

Under the assumption that copy-initialization expects a prvalue, the code snippet int x = x; would indeed lead to UB. This is because the right-hand 'x' is an uninitialized lvalue, and its lvalue-to-rvalue conversion would result in an indeterminate value.

Further Evidence

A proposed defect report highlights the need to clarify that lvalue-to-rvalue conversion on objects with an indeterminate value causes UB. This proposed change further supports the notion that copy-initialization should not allow such conversions.

Conclusion

Based on the available evidence, it is most likely that int x = x; constitutes UB in C , as the lvalue-to-rvalue conversion on the uninitialized 'x' is prohibited. However, it is important to note that the Standard lacks a definitive specification on expected value categories, leaving some room for ongoing debate.

The above is the detailed content of Is `int x = x;` Undefined Behavior in C Due to Lvalue-to-Rvalue Conversion?. 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