[ create a new paste ] login | about

Link: http://codepad.org/INu73waX    [ raw code | fork ]

C++, pasted on May 10:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void deduct_gold_from_player_for_gold_auction(int64 &playersGold, int numberOfStacks)
{
     const int GOLD_PER_STACK = 10000000;

     //imagine numberOfStacks * GOLD_PER_STACK is 6 billion
     //goldToRemove is 32 bits and will not be enough to
     //hold the gold to deduct, it will overflow and flip
     //to a value lesser than 2 billion.
     int goldToRemove = numberOfStacks * GOLD_PER_STACK;

     playersGold -= goldToRemove; //only < 2 billion deducted!
}

//
void refund_gold_to_player ( ... ) {
     //here the right math would give back the full 6 billion!
}


Create a new paste based on this one


Comments: