简体   繁体   中英

How to allocate a large amount of memory for an array?

The machine has 2 GB and more free memory. Like the maximum number of elements of the array is limited only by the capabilities of the OS / computer. That is, having a minimum of 2 GB array can have 2 ^ 32 -1 elements. But the compiler does not miss. What if I really want an array with 2 ^ 32 elements? :) I tried 2 ^ 31-1, but it does not work ... OS 64 bit, 6 GB memory, Win32 Console App

char * buffer = new char[2147483647]; //Microsoft C++ exception: std::bad_alloc at memory location 0x004FF998.

You CANNOT get a 2 GB single allocation in a Windows 32-bit application. You'd think that you could, since you can get 3 or 4 GB with /LARGEADDRESSAWARE. But you can't.

The Windows OS maps some trap pages right around the 2GB mark , for catching some kinds of programming errors I assume (actually read the link, I was wrong, it made the Alpha AXP port easier). And that means that your 2 GB array has nowhere it can fit.

So yeah, build your app as a 64-bit application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM