Re: Maximum size that an array can hold in C++
On Jun 8, 11:26 am, Ioannis Vranos <ivra...@freemail.gr> wrote:
Ankur Arora wrote:
[...]
One thing is sure, the maximum size in bytes of any object
(including built-in arrays in the stack, or in the heap), can
not be larger than the maximum value of size_t.
[...]
Usually the stack is more limited than the maximum value of
size_t.
Usually, an array anywhere is more limited than the maximum
value of size_t. On machines with linear addressing, size_t
will typically have the same size as a pointer, and be able to
contain the total size of your address space. Practically, the
code is going to take up some of that address space, and the
implementation will probably reserve some as well (the page 0,
for example, will not be mapped), so you're forcebly less than
size_t. If I look at the two architectures I have readily
available here (Solaris on a 64 bit Sparc, and Linux on a 32 bit
PC), size_t max is 18446744073709551615 and 4294967295. Both
systems put the code at the bottom, and the stack at the top, so
all data must fit between where the stack starts and where the
code ends: this gives 18446744067265583236 and 3078461488,
respectively. (That second figure seems rather low to me. On a
32 bit Sparc under Solaris, it is 4290217624, which is more what
one would expect.) And clearly, on the 64 bit systems, I'm not
going to be able to create an array anywhere near that size,
anywhere. (That's more that 18 EB, and I've only got a little
more than 100 GB virtual memory configured.)
A more specific way is by manually modifying the value of SIZE
in the following code, and running the compilation result:
#include <cstddef>
#include <iostream>
int main()
{
using std::cout;
using std::size_t;
const size_t SIZE= 1;
char array[SIZE]= {0};
}
For convenience, I'd make SIZE something like 2024*N, and invoke
the compiler with -DN=1 (or more). I wouldn't recommend trying
this, however---at least with Sun CC under Solaris, it will pass
with some pheonomenally large values of N (well over 100
million), only to cause the system to start thrashing lamentably
when you run it. (Provided, of course, you've done "ulimit -s
unlimited" before hand.) Other users on the system are likely
to be very displeased.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34