繁体   English   中英

为什么在 C++ 中编译 64 位时会出现 32 位溢出?

[英]Why do I get a 32-bit overflow when compiling in 64-bit in C++?

因此,我决定测试一个新的 C++ 龟型图形库。 我决定通过制作斐波那契螺旋来测试它。 但是,在运行程序时,它会停在中间。 我检查了发生了什么,斐波那契值反转为 -2147483647,就好像它是用 32 位编译的一样。 有人可以帮忙吗? 我在 Windows 上用 g++ 编译它。 注意:我使用向量来存储值。 这是我的代码:

//Includes and namespaces
#include "Cturtle.hpp"
#include<iostream>
namespace ct = cturtle;

std::vector<long int> fibonacci = {1, 2}; //This vector stores the fib sequence
ct::TurtleScreen scr; //Create a new Screen
ct::Turtle turtle(scr); //Create a new turtle on the screen

int main() {
    turtle.speed(ct::TS_FASTEST); //Set the turtle's speed to max
    
    while(true) {
        for(int i = 0; i < fibonacci.back(); i++) {
            
            fibonacci.push_back(fibonacci.back() + fibonacci.back()-1); //Add a new value to the fibonacci vector based off the previous two
            std::cout << fibonacci.back() << '\n'; //This is here for debug
            turtle.forward(1); //Move the turtle 1 step forward
            turtle.right(90 / fibonacci.back()); //Turn according to 90 divided by the current fibonacci value
        }
    }
}

MinGW 使用 MSVC 友好的 LLP64 数据 model

在 LLP64 中, intlong int都是 32 位,与平台位数(32/64 位)无关。

要获得 64 位数据类型,请使用long longint64_t

例如std::vector<long long> 还有这里: for(long long i = 0; ...

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM