undefined vs. unspecified (C++ standard)

This is a discussion on undefined vs. unspecified (C++ standard) within the C++ Programming forums, part of the General Programming Boards category; This is a quote from the C++ standard (5.4), regarding expressions: Except where noted, the order of evaluation of operands ...

  1. #1
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    undefined vs. unspecified (C++ standard)

    This is a quote from the C++ standard (5.4), regarding expressions:
    Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined.
    But right under is says:
    Code:
    i = v[i++]; // the behavior is unspecified
    i = 7, i++, i++; // i becomes 9
    i = ++i + 1; // the behavior is unspecified
    i = i + 1; // the value of i is incremented
    So, what's the difference between undefiend and unspecified? Both terms are defined in the standard, but they seem quite equivalent.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    This line worries me a little.

    i = v[i++]; // the behavior is unspecified

    Presumably, this means that it could behave differently on different compilers?

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Same for: a[i] = i++;

    Sang-drax, your answer

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    OK, thanks.

    But are the mentioned expressions undefined or just unspecified? The standard first says it's undefined then unspecified.
    If it's unspecified one can expect the same behavior in consecutive executions of a program.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >So, what's the difference between undefiend and unspecified?

    1.3.12 undefined behavior
    behavior, such as might arise upon use of an erroneous program construct or erroneous data, for which this
    International Standard imposes no requirements. Undefined behavior may also be expected when this
    International Standard omits the description of any explicit definition of behavior. [Note: permissible undefined
    behavior ranges from ignoring the situation completely with unpredictable results, to behaving during
    translation or program execution in a documented manner characteristic of the environment (with or without
    the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a
    diagnostic message). Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed. ]

    1.3.13 unspecified behavior
    behavior, for a well formed program construct and correct data, that depends on the implementation. The
    implementation is not required to document which behavior occurs. [Note: usually, the range of possible
    behaviors is delineated by this International Standard. ]

    >But are the mentioned expressions undefined or just unspecified?
    i = v[i++]; // undefined
    i = 7, i++, i++; // i becomes 9
    i = ++i + 1; // undefined
    i = i + 1; // the value of i is incremented

    Basically, the order of evaluation for an expression where not explicitly stated, is unspecified, such an example is:

    int x = f() + g();

    If an item is modified more than once in between sequence points such as a semicolon or comma, the behavior is undefined. The expression

    i = i + 1;

    does not invoke undefined behavior because the value of i is only used as an lvalue once. The expression

    i = 7, i++, i++;

    also does not invoke undefined behavior because the comma operator is a sequence point. i is assigned a value of 7 and then incremented twice. On the undefined side, the expression

    i = v[i++];

    is undefined because i is modified both on the right and left hand side of the assignment expression. There's no sequence point in between, so anything can happen. Next,

    i = ++i + 1;

    is also undefined, once again because i is modified more than once between sequence points.

    If you read your quote carefully, it will say the the order of evaluation of both the operators and side effects is unspecified unless explicitly stated. An item can only be modified once in an expression and any other use must be to access the contents of the item, not change them. If these rules are broken, the result is undefined.

    -Prelude
    My best code is written with the delete key.

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    You're saying:
    i = v[i++]; // undefined
    The standard is saying:
    i = v[i++]; // the behavior is unspecified
    According to the quoted paragraph, the expression should be undefined. Perhaps a typo in the code example?
    Last edited by Sang-drax; 11-11-2002 at 03:32 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >According to the quoted paragraph, the expression should be undefined. Perhaps a typo in the code example?
    This is an error in the standard. "unspecified" is supposed to be "undefined".

    -Prelude
    My best code is written with the delete key.

  8. #8
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Prelude

    This is an error in the standard. "unspecified" is supposed to be "undefined".
    Thanks alot. Now it makes sense!
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 04:15 PM
  2. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  4. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 05:46 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21