[英]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.