I’m currently writing a program where I need to run many simulations and speed is the name of the game. I’m currently using one long linear array allocated on heap and working out the dimensions myself and pulling straight from the index I work out. A co-worker has written a similar program and has got a good speed boost from using a 5D array. I know it would be very easy to allocate this as a vector, but as far as I’m aware there is a speed cost to using the vector as a result of bounds checking.
My question is why does this occur?
This compiles and runs fine:
int Array[20][20][20][20][20];
int main()
{
return 0;
}
This compiles but throws a stack overflow exception when wrapped in a class:
class Foo
{
int Array[20][20][20][20][20];
};
int main()
{
Foo foo;
return 0;
}
I have tried modifying my stack size in the Visual Studio 2005 linker options as well as changing the Enable Larger Addresses option, but nothing seems to make any difference.
The static array will be directly mapped into memory, not on the stack. The member variable will be allocated on the stack, which is likely not large enough by default. Also,
vectordoesn’t do any bounds checking inoperator[].