Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: What's causing this integer overflow?  (Read 1329 times)

0 Members and 1 Guest are viewing this topic.

GreenyUk

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
What's causing this integer overflow?
« 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! :)
« Last Edit: September 01, 2015, 01:50:24 pm by GreenyUk »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: What's causing this integer overflow?
« Reply #1 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).

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: What's causing this integer overflow?
« Reply #2 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 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...
« Last Edit: August 31, 2015, 01:50:55 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

GreenyUk

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: What's causing this integer overflow?
« Reply #3 on: September 01, 2015, 01:49:44 pm »
Ahh, okay! Thank you both for the explanations.

And indeed Hapax! My bad. :)