Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7589317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:07:49+00:00 2026-05-30T20:07:49+00:00

From Prasoon’s answer to question regarding Undefined Behavior and Sequence Points, I do not

  • 0

From Prasoon’s answer to question regarding “Undefined Behavior and Sequence Points”, I do not understand what the following means

.. the prior value shall be accessed only to determine the value to be stored.

As examples, the following are cited to possess Undefined Behaviour in C++:

  1. a[i] = i++;
  2. int x = i + i++;

Despite the explanations given there, I do not understand this part (I think I correctly understand the rest of the answer).


I do not understand what is wrong with the above code samples. I think these have well defined steps for the compiler as below.

a[i] = i++;

  • a[i] = i;
  • i = i + 1;

int x = i + i++ ;

  • x = i + i;
  • i = i + 1;

What am I missing? What does ‘prior value shall be accessed only to determine the value to be stored’ mean?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:07:51+00:00Added an answer on May 30, 2026 at 8:07 pm

    See also this question and my answer to it. I’m not going to vote to close this as a duplicate because you’re asking about C++ rather than C, but I believe the issue is the same in both languages.

    the prior value shall be accessed only to determine the value to be stored.

    This does seem like an odd requirement; why should the standard care why a value is accessed? It makes sense when you realize that if the prior value is read to determine the value to be stored in the same object, that implicitly imposes an ordering on the two operations, so the read has to happen before the write. Because of that ordering, the two accesses to the same object (one read and one write) are safe. The compiler cannot rearrange (optimize) the code in a way that causes them to interfere with each other.

    On the other hand, in an expression like

    a[i] = i++
    

    there are three accesses to i: a read on the left hand side to determine which element of a is to be modified, a read on the right hand side to determine the value to be incremented, and a write that stores the incremented value back in i. The read and write on the RHS are ok (i++ by itself is safe), but there’s no defined ordering between the read on the LHS and the write on the RHS. So the compiler is free to rearrange the code in ways that change the relationship between those read and write operations, and the standard figuratively throws up its hands and leaves the behavior undefined, saying nothing about the possible consequences.

    Both C11 and C++11 change the wording in this area, making some ordering requirements explicit. The “prior value” wording is no longer there. Quoting from a draft of the C++11 standard, 1.9p15:

    Except where noted, evaluations of operands of individual operators
    and of subexpressions of individual expressions are unsequenced. […]
    The value computations of the operands of an operator are sequenced
    before the value computation of the result of the operator. If a side
    effect on a scalar object is unsequenced relative to either
    anotherside effect on the same scalar object or a value computation
    using the value of the same scalar object, the behavior is undefined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From the original question (below), I am now offering a bounty for the following:
From time to time I see an enum like the following: [Flags] public enum
From 19 Deadly Sins of Software Security ; The following code is the poster
From this post How to use ROW_NUMBER in the following procedure? There are two
From the question here , There are two external libraries to use for http
From what I understand using something like require_once will essentially copy and paste the
from the title you can read that I'm given latitude and longitude points. They
From a web developer point of view, what changes are expected in the development
From a desktop application developer point of view, is there any difference between developing
From what I've read, VS 2008 SP1 and Team Foundation Server SP1 packages are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.