Is reading an indeterminate value undefined behavior?

前端 未结 4 1233
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 13:47

The question arose in the comments of an answer to the question Is C/C++ bool type always guaranteed to be 0 or 1 when typecast\'ed to int?

The code in question allo

相关标签:
4条回答
  • The answer to this question changes with the latest C++1y working draft(N3946) which we can find here. Section 8.5 Initializers paragraph 12 changes a lot from C++03 and C++11 and now contains the following (emphasis mine):

    If no initializer is specified for an object, the object is default-initialized. When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value, and if no initialization is performed for the object, that object retains an indeterminate value until that value is replaced (5.17). [ Note: Objects with static or thread storage duration are zero-initialized, see 3.6.2. — end note ] If an indeterminate value is produced by an evaluation, the behavior is undefined except in the following cases:

    and goes on to list some exceptions for unsigned narrow character type only, I have a complete quote in Has C++1y changed with respect to the use of indeterminate values and undefined behavior?.

    So in your case b has automatic storage duration and is not initialized and therefore has indeterminate value. So evaluating b[0] is indeed undefined behavior.

    Previously we were required to use the lvalue-to-rvalue conversion to prove this was undefined but that is problematic since the conversion is underspecified.

    Note that indeterminate value is italicized in this section and therefore it means it is being defined in place and so now C++1y actually defines the term. Previously the term was used without a definition, this is covered in defect report 616.

    0 讨论(0)
  • 2020-11-30 14:23

    On bool, the standard says under 3.9.1 Fundamental types:

    Values of type bool are either true or false.

    With a footnote stating:

    Using a bool value in ways described by this International Standard as “undefined,” such as by examining the value of an uninitialized automatic object, might cause it to behave as if it is neither true nor false.

    0 讨论(0)
  • 2020-11-30 14:24

    The fact that reading an Indeterminate Value generally results in Undefined Behavior is not merely a "theoretical" issue. Even for types where all possible bit patterns have defined values, it should not be considered "surprising" for indeterminate values to behave in ways which differ from Unspecified values. For example, if *p holds Indeterminate Value, and x is not used anywhere except as shown, the code:

    uint32_t x,y,z;
    ...
    x = *p;
    if (condition1) y=x;
    ... code that "shouldn't" affect *p if its value is defined
    if (condition2) z=x;
    

    could be rewritten as:

    if (condition1) y=*p;
    ... code that "shouldn't" affect *p if its value is defined
    if (condition2) z=*p;
    

    If the value of *p is Indeterminate, a compiler would not be forbidden from having the code between the two "if" statements modify its value. For example, if the storage occupied by *p was occupied by a "float" before it freed and re-malloc'ed, the compiler might write that "float" value between the two "if" statements above.

    0 讨论(0)
  • 2020-11-30 14:36

    yes, formally an rvalue conversion of indeterminate value is UB (except for unsigned char, originally i wrote "and variants" but as i recall the formal caters to 1's complement signed char where possibly minus 0 could be used as trap value)

    i'm too lazy to do the standard paragraph lookup for you, and also to lazy to care about downvotes for that

    however, in practice only a problem on (1) archaic architectures, and perhaps (2) 64-bit systems.

    EDIT: oops, i now seem to recall a blog posting and associated Defect Report about formal UB for accessing indeterminate char. so perhaps i'll have to actually check the standard, + search DRs. argh, it will have to be later then, now coffee!

    EDIT2: Johannes Schaub was kind enough to provide this link to SO question where that UB for accessing char was discussed. So, that's where I remembered it from! Thanks, Johannes.

    cheers & hth.,

    0 讨论(0)
提交回复
热议问题