2

在我一直在编辑的代码中,以前的程序员使用移位运算符将一个中等大的数字添加到 size_t 整数。当我出于调试目的使用这个特定整数时,我发现更改数字并没有产生可预测的结果。

输入:

std::size_t
    foo1 = 100000 << 20,
    foo2 = 200000 << 20,
    foo3 = 300000 << 20,
    foo4 = 400000 << 20;
std::cout << "foos1-4:";
std::cout << foo1;
std::cout << foo2;
std::cout << foo3;
std::cout << foo4;

产量:

foos1-4:
1778384896
18446744072971354112
1040187392
18446744072233156608

我知道这是某种溢出错误,但(据我所知有限) size_t 不应该有这些。据我了解 size_t 是一种无符号整数类型,能够容纳几乎无限数量的整数。

根据我对位移运算符的理解,此代码应将数字乘以 2^20(1048576)。本站其他页面的链接: 什么是按位移位(bit-shift)运算符,它们是如何工作的?

注意 - 我手工计算出 foo1 似乎是一个 32 位二进制截断的溢出错误,但所有其他的对我来说似乎完全是随机的。

来自http://en.cppreference.com/w/cpp/types/size_t:std::size_t 可以存储理论上可能的任何类型(包括数组)对象的最大大小。据此,我认为问题必须在于整数的声明方式或位移的操作方式。

这是怎么回事?

4

1 回答 1

11

问题不在于使用std::size_tint文字。UL您可以使用这里的后缀使它们足够长:

#include <iostream>

int main()
{
std::size_t
    foo1 = 100000UL << 20,
    foo2 = 200000UL << 20,
    foo3 = 300000UL << 20,
    foo4 = 400000UL << 20;
std::cout << "foos1-4:" << std::endl;
std::cout << foo1 << std::endl;
std::cout << foo2 << std::endl;
std::cout << foo3 << std::endl;
std::cout << foo4 << std::endl;
}

输出:

foos1-4:
104857600000
209715200000
314572800000
419430400000

现场演示


另请注意,编译器会向您发出警告:

main.cpp:6:19: warning: result of '(100000 << 20)' requires 38 bits to represent, but 'int' only has 32 bits [-Wshift-overflow=]
     foo1 = 100000 << 20,
            ~~~~~~~^~~~~
main.cpp:7:19: warning: result of '(200000 << 20)' requires 39 bits to represent, but 'int' only has 32 bits [-Wshift-overflow=]
     foo2 = 200000 << 20,
            ~~~~~~~^~~~~
main.cpp:8:19: warning: result of '(300000 << 20)' requires 40 bits to represent, but 'int' only has 32 bits [-Wshift-overflow=]
     foo3 = 300000 << 20,
            ~~~~~~~^~~~~
main.cpp:9:19: warning: result of '(400000 << 20)' requires 40 bits to represent, but 'int' only has 32 bits [-Wshift-overflow=]
     foo4 = 400000 << 20;
            ~~~~~~~^~~~~
于 2016-09-15T22:48:59.430 回答