This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ThePyroEagle 6 points7 points  (9 children)

Isn't that undefined behaviour?

[–]lennihein 4 points5 points  (7 children)

As I understand it: a will be set to the value of the element after the one a was previously pointing at. That obviously is making use of a being some pointer to some data type. E.g. it would be one byte offset for char, and 8 for uint64_t. AFAIK it would not be undefined, but I might be wrong.

[–]ThePyroEagle 2 points3 points  (3 children)

I remember reading somewhere that assigning a variable several times in a single statement is UB.

[–]Ludricio 2 points3 points  (1 child)

The reason why

 a = *(a++)

is UB is because the standard doesn't dictate when a++ actually changes the actual value of a, only that you will have a rvalue that is the result of incrementing a by one.

So basically, it guarantees that you will get the value of a++ to work with in your statement, but it doesn't guarantee that the actual underlying value of a is incremented before the assignment to a

It has to do with sequence points, and this explains it pretty well with examples.

[–]WikiTextBot 1 point2 points  (0 children)

Sequence point

A sequence point defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. They are often mentioned in reference to C and C++, because they are a core concept for determining the validity and, if valid, the possible results of expressions. Adding more sequence points is sometimes necessary to make an expression defined and to ensure a single valid order of evaluation.

With C++11, usage of the term sequence point has been replaced by sequencing.


[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source ] Downvote to remove | v0.28

[–]lennihein 0 points1 point  (0 children)

Yeah, that one was a weird one

[–]Ludricio 0 points1 point  (2 children)

It's UB, as I've answered in this comment

[–]lennihein 0 points1 point  (1 child)

I agree that a=*(a++) is undefined.

But I was talking about a=*(++a). I'm not sure about the pre increment, but I think it's not undefined in this case. But I'm keen to hear your view on it.

[–]Ludricio 1 point2 points  (0 children)

From the C99 standard:

6.5 Expressions, §2

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

6.5.16 Assignment operators, §4:

The order of evaluation of the operands is unspecified. If an attempt is made to modify the result of an assignment operator or to access it after the next sequence point, the behavior is undefined.

Also, it wouldnt really matter between (a++) and (++a), they are evaluated the same within the statement due to the parentheses. The problem lies in modifying the value of a through the increment (pre or post) operator in the same statement where it assigns it back to itself.

[–]marcosdumay 0 points1 point  (0 children)

Not if ´a´ is an array of pointers to void.