Hello! I am writing a small program and I need an array that contains 1.000.000.000 numbers. When I compile it, I get the error "size of array 'v' is too large." What is the maximum limit of elements an array can store?
OS: Windows 8.1 32 bit
IDE:CodeBlocks
"v" is an int
Compiler is GNU gcc
Thanks in advance!
First of all, it depends on environment. 32-bit OS allows less memory for an application than a 64-bit OS.
Second, you don't say what kind of array you are using, so I presume a plain statically allocated local array.
Local variables are in stack memory, which is quite limited.
Dynamic allocation uses free memory area. Try to use std::vector
You are allocating 1 billion numbers. If those numbers are integers, then they are probably 4 bytes long. You're running a 32 bit operating system which means that a process is limited to 4GB total (probably less: the OS usually reserves part of the address space for various other things).
Bottom line: you can't allocate a 4GB array when have only 4GB for the entire process.
What are you doing with the array? Chances are excellent that with a little thought, you'll find that you don't actually need to store all of the numbers simultaneously.