I am developing under Windows 10 + VS2017 (15.8.5)
The code I have is the following
#include <iostream>
int main(int argc, char** argv)
{
int i = 65176;
int j = 65224;
int k = 0;
int res[3] = {65536, 65536};
int index_int = i + j * (res[0] + 1) + k * (res[0] + 1)*(res[1] + 1);
unsigned int index_uint = i + j * (res[0] + 1) + k * (res[0] + 1)*(res[1] + 1);
size_t index_sizet = i + j * (res[0] + 1) + k * (res[0] + 1)*(res[1] + 1);
std::cout << "sizeof(int): " << sizeof(index_int) << " - index_int = " << index_int << std::endl;
std::cout << "sizeof(unsigned int): " << sizeof(index_uint) << " - index_uint = " << index_uint << std::endl;
std::cout << "sizeof(size_t): " << sizeof(index_sizet) << " - index_sizet = " << index_sizet << std::endl;
return 0;
}
which prints out
sizeof(int): 4 - index_int = -20316832
sizeof(unsigned int): 4 - index_uint = 4274650464
sizeof(size_t): 8 - index_sizet = 18446744073689234784
int vs unsigned int: Since the range for an int
is (-2,147,483,648, 2,147,483,647) the operation for index_int
overflows the limits hence the negative value, contrary to unsigned int
whose range is (0 to 4,294,967,295).
unsigned int vs size_t: However, I don't understand why size_t
has a whole different value, what I know is that it has a larger range because of 64 bit it has. What or how the calculation is carried out?