开发者

[Memory allocation problem]Unhandled exception: Microsoft C++ exception: std::bad_alloc at memory location

开发者 https://www.devze.com 2023-01-16 03:59 出处:网络
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:

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:

int size = 512*5开发者_开发知识库12*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.

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


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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消