简体   繁体   中英

Why does modulo division go wrong for mix of size_t and unsigned int in C++

Given a program

#include <iostream>
using namespace std;

int main()
{
     const size_t DoW = 7;
     const unsigned int DAYS_OF_WEEK = static_cast<unsigned int> (DoW);
     unsigned int dayOfFirstDay = 0;
     unsigned int _firstDayOfWeek = 1;
     unsigned int diff = (DAYS_OF_WEEK+ (dayOfFirstDay - _firstDayOfWeek) ) % DAYS_OF_WEEK;
     cout << "diff = ("  << DAYS_OF_WEEK << " + (" << dayOfFirstDay << " - " << _firstDayOfWeek << ")) %" << DAYS_OF_WEEK
         << " = " << diff << endl;
     return 0;
}

The output of that program is

diff = (7 + (0 - 1)) %7 = 6

which is expected. But a modified program without static_cast

#include <iostream>
using namespace std;

int main()
{
     const size_t DAYS_OF_WEEK = 7;
     unsigned int dayOfFirstDay = 0;
     unsigned int _firstDayOfWeek = 1;
     unsigned int diff = (DAYS_OF_WEEK+ (dayOfFirstDay - _firstDayOfWeek) ) % DAYS_OF_WEEK;
     cout << "diff = ("  << DAYS_OF_WEEK << " + (" << dayOfFirstDay << " - " << _firstDayOfWeek << ")) %" << DAYS_OF_WEEK
         << " = " << diff << endl;
     return 0;
}

outputs

diff = (7 + (0 - 1)) %7 = 3

which is not expected. Why?

(Both programs are compiled with g++ 9.3.0 on Ubuntu 64 Bit)

It seems on your platform size_t is 64-bit, and unsigned int is 32-bit.

There is no integral promotion to 64-bits 1 . This is the danger of mixing 64-bit operands in expressions.

So a 32-bit wraparound of -1 remains as 4294967295 when converted to 64 bits.

And we get 7 + 4294967295 (performed in 64 bits) = 4294967302 (no wraparound).

4294967302 % 7 = 3


1 Except for systems where ( unsigned ) int itself is 64 bits, which is currently unlikely.

Such result can happen when size_t has more width than unsigned int .

The subtraction of unsigned int and unsigned int wraps around and results in unsigned int . 0 - 1 results in -1 , and it may become 0xffffffff when unsigned int is 4-byte long.

Then, adding that with another unsigned int will result in unsigned int , so the result looks like normal subtraction and addition.

On the other hand, adding with size_t will have it calculate in size_t domain, so truncation doesn't happen and the value 7 + 0xffffffff will be divided instead of 7 - 1 .

Here is an example code to check the values before division:

#include <iostream>
#include <ios>

int main()
{
     const size_t DoW = 7;
     const unsigned int DAYS_OF_WEEK = static_cast<unsigned int> (DoW);
     unsigned int dayOfFirstDay = 0;
     unsigned int _firstDayOfWeek = 1;
     size_t to_add = dayOfFirstDay - _firstDayOfWeek;
     size_t diff_uint = DAYS_OF_WEEK+ (dayOfFirstDay - _firstDayOfWeek);
     size_t diff_sizet = DoW+ (dayOfFirstDay - _firstDayOfWeek);
     std::cout << "sizeof(unsigned int) = " << sizeof(unsigned int) << '\n';
     std::cout << "sizeof(size_t) = " << sizeof(size_t) << '\n';
     std::cout << std::hex;
     std::cout << "to add     : 0x" << to_add << '\n';
     std::cout << "diff_uint  : 0x" << diff_uint << '\n';
     std::cout << "diff_sizet : 0x" << diff_sizet << '\n';
     return 0;
}

Here is an example of output :

sizeof(unsigned int) = 4
sizeof(size_t) = 8
to add     : 0xffffffff
diff_uint  : 0x6
diff_sizet : 0x100000006

dayOfFirstDay - _firstDayOfWeek is an unsigned int . As _firstDayOfWeek is greater than dayOfFirstDay the value is an underflow and wrap around and becomes max value of unsigned int . Let's call this value max_uint .

On the other hand DAYS_OF_WEEK is a size_t which is probably a wider type than unsigned int . This means that DAYS_OF_WEEK + max_uint is not overflowing. So you end-up computing max_uint % 7 . But max_uint % 7 is not equal to -1 ...

Try it with less obfuscation:

#include <stdio.h>
#include <stddef.h>

int main() {
  printf("0u - 1u = %u\n", 0u - 1u);
  printf("7u + (0u - 1u) = %u\n", 7u + (0u - 1u));
  printf("7zu + (0u - 1u) = %zu\n", size_t{7} + (0u - 1u));
}

The output I get:

0u - 1u = 4294967295
7u + (0u - 1u) = 6
7zu + (0u - 1u) = 4294967302

As you can see, 0u - 1u results in a wraparound. Adding this huge number to an unsigned int results in another wraparound. Adding it to a size_t doesn't as the entire value is representable. For that reason, you get different results after the modulus operator.

In the initializer expression of this declaration

unsigned int diff = (DAYS_OF_WEEK+ (dayOfFirstDay - _firstDayOfWeek) ) % DAYS_OF_WEEK;

the sub-expression (dayOfFirstDay - _firstDayOfWeek) is equal to the maximum value of the type unsigned int .

Thus in this sub-expression when DAYS_OF_WEEK has the type unsigned int

(DAYS_OF_WEEK+ (dayOfFirstDay - _firstDayOfWeek) )

an overflow occurs.

When DAYS_OF_WEEK has the type size_t when neither overflow occurs.

This is the reason of different results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM