Girl_Developer
Girl_Developer

Reputation: 135

The limited allocation size C++

I use Visual Studio 2008. I have dynamically declared the variable big_massive:

unsigned int *big_massive = new unsigned int[1073741824]

But, when I tried to debug this program, I got following error: Invalid allocation size: 4294967295 bytes. I hope there are any path to avoid such error? Thank you!

Upvotes: 3

Views: 2893

Answers (3)

BatchyX
BatchyX

Reputation: 5114

  • A 32 bit system cannot access more than 4GB of memory per process. However, allocating 3GB of memory is fine on OS supporting lazy allocation and overcommiting, even if you only use the first 10kB, and your maximum swap+memory is 1GB anyway. But keep in mind that relying on this is stupid in the first place.
  • Before trying to use that much memory, check if you can't represent your data in a more compact form. If your array has holes, or values are repeated, or you don't use the full 32bit range of your int, or you don't need those values to have a specific order, just do not use an array.
  • Remember RAM is for temporary data. If your data need to be written on disk, why don't you use disk space in the first place. You might even use memory-mapped files (you select a part of your file, and you can access it like memory). You might also like the (easier or not) alternatives of databases management systems.

Upvotes: 2

Jon Hanna
Jon Hanna

Reputation: 113242

Amount of virtual memory per process on 32bit Windows system or 64bit Windows system running a 32bit program (WoW64): 2147483648 Amount of memory needed to hold an array of 1073741824 4-byte unsigned integers: 4294967296 Can't possibly fit in the amount of memory available, so it's an invalid allocation.

Upvotes: 3

Mat
Mat

Reputation: 206689

That allocation is simply not possible on 32bit x86 systems with sizeof(int)==4 (you are requesting 4GB). A process's total address space is limited to 4GB, and the process itself is usually limited to less than that (2GB or 3GB for 32bit Windows depending on boot.ini settings and the Windows edition, not sure which limit applies for 32bit processes on 64bit Windows, but 4GB is simply not possible).

For the 64bit case, you'd need to have 4GB of virtual memory available to back that allocation for it to succeed.

Upvotes: 6

Related Questions