Hi,
can somene tell what is the max number of element of an array?At 1mil I get an overflov error,but 0.5mil runs good.Bad actually,what is the limit of memory that can be alocated?
The largest array on any system is freeRAM/sizeof(type) elements. For example, with 4 GB of RAM, you can have 4294967296 chars, 268435456 shorts, 134217728 longs or floats, or 67108864 doubles.
Or course, once you've allocated all the memory, there's very little to can do. Unless you're using virtual memory, of course. In which case it's nearly impossible to allocate all the memory.
@helios: RAM doesn't come into it. As your operating system manages a Swap File (Paged Memory) that will Grow/Shrink as required when the ram is fully utilised.
@Nandor: This is going to depend on a couple of things. If the Array is static, then it will depend on the available memory in the application's stack. Once you fill the stack you will receive an overflow error.
If the array is dynamic, you should only be limited by the amount of addressable memory. Either 32bit or 64bit based on the CPU and OS.
No, they were chars. It would be kind of a coincidence that I ran out of chars right when I was close to filling the physical memory. And I just checked to see what the virtual memory limit is and I set it to 4 GB, so that wasn't it, either.
Ok. The bit-ness of your CPU/OS tells you how many different addresses you can allocate.
If I allocate all 32bits worth of them as chars (1 byte) thats not very useful. However, If I was storing only 30 addresses each containing a 1gb bitmap. Then I would have 30gbs allocated using only addresses. This is dynamically.
Statically (on the stack vs on the heap). You can allocate only as many objects as our stack will handle before it overflows.
Sorry. I didn't express myself correctly. The arrays were arrays of chars, but they where 50*1024^2 long. Rather than allocating 50 million chars, I was allocating whole chunks of memory at a time. So it turned out to be just a few tens of addresses.
EDIT: Never mind. I just found out the number I was looking at was size in virtual memory. It'd seem there's a limit of 2 GB per program, or something like that.
As I said, static (local variables) allocation will be limited by stack size.
Dynamic allocation is limited only by the amount of addressable memory.
With this code, make sure you turn on the virtual memory display too. Once it fills the ram it'll drop the ram usage to about 20mbs, but continue to grow the virtual memory size. It will continue until the swap file is consumed and has grown to it's limit (or file system available space) then the new should fail. The speed slows dramatically once it's moved to virtual memory.