简体   繁体   English

在32位应用程序中使用uint64_t

[英]Using uint64_t in 32 bit application

Due to some libraries, I have to compile my application in 32 bit, but I need to use integer variables that exceed the max number of 32 bit types. 由于某些库,我必须以32位编译我的应用程序,但我需要使用超过32位类型的最大数量的整数变量。 So for example if I try to use uint64_t I get an overflow at 2147483647 . 因此,例如,如果我尝试使用uint64_t我会在2147483647获得溢出。

I thought it is possible to use 64 bit integer variables in 32 bit application, so what did I miss here? 我认为可以在32位应用程序中使用64位整数变量,所以我在这里错过了什么? Do I have to include some specific header oder do I have to set some option therefore? 我是否必须包含一些特定的标题或者我必须设置一些选项吗? Using VS17. 使用VS17。

EDIT: 编辑:

I did some testing, and in this example program, I can reproduce my overflow problem. 我做了一些测试,在这个示例程序中,我可以重现我的溢出问题。

#include <iostream>

int main()
{
    uint64_t i = 0;

    while (true)
    {
        std::printf("%d\n",i);
        i += (uint64_t)10000;
    }
    return 0;
}

The bug is here: 错误在这里:

 std::printf("%d\\n",i); ^^ 

You've used the wrong format specifier, and therefore the behaviour of the program is undefined. 您使用了错误的格式说明符,因此未定义程序的行为。 %d is for signed int . %d用于signed int You need to use 你需要使用

std::printf("%" PRIu64 "\n",i);

PRIu64 is declared in <cinttypes> . PRIu64<cinttypes>声明。

PS You also haven't included the header which declares std::printf . PS你还没有包含声明std::printf的头文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 uint64_t在32位机器上写入 - uint64_t writes in 32 bit machine 为什么在 64 位机器上使用 uint8_t 编写的这段代码比使用 uint32_t 或 uint64_t 编写的类似代码运行得更快? - Why does this piece of code written using uint8_t run faster than analogous code written with uint32_t or uint64_t on a 64bit machine? std::bitset&lt;64&gt; 是否适用于 uint64_t 的 32 位机器? - Does std::bitset<64> work on 32 bit machines for uint64_t? 将包含 32 位值的 uint64_t 传递给参数实际上是 uint32_t 的外部 function 是否安全? - Is it safe to pass a uint64_t containing a 32-bit value to an external function whose parameter is actually a uint32_t? uint64_t的未定义高阶,同时移位和屏蔽32位值 - Undefined high-order of uint64_t while shifting and masking 32-bit values int32_t 到 uint64_t 转换的顺序 - Order of int32_t to uint64_t casting uint64_t或std :: bitset上的位操作 - Bit manipulation on uint64_t or std::bitset 如何使用按位运算将随机 uint64_t 转换为范围 (0, 1) 内的随机双精度值 - How to convert random uint64_t to random double in range (0, 1) using bit wise operations 如何在uint64_t中移位&gt; = 32位? - How to shift >= 32 bits in uint64_t? 为什么在 Mac OS X 上使用 size_t 时 uint32_t 和 uint64_t 之间存在歧义? - Why is there ambiguity between uint32_t and uint64_t when using size_t on Mac OS X?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM