g++ 为 std::bitset 分配的内存不足
g++ out of memory allocating for std::bitset
我在这里分配 10^9 位:
#include <bitset>
#include <iostream>
const int N = 1000000000;
std::bitset<N> b;
int main()
{
std::cout << sizeof(b) << std::endl;
}
我得到 cc1plus.exe: out of memory allocating 268439551 bytes
。
但是当我这样做时
#include <bitset>
#include <iostream>
const int N = 1000000000;
int l[N/32];
int main()
{
std::cout << sizeof(l) << std::endl;
}
125000000 字节 (125 MB) 分配得很好。如果我将 N
更改为 10 的不同次方,我会看到两个 sizeof
是相同的。我什至看不到 268439551 字节限制是从哪里来的,因为它是 268.4 MB,而我有大约 4 GB 的可用 RAM。即使在 32 位系统上,~200 MB 也不会造成问题,并且不知何故达到了字节限制。是什么导致了这里的问题?
在具有 8 GB RAM 的 Windows 8.1 上使用 gcc 4.8.3。
这似乎是 GCC for c++11 的错误:Gcc uses large amounts of memory and processor power with large C++11 bitsets。使用 -std=c++98
编译对我来说是一个临时的解决方法。
我在这里分配 10^9 位:
#include <bitset>
#include <iostream>
const int N = 1000000000;
std::bitset<N> b;
int main()
{
std::cout << sizeof(b) << std::endl;
}
我得到 cc1plus.exe: out of memory allocating 268439551 bytes
。
但是当我这样做时
#include <bitset>
#include <iostream>
const int N = 1000000000;
int l[N/32];
int main()
{
std::cout << sizeof(l) << std::endl;
}
125000000 字节 (125 MB) 分配得很好。如果我将 N
更改为 10 的不同次方,我会看到两个 sizeof
是相同的。我什至看不到 268439551 字节限制是从哪里来的,因为它是 268.4 MB,而我有大约 4 GB 的可用 RAM。即使在 32 位系统上,~200 MB 也不会造成问题,并且不知何故达到了字节限制。是什么导致了这里的问题?
在具有 8 GB RAM 的 Windows 8.1 上使用 gcc 4.8.3。
这似乎是 GCC for c++11 的错误:Gcc uses large amounts of memory and processor power with large C++11 bitsets。使用 -std=c++98
编译对我来说是一个临时的解决方法。