Sign in to follow this  
pupillo

C ++ postfix operator

Recommended Posts

Hi, in this simple example: char ccc[50]; unsigned char p; p=10; ccc[(p++) + p*2]=123; what happens? 1) p is incremented immediately after evaluating p, just before making p*2. ccc[32] becomes 123 2) p is incremented after making =123. ccc[30] becomes 123 instead of ccc[32] In my opinioin the 1) should be correct but Visual C++ and gcc follow the 2), whereas my crosscompiler for Renesas CPU follows the 1) What do you think? Regards Tommaso

Share this post


Link to post
Share on other sites
The order of evaluation is not defined in c++, i.e. it is undefined whether the value of p+1 is assigned directly after reading p, or if it happens after the evaluation of the whole statement. So it is save to use p++ or ++p as long as you 'use' p just once.

edit: see for example wikipedia

btw: I acknowledge about your opinion when it *should* occur, even if that's not beginner's compiler construction to implement it (I've tried it once)

Share this post


Link to post
Share on other sites
The standard simply states that "after the result is noted, the value of the object is modified by adding 1 to it". Of course, who the hell knows when the "result is noted".

And while you should never write code like that, I do believe that 2) is the most sane way to implement it. To me, the "post" in post-fix means right after the next sequence point (technically right before the sequence point but that's arguing semantics), the closest defined point in the execution sequence. Not that opinions really matter since the different compilers implement it the way they do regardless of what we think :)

Share this post


Link to post
Share on other sites
Quote:
Original post by pupillo
ccc[(p++) + p*2]=123;

1) p is incremented immediately after evaluating p, just before making p*2.

Why are you even assuming that "(p++)" is evaluated before "p*2"? The order of evaluation of operands is also undefined. For example, the following code

int f()
{
std::cout << "hello ";
return 0;
}

int g()
{
std::cout << "world!";
return 0;
}

std::cout << f()+g() << std::endl;

can output "hello world!0" or "world!hello 0" because f() or g() may be called first.

Share this post


Link to post
Share on other sites
[/quote]
Why are you even assuming that "(p++)" is evaluated before "p*2"?
[/quote]
Yes, the order depends on the compiler and I supposed that the order was L to R, but it didn't want to be the point of the issue: I could post my dubt in another way: "Is the increment done before any other evaluation/operation or at the end of the entire expression?". I could find out an example where, even supposing R to L, the problem remains.

Share this post


Link to post
Share on other sites
Quote:
Original post by pupillo
Yes, the order depends on the compiler and I supposed that the order was L to R, but it didn't want to be the point of the issue: I could post my dubt in another way: "Is the increment done before any other evaluation/operation or at the end of the entire expression?". I could find out an example where, even supposing R to L, the problem remains.

There are no sequence points in the expression calculating your index. That means the result is undefined. If you invoke undefined behaviour, the compiler is allowed to do anything it likes, including reformat your hard drive.

The only valid answer to your above question is this: the increment may be done before or after any other evaluation or operation, or after the at the end of the entire expression.

For the code to be correct it must be rewritten.

Share this post


Link to post
Share on other sites
Quote:
Original post by pupillo
Is the increment done before any other evaluation/operation or at the end of the entire expression?

The increment is guaranteed to take effect at the next sequence point. That means it could happen anywhere between "right after p++" and "at the end of the entire expression".

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this