![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
Quote:
Yes, the i++ gets run after the expression is evaluated. But is that before or after the assignment is evaluated. |
|
|
|
|
|
|
#12 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
My mistake - the whole assignment is the expression. So the ++ is done at the end of the whole thing.
But I would definitely mark it down as bad style. |
|
|
|
|
|
#13 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
Sorry there Dark, edited my post you were right and GCC agrees. But from the face of it, I would call it valid if bad style like yourself.
|
|
|
|
|
|
#14 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The fact that one compiler acts as you deem properly does not mean that all compilers will. If the language says it's undefined, then you can't count on a compiler implementation being any specific thing. Anything at all might happen, or nothing. What ever did (or didn't) happen would be okay.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#15 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5
![]() |
I suppose you could describe;
i = i++; In practice, it is a subtle form of undefined behaviour (i.e. it is unlikely to cause a crash). To understand why it is undefined behaviour, consider what happens with this; j = i++; j = i;
++i; int temp = i;
++i;
j = temp;There are technically more possibilities, depending on how the compiler works (eg exploiting machine instructions or other features, aggressiveness of optimisation). The C and C++ standards deliberately avoid specifying how a compiler must work internally, and the first option will be the best implementation choice on some machines and the worst on others. But specifying what happens when code is modified multiple times between sequence points (as in i = i++;) unfairly limits the freedom of the compiler to optimise performance of its output code (in the sense that it will be good for one compiler vendor but penalise another). Which is why such things are described as undefined behaviour: there is no end of possibilities for how compilers might work internally when they see such code. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Simple Perl / MySQL Problem.. pls help! | domquemo | Perl | 0 | Jan 11th, 2006 4:08 AM |
| How to post a question | nnxion | C++ | 10 | Jun 3rd, 2005 11:53 AM |
| How to post a question | nnxion | C++ | 0 | Jun 3rd, 2005 8:55 AM |
| How to post a question | nnxion | C | 0 | Jun 3rd, 2005 8:55 AM |
| Problem with using DLL from simple c++ program. | scicatur | C++ | 5 | May 12th, 2005 12:46 PM |