News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

Bug: 64-bit, unsigned num++

Started by aj, August 27, 2008, 06:31:44 PM

Previous topic - Next topic

aj

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:


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:


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.

Robert

#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

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.