SFML community forums

Help => General => Topic started by: GreenyUk on August 31, 2015, 01:00:22 am

Title: What's causing this integer overflow?
Post by: GreenyUk on August 31, 2015, 01:00:22 am
I have the following code:

this->position.y = (window->getSize().y - (GRID_HEIGHT * TILE_SIZE)) / 2;

When window->getSize().y - (GRID_HEIGHT * TILE_SIZE) < 0, it overflows.

this->position.y = -60 / 2;                                                  // reaches correct answer of -30
this->position.y = (window->getSize().y - (GRID_HEIGHT * TILE_SIZE));        // reaches correct answer of -60
this->position.y = (window->getSize().y - (GRID_HEIGHT * TILE_SIZE)) / 2;    // overflows

Either I'm missing something crazy obvious, or there's something weird going on here. How can line 1 work fine, but line 3 overflows when it's essentially the same equation?

If I replace all values with their actual values it works fine:

this->position.y = (window->getSize().y - (GRID_HEIGHT * TILE_SIZE)) / 2;   // overflow. result is 2147483618
this->position.y = (1080 - (19 * 60)) / 2;                                  // exact same values as the above line, just hard-coded, and results in -15

Any help would be greatly appretiated, I'm very confused right now! :)
Title: Re: What's causing this integer overflow?
Post by: G. on August 31, 2015, 07:22:51 am
window->getSize().y is an unsigned int, and the resulting type of your subtraction is probably an unsigned int too so it overflows to 4294967236 (max value of an unsigned int - 60) because it's negative, and then it overflows back to -60 when giving it to position.y.
When you divide it by 2, it no longer overflows back to -60 because 2147483618 is less than the maximum value of a signed int (2147483648).
Title: Re: What's causing this integer overflow?
Post by: Hapax on August 31, 2015, 01:39:47 pm
Your "hard-coded" values are all signed ints. If you want the same behaviour from the variables, you could cast (http://en.cppreference.com/w/cpp/language/static_cast) them to a signed int.

Try this:
this->position.y = (1080u - (19u * 60u)) / 2u;

By the way, your hard-coded (signed int) result should be -30, not -15...
Title: Re: What's causing this integer overflow?
Post by: GreenyUk on September 01, 2015, 01:49:44 pm
Ahh, okay! Thank you both for the explanations.

And indeed Hapax! My bad. :)