c++ - 编译器在编译时如何检测数字溢出?

标签 c++ compiler-theory

编译器将源代码作为字符串处理,因此在 C++ 中,例如,当它鼓励像 unsigned char x = 150; 这样的语句时,它从类型限制中知道 unsigned char 必须是在 0255 之间。

我的问题是,虽然数字 150 仍然是字符串,但编译器使用什么算法来比较数字序列(在本例中为 150)与类型限制?

我做了一个简单的算法来为十进制、八进制、十六进制和小端二进制的“int”类型执行此操作,但我不认为编译器会做这样的事情来检测数字溢出。

我做的算法是用C++编码的:

typedef signed char int8;
typedef signed int  int32;

#define DEC  0
#define HEX  1
#define OCT  2
#define BIN  3

bool isOverflow(const char* value, int32 base)
{
    // left-most digit for maximum and minimum number
    static const char* max_numbers[4][2] =
    {
        //                 INT_MAX                           INT_MIN
        {                       "2147483647",                       "2147483648" }, // decimal
        {                         "7fffffff",                         "80000000" }, // hexadecimal
        {                      "17777777777",                      "20000000000" }, // octal
        { "01111111111111111111111111111111", "10000000000000000000000000000000" }  // binary
    };

    // size of strings in max_numbers array
    static const int32 number_sizes[] = { 10, 8, 11, 32 };

    // input string size
    int32 str_len = strlen(value);

    // is sign mark exist in input string
    int32 signExist = ((base == DEC || base == OCT) && *value == '-');

    // first non zero digit in input number
    int32 non_zero_index = signExist;

    // locate first non zero index
    while(non_zero_index < str_len && value[non_zero_index] == 0) non_zero_index++;

    // if non_zero_index equal length then all digits are zero
    if (non_zero_index == str_len) return false;

    // get number of digits that actually represent the number
    int32 diff = str_len - non_zero_index;

    // if difference less than 10 digits then no overflow will happened
    if (diff < number_sizes[base]) return false;
    // if difference greater than 10 digits then overflow will happened
    if (diff > number_sizes[base]) return true;

    // left digit in input and search strings
    int8 left1 = 0, left2 = 0;

    // if digits equal to 10 then loop over digits from left to right and compare
    for (int32 i = 0; non_zero_index < str_len; non_zero_index++, i++)
    {
        // get input digit
        left1 = value[non_zero_index];
        // get match digit
        left2 = max_numbers[signExist][i];

        // if digits not equal then if left1 is greater overflow will occurred, false otherwise
        if (left1 != left2) return left1 > left2;
    }

    // overflow won't happened
    return false;
}

此算法可以优化以适用于所有整数类型,但对于 float ,我必须创建一个新算法以使用 IEEE 浮点表示。

我认为编译器使用有效的算法来检测溢出而不是我的,不是吗?

最佳答案

编译器以最简单的方式处理它:他们将数字转换为适当的整数或 float 。没有法律规定编译器不能根据需要将字符串从字符串转换为其他一些表示形式。

但是现在,考虑一下您原来的问题;如果您获取数字并构建例程将它们视为数字会怎么样?比方说,一个算法可以采用

6 + 5

并将总和计算为两位数字符串11?将其扩展到其他操作,您可以直接计算 32769 是否大于 32768

关于c++ - 编译器在编译时如何检测数字溢出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6208758/

相关文章:

algorithm - 确定最大堆栈深度

c - "Type and Size Specifier"- 术语

c++ - 将 emplace_back 与 unique_ptrs 容器一起使用是否安全?

C++ — 参数个数错误

c++ - 查找数组的最大值及其位置

haskell - 如何合并Hoopl图 block /如何通过 block

compiler-construction - 编译器 : Register Allocation Against Complex Branching/Jumps

c++ - 不确定这段代码在做什么

c++ - 将局部函数作为参数传递给全局函数

language-features - 语言和 VM : Features that are hard to optimize and why