【发布时间】:2021-02-10 17:01:22
【问题描述】:
此代码显示消息“在抛出 'std::bad_alloc' 实例后调用终止 什么():std::bad_alloc
进程返回 3 (0x3) 执行时间:0.331 s" 我无法确定的问题在哪里。我使用了代码块。我的电脑内存是 8GB。
#include <iostream>
#include<vector>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <sstream>
using namespace std;
int main(){
ofstream bcrs_tensor;
bcrs_tensor.open("bcrs_tensor_Binary", ios::out | ios::binary);
int X=6187,Y=25,Z=78,M=33;
int new_dimension_1,new_dimension_2,new_x_1,new_x_2;
new_dimension_1=X*Z;
new_dimension_2=Y*M;
int* new_A = new int[ new_dimension_1*new_dimension_2 ];
vector<int> block_value,CO_BCRS,RO_BCRS;
block_value.reserve(303092010);
CO_BCRS.reserve(1554318);
RO_BCRS.reserve(37124);
cout<<"size"<<sizeof(block_value)<<endl;
return 0;
}
【问题讨论】:
-
确保你是为 x64 架构构建的,x32 的上限是 2GB,而你已经接近这个上限了。
-
new int[ new_dimension_1*new_dimension_2 ]需要大约 2GB 的内存。您的向量需要另外 1-1.5 GB 的内存。您是否有足够的能力来涵盖这一点,并且还知道这些夹头需要是连续的。 -
有什么办法可以解决这个问题吗? @Yksisarvinen
-
@SafayetHossainSobuj 是的 - 重新设计你的代码,不要一次分配这么多内存。或者为 64 位系统编译你的代码。
-
@SafayetHossainSobuj 您没有展示您实际使用分配的所有内存在做什么,但设计合理的算法很少需要分配 2GB 的 内存 来处理 2GB 数据.
标签: c++ memory storage tensor bad-alloc