Note that there are some explanatory texts on larger screens.

plurals
  1. POC/C++ post-increment/-decrement and function call
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points">Undefined Behavior and Sequence Points</a> </p> </blockquote> <p>I am using microsoft visual c++. Look at the following example:</p> <pre><code>int n = 5; char *str = new char[32]; strcpy(str, "hello world"); memcpy(&amp;str[n], &amp;str[n+1], 6+n--); printf(str); // output is "hell world" </code></pre> <p>So unexpectadly my compiler produces code that <strong>first</strong> decrements n and then executes memcpy. The following source will do what i expected to happen:</p> <pre><code>int n = 5; char *str = new char[32]; strcpy(str, "hello world"); memcpy(&amp;str[n], &amp;str[n+1], 6+n); n--; printf(str); // output is "helloworld" </code></pre> <p>First I tried to explain it to myself. The last parameter gets pushed on the stack first, so it may be evaluated first. But I really believe that post increment/decrement guarantee to be evaluated after the next semicolon.</p> <p>So I ran the following test:</p> <pre><code>void foo(int first, int second) { printf("first: %i / second: %i", first, second); } int n = 10; foo(n, n--); </code></pre> <p>This will output "first: 10 / second: 10".</p> <p>So my question is: Is there any defined behaviour to this situation? Can somebody point me to a document where this is described? Have I found a compiler bug ~~O.O~~?</p> <p>The example is simplyfied to not make sence anymore, it just demonstrates my problem and works by itself.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload