Arrays/Vectors max elements dimension

Hi everybody,

I have a curiosity/problem about the dimension of my arrays.
I'm programming under linux & windows & mac, in a mathematical program.
So I need to use arrays and vectors like:
1
2
3
#define N 1000.....
double a[N];
double b[N][N];

But the problem is N. On my Linux I can use N < 1000000 (with g++), on Windows I arrive at N < 10000.
1) I understood that de max. dimension of my array depends of:
a) OS/Hardware limitations with I use a Dynamic Array like:
double *a = new double[N];
b) OS/Hardware + compiler stack size if I use a static array.
Is it correct?

2) Now if I use dynamic arrays, how can I determine (with precision) the maximun dimension (N) of my array that I can allocate, using C++ (or Qt classes)?
NB: My application will get vectors from user, so I would like to determine the OS/Hardware "limit" (before add data) and show a warning.

3) Is there some method/technic in C++ to allocate arrays with "infinite" dimension?
Last edited on
1) Largely, yes, though global variables are not allocated on the stack; they are elsewhere. The Windows compiler might have an arbitrary limitation on the size of the global data.

2) You can't in a platform independent fashion.

3) No, but you can resize the array to the size you need. Consider looking at
any of the STL containers, particularly vector. vector<> is limited to 2^32
elements or the size of available memory, whichever is tighter.
Thank you very much for the answers.
For such large datasets, a <deque> might be the best option, since it doesn't require contiguous memory. Look it up on the reference section of this site.
Only a last little thing, someone knows how to calculate exactly the array maximum size (in bits), platform and hardware independent? In differents posts people say things like:
8bits*N, or 32bits*N, or 16bits*N, sizeof(int)*N
Is there a solution?
Last edited on
2) You can't in a platform independent fashion.

It depends on the amount of memory available and the size of the elements you want to allocate.
For example, on a system with 2 GB of free RAM and sizeof(int)==4, you can allocate 536,870,912 ints. The number is inversely proportional to the size of the elements, and directly proportional to the amount of free memory.
Last edited on
Ok, thank you very much for all explanation.
Topic archived. No new replies allowed.