Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 17th, 2006, 6:23 PM   #11
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by Game_Ender View Post
You're getting pre/post increment confused Dark. 'i++' is a post increment operator, it gets runs after the expression is evaluated. So "i = i++" results in "i = i; i += 1". Also if it were a pre increment operator you would have "i = ++i" => "i += 1; i = i". So both do the same thing, are valid C/C++ and definitely don't do nothing.
No, I'm not getting pre/post increment confused.

Yes, the i++ gets run after the expression is evaluated. But is that before or after the assignment is evaluated.
The Dark is offline   Reply With Quote
Old Dec 17th, 2006, 6:27 PM   #12
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Old Dec 17th, 2006, 6:31 PM   #13
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
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.
Game_Ender is offline   Reply With Quote
Old Dec 17th, 2006, 7:15 PM   #14
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Dec 18th, 2006, 1:50 AM   #15
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5 grumpy is on a distinguished road
I suppose you could describe;
 i = i++;
as bad style, but that's an understatement. It actually yields undefined behaviour, as it modifies i twice between two sequence points.

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++;
This code can (simplistically, assuming i and j are distinct variables) be translated into;
    j = i;
    ++i;
or into;
    int temp = i;
    ++i;
    j = temp;
depending on how the compiler works. Consider what happens if j is actually i in the above two cases.

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.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC