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

  • Home
  • SEARCH
  • 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 8659799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:05:42+00:00 2026-06-12T16:05:42+00:00

Possible Duplicate: Undefined Behavior and Sequence Points As defined in the standard, E1 +=

  • 0

Possible Duplicate:
Undefined Behavior and Sequence Points

As defined in the standard, E1 += E2 is almost same to E1 = E1 + E2 except that E1 is only evaluated once. So, in addition, would “p += (*p)++ + c”; cause an undefined behavior?

Try the following code in gcc/g++ (4.7 / 4.4). There are 2 kind of results: bxxxxx (g++4.7) or axbxxx (gcc, g++ 4.4). If we’re executing (1) but not (2) in the code, we can only get axbxxx.

#include <stdio.h>

int main() {
    char s[] = "axxxxx";
    char *p = s;

    printf("s = %s in the beginning.\n"
           "p is pointed at the %d-th char.\n", s, p - s);
    //p = p + (*p)++ * 3 + 2 - 'a' * 3; // (1)
    p += (*p)++ * 3 + 2 - 'a' * 3; // (2)
    printf("p is moved ahead by %d steps\n", p - s);
    printf("s = %s after the operation.\n", s);
    return 0;
}

I can’t find why it cause undefined behavior, nor can I assert that it’s a bug of gcc.

For the axbxxx result, I also can’t understand why a operand or post ++ is evaluated twice (once getting the value, and later saving it). Since in the standard says “1 … is added to it”, I think the address should only be evaluated once. If the address of the operand of the post ++ is evaluated only once, the effect of the expression will be the same despite in whatever order the assignments are executed.

=== UPDATE ===

After reading the document linked in the first comment, I think the following rule may matter:

“2) Furthermore, the prior value shall be accessed only to determine the value to be stored.” .

So, would the access of p in “p = p + (*p)++ * 3 + c” be considered a part of “prior value” of *p which has nothing to do with the value to be stored in *p?

IMO, this rule is not violated.

  • 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-06-12T16:05:43+00:00Added an answer on June 12, 2026 at 4:05 pm

    No, p = p + (*p)++ * 3 + c doesn’t cause any undefined behavior, assuming that p does not point at c.

    In this case the questionable part is the read and modification of the value of *p inside the expression. However, that value is read for the purpose of determining the new value of p (there’s a straightforward data dependency of the new value of p on the value read in *p), so it does not violate the requirements.

    I’d guess that the bug in the compiler is actually rooted in its incorrect behavior in an unspecified situation. Note that the expression has two side-effects: store the new value in p and store the new value in *p. It is unspecified in which order these side-effects occur. However, during the evaluation of the (*p)++ subexpression the compiler was supposed to “fix” the specific lvalue argument of ++ to make sure that the new (incremented) value was stored in that exact object. It looks like the older version of the compiler failed to do that, i.e. the new value of p is evaluated first and then the new value of *p is stored through the new value of p. This is obviously incorrect.

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

Sidebar

Related Questions

Possible Duplicate: Undefined, unspecified and implementation-defined behavior I know calling delete on same object
Possible Duplicate: Undefined Behavior and Sequence Points The variable i is changed twice, but
Possible Duplicate: Undefined Behavior and Sequence Points int a=10; a=a++; a=++a; a=(a++); Can you
Possible Duplicate: Undefined Behavior and Sequence Points the output of the programme #include<stdio.h> main()
Possible Duplicate: Undefined Behavior and Sequence Points Working out the below code by hand:
Possible Duplicate: Unsequenced value computations (a.k.a sequence points) Undefined Behavior and Sequence Points Operator
Possible Duplicate: Undefined Behavior and Sequence Points after the following c++ code, the array
Possible Duplicate: FAQ : Undefined Behavior and Sequence Points Different outputs on different compiler?
Possible Duplicate: Undefined behavior and sequence points What is the value of x after
Possible Duplicate: Undefined Behavior and Sequence Points What is the outcome of the following

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.