I know there is an upper limit on array size. I want to understand what determines the maximum size of an array? Can someone give a simple detailed explanation?
1 Answers
It depends, on the language, library, operating system.
For an in-memory array which is what most languages offer by default, the upper limit is the address space given to the process. For Windows this is 2 or 3 GB for 32-bit applications, and for 64-bit applications it is the smaller of 8 TB and (physical RAM + page file size limit).
For a custom library using disk space to (very slowly) access an array, the limit will probably be the size of the largest storage volume. With RAID and 10+ TB drives that could be a very large number.
Once you know the memory limit for an array, the upper limit on the number of elements is (memory / element size). The actual limit will often be less if the element is small, since the array addressing might use 32-bit unsigned integers which can only address 4 GB elements.
This is for simple contiguous, typed arrays offered by languages like C++. Languages like PHP where you can define a['hello'] = 'bobcat'; a[12] = 3.14;
are more like maps and can use much more memory per element since they store a value for the index

- 1,427
- 1
- 16
- 18
-
Even if there is an upper limit on ram size, why is an array size less than the actual memory. – Nov 09 '19 at 22:28
-
By "actual" memory to you mean the total memory of the PC, or the 2 - 3 GB process address space limits of 32-bit applications? I listed one common reason (index data type of 32-bit integer) others are going to be specific to a language's implementation. It would help if you go into more detail. – Dave S Nov 09 '19 at 23:47