c ++内存分配问题。 'new'throws bad_alloc? [英] c++ memory allocation problem. 'new' throws bad_alloc?

查看:146
本文介绍了c ++内存分配问题。 'new'throws bad_alloc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的应用程序中,我保存voxel-data(压缩字节数组),并尝试将其重新加载到内存中。



但是我的应用程序在Windows 7机器(64位,12 Gb)中崩溃,产生了bad_alloc。





压缩字节大小约为200Mb,未压缩字节大小约为300Mb(因此,在此之前只有500Mb的预留空间)语句,其中留下几乎8 Gb不包括系统内存)。我不要运行任何其他主要的内存分配步骤之前,所以我不认为内存是碎片。
我使用MinGW GCC版本4.5.0



有任何提示吗?



  QFile fileVol(strVFile); 

//读取VolumeData
fileVol.open(QIODevice :: ReadOnly);
QDataStream volstream(& fileVol);
QByteArray volCmprsdByteArray; //这保存压缩的字节数组

//读取一些附加信息
int nx,ny,nz,bbp;
int voltype;
volstream>> nx;
volstream>> ny;
volstream>> nz;
volstream>> bbp;
volstream>> voltype;
volstream>> volCmprsdByteArray; //读取压缩的voxel-data

//我们有原始的未压缩像素数据
QByteArray volUncmprsdByeArray = qUncompress(volCmprsdByteArray);

int uncompressedSize = volUncmprsdByeArray.size(); //字节数组的大小
qDebug(new char for uncompressed data size%d,uncompressedSize);

unsigned char * volumeData = NULL;

//尝试分配新内存
try {
// #####在这里。 ####
volumeData = new unsigned char [uncompressedSize];
}
catch(std :: bad_alloc e)
{
cout< lu_solver()bad_alloc:< e.what()< endl;
cout<< 尺寸<< uncompressedSize<< endl;
// return
}


解决方案

作为32位二进制在Windows上,你只能获得2GB的可用地址空间(上半部分是保留)。



当你认为地址空间可以分段通过分配很多不同的东西(DLL被加载,线程栈,动态分配),很可能你最终会有大量的分配失败很快。



可以尝试为链接器提供 / LARGEADDRESSAWARE 标志,这将在正确配置的32位Windows上提供3GB的地址空间,并在64位Windows上提供完整的4GB地址空间。真的,但是,一个好主意是找到一种方法来重新实现你的程序,这样你可以使用几个较小的分配,而不是一个巨大的。


Greetings all,

In my application I save voxel-data (compressed byte array) and trying to load it back again into memory.

But my application crashes in Windows 7 machine (64 bit ,12 Gb) giving bad_alloc .

This works fine on Linux ,and even runs on someother Windows 7 machine with 4 Gb memory.

Compressed bye size is about 200Mb and uncompressed byte size is about 300Mb.(so only 500Mb reserved before this 'new' statement,which leaves almost 8 Gb excluding system memory ). I dont run any other major memory allocation steps before this ,so I dont think memory is fragmented. I use MinGW GCC version 4.5.0

Any tips on this ?

thanks in advance.

QFile fileVol(strVFile);

//Read VolumeData
fileVol.open(QIODevice::ReadOnly);
QDataStream volstream(&fileVol);
QByteArray volCmprsdByteArray;     //This holds the compressed byte array

//Read some additional information
int nx, ny, nz, bbp;
int voltype;
volstream >> nx;
volstream >> ny;
volstream >> nz;
volstream >> bbp;
volstream >> voltype;
volstream >> volCmprsdByteArray;     //read our compressed voxel-data

//we have original uncompressed pixel data
QByteArray volUncmprsdByeArray = qUncompress(volCmprsdByteArray);

int uncompressedSize = volUncmprsdByeArray.size(); // size of the byte array
qDebug("new char for uncompressed data size %d",uncompressedSize);

unsigned char* volumeData=NULL;

//Trying to allocate new memory 
try {
    // ##### breaks here.   ####
    volumeData =new unsigned char[uncompressedSize];   
 }
catch (std::bad_alloc e)
 {
      cout << "lu_solver() bad_alloc: " << e.what() << endl;
      cout << "Size " << uncompressedSize << endl;
      //return;
}

解决方案

If you are building the program as a 32-bit binary on Windows, you only get 2GB of available address space (the upper half is reserved).

When you consider that the address space can be fragmented by lots of different things being allocated (DLLs getting loaded, thread stacks, dynamic allocation), it's likely that you'll end up having large allocations fail very quickly.

You can try giving the linker the /LARGEADDRESSAWARE flag, which will give you a 3GB address space on correctly configured 32-bit Windows and the full 4GB address space on 64-bit Windows. Really, though, it's a good idea to find a way to reimplement your program so that you can use several smaller allocations instead of one huge one.

这篇关于c ++内存分配问题。 'new'throws bad_alloc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆