【问题标题】:g++ out of memory allocating for std::bitsetg++ 为 std::bitset 分配的内存不足
【发布时间】:2017-01-11 08:49:17
【问题描述】:

这里我分配了 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。

【问题讨论】:

  • 编译器由于某种原因内存不足,因此可能是编译器中的错误。我会更新它; 4.8.3 已经超过两年了。
  • 尝试使用-ftrack-macro-expansion=0 编译器选项。如果有帮助,那么您可能会受到gcc.gnu.org/bugzilla/show_bug.cgi?id=56746 的影响
  • @molbdnilo 我用 gcc 5.3.0 再次尝试了它。编译需要大约 15 秒的时间,但我仍然遇到同样的错误。

标签: c++ c++11 g++ out-of-memory std-bitset


【解决方案1】:

这似乎是 GCC for c++11 的一个错误:Gcc uses large amounts of memory and processor power with large C++11 bitsets。使用-std=c++98 编译对我来说是一种临时解决方法。

【讨论】:

    猜你喜欢
    • 2012-08-12
    • 2015-08-15
    • 2011-01-11
    • 2014-02-18
    • 1970-01-01
    • 2019-10-24
    • 2010-12-30
    • 2020-06-25
    • 2023-04-05
    相关资源
    最近更新 更多