NO

Author Topic: Bug: 64-bit, unsigned num++  (Read 2985 times)

aj

  • Guest
Bug: 64-bit, unsigned num++
« on: August 27, 2008, 06:31:44 PM »
Using version 5.00.8 (Win64), on Vista SP1 x64 compiling a 64-bit DLL.


I've just spent 30 minutes looking for a bug in my program and what i found was this:

Code: [Select]
unsigned num;

num = 0;
for (i = 1; i <= some_maximum; i++) {
    num++;

    // other things happen here - not touching num
}

The error was that num was always set to one (1), no matter what - and it shouldn't.

The solution looked like this:

Code: [Select]
unsigned num;

num = 0;
for (i = 1; i <= some_maximum; i++) {
    num = num + 1;

    // other things happen here - not touching num
}
Just that one line was changed, and my problem was solved.
I won't post more of the code here - but if Pelle is interested in this problem I can PM it to you.

Offline Robert

  • Member
  • *
  • Posts: 245
Re: Bug: 64-bit, unsigned num++
« Reply #1 on: August 28, 2008, 02:30:32 AM »
#include <stdio.h>

void main( void )
{
unsigned num;
int i;

num = 0;
for (i = 1; i <= 5; i++) {
    printf("%d\n", num);
    num++;

    // other things happen here - not touching num
}
}

prints

0
1
2
3
4

What's the problem?

Robert Wishlaw

aj

  • Guest
Re: Bug: 64-bit, unsigned num++
« Reply #2 on: August 29, 2008, 06:59:54 PM »
The problem is that my code doesn't look like that.
It's a for-loop inside an if-case, all this in a function.

The solution I proposed earlier didn't solve the issue either. The reason it worked was a swprintf-call inserted in the for-loop, probably introducing a dependence that the compiler didn't think it could optimize away.

The real solution was an introduction of a variable 'temp' that just takes the value of 'num' and increases it and at a later point 'num = temp'.

I'm going to try to isolate the problem.
I know this info doesn't help very much, but as stated above the error occurs in code I'm not willing to post publicly - and I've got my hands full with other things right now.