Max array size C++?

Started by
24 comments, last by phresnel 13 years, 11 months ago
I need a way to store a lots of numbers for a .3ds model loader. The problem is that it crashes when I try to increase the array. Like: Works: float X[2][5000]; float Y[2][5000]; float Z[2][5000]; Instant crash: float X[10][5000]; float Y[10][5000]; float Z[10][5000]; Have I reached some kind of a limit on the array size or what?
Advertisement
Probably; you might have reached the stack size limit. You can increase the stack size in Visual C++, under Project Properties, Linker, System.
Or allocate dynamically, assuming you have a 32-bit system, each array takes 5000 * 10 * 4 = 200.000 bytes. There's enough of that on the heap:

float *X = new float[10 * 5000];X[(y * 5000) + x] = 5.0f;delete[] X;
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Quote:Original post by _fastcall
Probably; you might have reached the stack size limit. You can increase the stack size in Visual C++, under Project Properties, Linker, System.


What do I need to change? This is what I see: http://img541.imageshack.us/img541/3460/51844983.jpg

Quote:Original post by Decrius
Or allocate dynamically, assuming you have a 32-bit system, each array takes 5000 * 10 * 4 = 200.000 bytes. There's enough of that on the heap:

float *X = new float[10 * 5000];X[(y * 5000) + x] = 5.0f;delete[] X;


Does that still retain the [10][5000] look? I want to be able to access it like [object][vertex] (dont know how else to explain it)
I wouldn't change the stack size at all, allocate the array with new[] instead - then you're bound by the available address space (~2GB on a 32-bit system), not the stack size.
Quote:Original post by TutenStain
Does that still retain the [10][5000] look? I want to be able to access it like [object][vertex] (dont know how else to explain it)


No it doesn't. But practically you still do the same thing, basically it expands as:

X[object][vertex] -> X[(object * max_vertices) + vertex] which equals *(X + (object * max_vertices) + vertex)

So yes, you do need to remember max_vertices.

[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Quote:Original post by Decrius
Quote:Original post by TutenStain
Does that still retain the [10][5000] look? I want to be able to access it like [object][vertex] (dont know how else to explain it)


No it doesn't. But practically you still do the same thing, basically it expands as:

X[object][vertex] -> X[(object * max_vertices) + vertex] which equals *(X + (object * max_vertices) + vertex)

So yes, you do need to remember max_vertices.


Make sense. But cant I do it this way:
float (*x)[1000] = new float[1000][1000];
Thats not how it works. This would be right:

const int X = 1000;
const int Y = 5000;
float* Foo = new float[X*Y];

And then access it like Decrius said.
Quote:Original post by mind in a box
Thats not how it works. This would be right:

const int X = 1000;
const int Y = 5000;
float* Foo = new float[X*Y];

And then access it like Decrius said.



Thanks, think I got it. Will turn back here if I have any questions.

Thanks again!
Quote:Original post by TutenStain
Make sense. But cant I do it this way:
float (*x)[1000] = new float[1000][1000];


Yes, that will work so long as you know all but the first dimension at compile time.

int n = 10;
float (*x)[1000] = new float[n][1000]; // works
float (*y)[n] = new float[1000][n]; // doesn't work

You may also be interested in boost::array and boost::multi_array.

This topic is closed to new replies.

Advertisement