Click here to Skip to main content
15,909,822 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: How to increment a variable Pin
Luc Pattyn19-Mar-09 7:39
sitebuilderLuc Pattyn19-Mar-09 7:39 
GeneralRe: How to increment a variable Pin
Robert Surtees19-Mar-09 7:49
Robert Surtees19-Mar-09 7:49 
GeneralRe: How to increment a variable Pin
Tom Delany9-Apr-09 12:12
Tom Delany9-Apr-09 12:12 
GeneralRe: How to increment a variable Pin
Robert Surtees9-Apr-09 19:56
Robert Surtees9-Apr-09 19:56 
GeneralRe: How to increment a variable Pin
CurtD19-Mar-09 12:59
CurtD19-Mar-09 12:59 
GeneralRe: How to increment a variable Pin
Luc Pattyn19-Mar-09 13:11
sitebuilderLuc Pattyn19-Mar-09 13:11 
GeneralRe: How to increment a variable Pin
supercat919-Mar-09 18:40
supercat919-Mar-09 18:40 
GeneralRe: How to increment a variable Pin
CurtD20-Mar-09 11:50
CurtD20-Mar-09 11:50 
supercat9 wrote:
Wouldn't a more efficient implementation be to simply do nothing, given the lack of a sequence point between the increment and the assignment? Not that I'd generally expect a compiler to find that optimization, but I would have expected code like: mov eax,[_i] ; Fetch value before increment inc dword [_i] ; Do increment mov [_i],eax ; Store fetched valuewhich would, of course, effectively do nothing.

BTW, is there any guarantee about the behavior of "do_something(var++)" if do_something references var? What about "i=++i"? Even though it's hard to imagine that "i=++i;" would do anything other than increment "i", I don't think there's any guarantee that it won't?

This code doesn't "do nothing"
i = i++;
It still has to inc i and store it in i. It is just a long form of
++i;

The VS optimizer usually does a pretty good job. I had to heavily optimize some code a few years ago. I don't remember which VS version I was using, but I was impressed with the code it came up with.

In this case it generates

mov eax,1
add dword ptr [i],eax

This seems a little strange to me. Maybe an "add" is much faster than an "inc". It's been a while since I was optimizing asm code.

do_something(var++) is going to push the current var on the stack, call do_something, then inc var. Nothing strange about that.
GeneralRe: How to increment a variable Pin
supercat923-Mar-09 5:53
supercat923-Mar-09 5:53 
GeneralRe: How to increment a variable Pin
CurtD23-Mar-09 6:38
CurtD23-Mar-09 6:38 
GeneralRe: How to increment a variable Pin
supercat923-Mar-09 7:22
supercat923-Mar-09 7:22 
GeneralRe: How to increment a variable Pin
CurtD23-Mar-09 8:41
CurtD23-Mar-09 8:41 
GeneralRe: How to increment a variable [modified] Pin
CurtD24-Mar-09 8:34
CurtD24-Mar-09 8:34 
GeneralIt's the thought that counts PinPopular
CurtD18-Mar-09 15:07
CurtD18-Mar-09 15:07 
GeneralRe: It's the thought that counts Pin
Michael Bookatz19-Mar-09 7:24
Michael Bookatz19-Mar-09 7:24 
GeneralRe: It's the thought that counts Pin
CurtD19-Mar-09 12:34
CurtD19-Mar-09 12:34 
GeneralRe: It's the thought that counts Pin
Tony Pottier19-Mar-09 23:29
Tony Pottier19-Mar-09 23:29 
GeneralRe: It's the thought that counts Pin
Luc Pattyn19-Mar-09 7:28
sitebuilderLuc Pattyn19-Mar-09 7:28 
GeneralRe: It's the thought that counts Pin
Mycroft Holmes20-Mar-09 0:18
professionalMycroft Holmes20-Mar-09 0:18 
GeneralRe: It's the thought that counts Pin
supercat920-Mar-09 6:30
supercat920-Mar-09 6:30 
GeneralRe: It's the thought that counts Pin
CurtD20-Mar-09 12:03
CurtD20-Mar-09 12:03 
GeneralRe: It's the thought that counts Pin
Thomas Weller23-Mar-09 8:58
Thomas Weller23-Mar-09 8:58 
GeneralRe: It's the thought that counts Pin
CurtD24-Mar-09 8:12
CurtD24-Mar-09 8:12 
GeneralRe: It's the thought that counts Pin
Vikram A Punathambekar6-Apr-09 19:55
Vikram A Punathambekar6-Apr-09 19:55 
GeneralRe: It's the thought that counts Pin
BillW3323-Apr-09 6:47
professionalBillW3323-Apr-09 6:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.