79629670

Date: 2025-05-20 01:42:26
Score: 2
Natty:
Report link

各位前辈,我注意到了“Does anyone know where to find out the final definition to check out that it is an unsigned int (or the like)?”,如果仅仅是要检测size_t是否为typedef unsigned int 或者是 typedef unsigned long long, 我有一种不知道是否合理的溢出实验:


#include <stdio.h>

int main()
{
    unsigned int a = 0, b = 1000000;
    while (1)
    {
        printf("a = %lld  b = %lld\n", a, b);
        a = b;
        b += 1000000;
        if (a > b)
        {
            printf("a = %lld  b = %lld\n", a, b);
            break;
        }
    }
    return 0;
}

在最后一行会输出:a = 4294000000 b = 32704

#include <stdio.h>

int main()
{
    int a = 0, b = 1000000;
    while (1)
    {
        printf("a = %lld  b = %lld\n", a, b);
        a = b;
        b += 1000000;
        if (a > b)
        {
            printf("a = %lld  b = %lld\n", a, b);
            break;
        }
    }
    return 0;
}

在最后一行会输出:a = 2147000000 b = 2148000000

上述代码都发生了数据溢出。

对于size_t,是否可以通过类似的方法来验证size_t为typedef unsigned inttypedef unsigned long long

(代码测试:Windows11-VS2022-MSVC-C++14-Debug-x64)

Reasons:
  • RegEx Blacklisted phrase (2): Does anyone know
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: 王善圳