[内存分配问题]未处理的异常:Microsoft c++异常:std::bad_alloc在内存位置。

时间:2022-01-24 21:12:46

I am using visual studio 2008 for developing. My program needs to deal with a huge amount of memory. The error happens when my program try to allocate a 512M float array. Code is the following:

我正在使用visual studio 2008进行开发。我的程序需要处理大量的内存。当我的程序试图分配一个512M的浮点数组时发生错误。代码如下:

int size = 512*512*512;
float *buffer = new float[size];

Before this allocation, the program already consumed around 554M memory. My desktop has 4G main memory and I am using windows xp 32bits.

在此分配之前,程序已经消耗了大约5.54亿个内存。我的桌面有4G主存,我使用的是windows xp 32位。

How can I avoid the allocation error? Thanks very much for your input!

如何避免分配错误?非常感谢您的输入!

1 个解决方案

#1


4  

Your array requires too much contiguous memory. Your program has a bit less of 2 gigabytes of virtual memory available but that address space is broken up by chunks of code, data and various heaps. Memory is allocated from the free space between those chunks. On a 32-bit operating system you can get ~650 MB when you allocate immediately. That goes South when your program starts using memory. The sum of all memory allocations is still ~2GB.

数组需要太多的连续内存。你的程序只有不到2g的可用虚拟内存,但是地址空间是由代码块、数据和各种堆分割的。内存是从这些块之间的空闲空间分配的。在32位操作系统上,当您立即分配时,您可以得到~650 MB。当你的程序开始使用内存时,情况就变糟了。所有内存分配的总和仍然是~2GB。

Use a 64-bit operating system or partition your data structures. SysInternals' VMMap utility can give you insight in the virtual memory mapping of your program.

使用64位操作系统或分区数据结构。SysInternals的VMMap实用工具可以让您了解程序的虚拟内存映射。

#1


4  

Your array requires too much contiguous memory. Your program has a bit less of 2 gigabytes of virtual memory available but that address space is broken up by chunks of code, data and various heaps. Memory is allocated from the free space between those chunks. On a 32-bit operating system you can get ~650 MB when you allocate immediately. That goes South when your program starts using memory. The sum of all memory allocations is still ~2GB.

数组需要太多的连续内存。你的程序只有不到2g的可用虚拟内存,但是地址空间是由代码块、数据和各种堆分割的。内存是从这些块之间的空闲空间分配的。在32位操作系统上,当您立即分配时,您可以得到~650 MB。当你的程序开始使用内存时,情况就变糟了。所有内存分配的总和仍然是~2GB。

Use a 64-bit operating system or partition your data structures. SysInternals' VMMap utility can give you insight in the virtual memory mapping of your program.

使用64位操作系统或分区数据结构。SysInternals的VMMap实用工具可以让您了解程序的虚拟内存映射。