Post Reply 
Undoc'd Feature?
07-08-2022, 08:32 PM (This post was last modified: 07-08-2022 08:42 PM by jte.)
Post: #20
RE: Undoc'd Feature?
(07-08-2022 05:00 AM)Wes Loewer Wrote:  
Code:
b=a++;

This could be interpreted as either
1) evaluate a (5)
2) assign that value to a (since b refers to a) (a=5)
3) increment a, making a (and b) 6

or interpreted as
1) evaluate a (5)
2) increment a (a is now 6)
3) assigned the value in step 1 to a, so a is back to 5 again.

Am I on the right track?

A+ Smile

In theory, some of the numbered steps could be broken down into smaller pieces when machine code is being generated (e.g., if an integer was larger than the architecture’s register size [such as 32-bit ints on a 16-bit CPU or 64-bit “long long” integers on a 32-bit CPU]); these smaller steps could be interleaved by the compiler (maybe assign half of an integer, do an increment on a lower half, do a carry into the upper half, assign the other half of an integer).

(Or… if we reach even further into theoretical possibilities: perhaps the machine code for a routine has all machine registers fully employed and the compiler doesn’t want to use a register simply to hold a “1” and knows that two registers always differ by 1 [and the architecture doesn’t have an increment instruction the compiler likes… how far can we reach? Wink], it could use those two registers to effect a ++ with something like “+=5” and a “-=4”… and then slap the assignment given in the source code right in the middle of that.)

This topic always reminds me of a disagreement I had, as an undergraduate, with another undergraduate (around 30 years ago, now!), over the validity of xor-swapping with chained xor-assignment (“a ^= b ^= a ^= b”) in C — my point being that the lack of sequence points had such code also veering into undefined behaviour. (Values are being used before they are known to have settled post-assignment.) Funnily enough, when I mentioned this to my daughter when we went running yesterday, I noted that ”the other undergraduate” need only have waited 30 years for me to be more convinced of his argument, as C++17 introduces additional sequencing constraints involving assignment operators.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Undoc'd Feature? - toml_12953 - 07-03-2022, 03:42 AM
RE: Undoc'd Feature? - Wes Loewer - 07-03-2022, 09:14 AM
RE: Undoc'd Feature? - toml_12953 - 07-03-2022, 09:38 AM
RE: Undoc'd Feature? - Wes Loewer - 07-03-2022, 01:16 PM
RE: Undoc'd Feature? - Wes Loewer - 07-03-2022, 01:49 PM
RE: Undoc'd Feature? - toml_12953 - 07-03-2022, 02:18 PM
RE: Undoc'd Feature? - Wes Loewer - 07-03-2022, 06:24 PM
RE: Undoc'd Feature? - Albert Chan - 07-03-2022, 07:09 PM
RE: Undoc'd Feature? - toml_12953 - 07-04-2022, 01:49 AM
RE: Undoc'd Feature? - toml_12953 - 07-04-2022, 09:41 AM
RE: Undoc'd Feature? - Wes Loewer - 07-04-2022, 03:40 PM
RE: Undoc'd Feature? - xerxes - 08-03-2022, 08:17 PM
RE: Undoc'd Feature? - parisse - 07-03-2022, 11:02 AM
RE: Undoc'd Feature? - Wes Loewer - 07-03-2022, 01:03 PM
RE: Undoc'd Feature? - parisse - 07-03-2022, 06:31 PM
RE: Undoc'd Feature? - Wes Loewer - 07-03-2022, 07:34 PM
RE: Undoc'd Feature? - jte - 07-08-2022, 03:15 AM
RE: Undoc'd Feature? - Wes Loewer - 07-08-2022, 05:00 AM
RE: Undoc'd Feature? - jte - 07-08-2022 08:32 PM
RE: Undoc'd Feature? - Wes Loewer - 07-09-2022, 05:15 AM
RE: Undoc'd Feature? - RPNerd - 07-09-2022, 12:20 PM
RE: Undoc'd Feature? - Albert Chan - 07-09-2022, 05:10 PM
RE: Undoc'd Feature? - Wes Loewer - 07-10-2022, 05:22 AM
RE: Undoc'd Feature? - toml_12953 - 07-10-2022, 10:22 AM
RE: Undoc'd Feature? - RPNerd - 07-10-2022, 12:48 PM
RE: Undoc'd Feature? - Wes Loewer - 07-17-2022, 01:05 PM
RE: Undoc'd Feature? - jte - 07-31-2022, 02:12 AM
RE: Undoc'd Feature? - Wes Loewer - 07-31-2022, 06:43 PM
RE: Undoc'd Feature? - toml_12953 - 07-31-2022, 07:25 PM
RE: Undoc'd Feature? - Wes Loewer - 08-01-2022, 03:48 AM
RE: Undoc'd Feature? - ijabbott - 07-14-2022, 05:22 PM
RE: Undoc'd Feature? - toml_12953 - 07-10-2022, 05:36 PM
RE: Undoc'd Feature? - Albert Chan - 07-10-2022, 05:57 PM
RE: Undoc'd Feature? - Wes Loewer - 07-10-2022, 07:38 PM
RE: Undoc'd Feature? - ijabbott - 07-11-2022, 07:49 PM
RE: Undoc'd Feature? - RPNerd - 07-12-2022, 11:03 AM
RE: Undoc'd Feature? - ijabbott - 07-13-2022, 09:34 PM
RE: Undoc'd Feature? - Wes Loewer - 07-12-2022, 01:49 PM
RE: Undoc'd Feature? - ijabbott - 07-13-2022, 10:12 PM
RE: Undoc'd Feature? - toml_12953 - 07-13-2022, 10:50 PM
RE: Undoc'd Feature? - jte - 07-31-2022, 01:29 AM
RE: Undoc'd Feature? - Wes Loewer - 07-31-2022, 10:13 AM
RE: Undoc'd Feature? - OlidaBel - 07-04-2022, 09:11 AM
RE: Undoc'd Feature? - toml_12953 - 07-04-2022, 09:36 AM
RE: Undoc'd Feature? - ctrclckws - 07-13-2022, 12:44 PM
RE: Undoc'd Feature? - Wes Loewer - 07-13-2022, 08:01 PM



User(s) browsing this thread: 1 Guest(s)