array limits

Jul 23, 2008 at 6:13pm
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?
Jul 23, 2008 at 7:31pm
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.
Jul 23, 2008 at 8:07pm
@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.
Jul 23, 2008 at 11:00pm
However, I've tried allocating as much memory as possible, and it never gets past 1.8 GB or so.
Jul 23, 2008 at 11:32pm
That depends on the size of the objects your allocating.

There is a number to the amount of objects you can allocate. This is determined by the 32bit or 64bitness of your OS/CPU.

The size of each object could be 1gb though.
Jul 23, 2008 at 11:41pm
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.
Jul 23, 2008 at 11:43pm
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.

Jul 23, 2008 at 11:48pm
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.
Last edited on Jul 24, 2008 at 12:03am
Jul 24, 2008 at 12:01am
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main() {
  long long *ptr = 0;

  while (true) {
    ptr = new long long; // World's fastest memory leak :D

  }

  return 0;
}


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.
Last edited on Jul 24, 2008 at 12:02am
Topic archived. No new replies allowed.